Skip to content

Commit

Permalink
feat: add accent color support to the progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
tulilirockz committed Dec 8, 2024
1 parent 575c827 commit b0d7584
Showing 1 changed file with 70 additions and 1 deletion.
71 changes: 70 additions & 1 deletion lib/percentmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"fmt"
"log/slog"
"math"
"os"
"strconv"
"strings"
"time"

"github.com/jedib0t/go-pretty/v6/progress"
Expand Down Expand Up @@ -32,8 +35,50 @@ var CuteColors = progress.StyleColors{
Speed: text.Colors{text.FgBlue},
}

func setColorsAccent(colors *progress.StyleColors, accent string) {
highlightColor := text.Colors{text.FgHiBlue}
lowColor := text.Colors{text.FgBlue}

switch accent {
case "blue":
highlightColor = text.Colors{text.FgHiBlue}
lowColor = text.Colors{text.FgBlue}
case "teal":
highlightColor = text.Colors{text.FgHiCyan}
lowColor = text.Colors{text.FgCyan}
case "green":
highlightColor = text.Colors{text.FgHiGreen}
lowColor = text.Colors{text.FgGreen}
case "yellow":
highlightColor = text.Colors{text.FgHiYellow}
lowColor = text.Colors{text.FgYellow}
case "orange":
highlightColor = text.Colors{text.FgHiYellow}
lowColor = text.Colors{text.FgYellow}
case "red":
highlightColor = text.Colors{text.FgHiRed}
lowColor = text.Colors{text.FgRed}
case "pink":
highlightColor = text.Colors{text.FgHiMagenta}
lowColor = text.Colors{text.FgMagenta}
case "purple":
highlightColor = text.Colors{text.FgHiMagenta}
lowColor = text.Colors{text.FgMagenta}
case "slate":
highlightColor = text.Colors{text.FgHiWhite}
lowColor = text.Colors{text.FgWhite}
}

colors.Percent = highlightColor
colors.Tracker = highlightColor
colors.Time = lowColor
colors.Value = lowColor
colors.Speed = lowColor
}

func NewProgressWriter() progress.Writer {
pw := progress.NewWriter()

pw.SetTrackerLength(25)
pw.Style().Visibility.TrackerOverall = true
pw.Style().Visibility.Time = true
Expand All @@ -45,7 +90,31 @@ func NewProgressWriter() progress.Writer {
pw.SetTrackerPosition(progress.PositionRight)
pw.SetUpdateFrequency(time.Millisecond * 100)
pw.Style().Options.PercentFormat = "%4.1f%%"
pw.Style().Colors = CuteColors

colorsSet := CuteColors

var targetUser int
baseUser, exists := os.LookupEnv("SUDO_UID")
if !exists || baseUser == "" {
targetUser = 0
} else {
var err error
targetUser, err = strconv.Atoi(baseUser)
if err != nil {
slog.Error("Failed parsing provided user as UID", slog.String("user_value", baseUser))
return pw
}
}

if targetUser != 0 {
cli := []string{"gsettings", "get", "org.gnome.desktop.interface", "accent-color"}
out, err := RunUID(targetUser, cli, nil)
accent := strings.TrimSpace(strings.ReplaceAll(string(out), "'", ""))
if err == nil {
setColorsAccent(&colorsSet, accent)
}
}
pw.Style().Colors = colorsSet
return pw
}

Expand Down

0 comments on commit b0d7584

Please sign in to comment.