Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added background color to progress bar #535

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions progress/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ func WithSolidFill(color string) Option {
}
}

func WithBackgroundColor(color string) Option {
return func(m *Model) {
m.BackgroundColor = color
}
}

// WithFillCharacters sets the characters used to construct the full and empty components of the progress bar.
func WithFillCharacters(full rune, empty rune) Option {
return func(m *Model) {
Expand Down Expand Up @@ -148,6 +154,9 @@ type Model struct {
Empty rune
EmptyColor string

// Background color of the progress bar
BackgroundColor string

// Settings for rendering the numeric percentage.
ShowPercentage bool
PercentFormat string // a fmt string for a float
Expand Down Expand Up @@ -303,6 +312,8 @@ func (m Model) barView(b *strings.Builder, percent float64, textWidth int) {

fw = max(0, min(tw, fw))

bg := m.color(m.BackgroundColor)

if m.useRamp {
// Gradient fill
for i := 0; i < fw; i++ {
Expand All @@ -320,17 +331,18 @@ func (m Model) barView(b *strings.Builder, percent float64, textWidth int) {
b.WriteString(termenv.
String(string(m.Full)).
Foreground(m.color(c)).
Background(bg).
String(),
)
}
} else {
// Solid fill
s := termenv.String(string(m.Full)).Foreground(m.color(m.FullColor)).String()
s := termenv.String(string(m.Full)).Foreground(m.color(m.FullColor)).Background(bg).String()
b.WriteString(strings.Repeat(s, fw))
}

// Empty fill
e := termenv.String(string(m.Empty)).Foreground(m.color(m.EmptyColor)).String()
e := termenv.String(string(m.Empty)).Foreground(m.color(m.EmptyColor)).Background(bg).String()
n := max(0, tw-fw)
b.WriteString(strings.Repeat(e, n))
}
Expand Down Expand Up @@ -359,6 +371,10 @@ func (m *Model) setRamp(colorA, colorB string, scaled bool) {
}

func (m Model) color(c string) termenv.Color {
if c == "" {
return termenv.NoColor{}
}

return m.colorProfile.Color(c)
}

Expand Down