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 13, 2024
1 parent 6b7d8bf commit 7add70a
Showing 1 changed file with 67 additions and 1 deletion.
68 changes: 67 additions & 1 deletion lib/percentmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,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 +87,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 7add70a

Please sign in to comment.