From 12af260dfe0dc0399ded94e58c7706933c1189ee Mon Sep 17 00:00:00 2001 From: Tanish Azad Date: Tue, 4 Jun 2024 01:48:02 +0530 Subject: [PATCH 1/2] added background to progress bar --- progress/progress.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/progress/progress.go b/progress/progress.go index fa04635a..d987c245 100644 --- a/progress/progress.go +++ b/progress/progress.go @@ -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) { @@ -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 @@ -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++ { @@ -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)) } @@ -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) } From 302b5489a46418c468397e32bff5f4020ab7a744 Mon Sep 17 00:00:00 2001 From: Tanish Azad Date: Tue, 4 Jun 2024 02:15:48 +0530 Subject: [PATCH 2/2] gofmt --- progress/progress.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/progress/progress.go b/progress/progress.go index d987c245..249c248f 100644 --- a/progress/progress.go +++ b/progress/progress.go @@ -80,9 +80,9 @@ func WithSolidFill(color string) Option { } func WithBackgroundColor(color string) Option { - return func(m *Model) { - m.BackgroundColor = color - } + return func(m *Model) { + m.BackgroundColor = color + } } // WithFillCharacters sets the characters used to construct the full and empty components of the progress bar. @@ -154,8 +154,8 @@ type Model struct { Empty rune EmptyColor string - // Background color of the progress bar - BackgroundColor string + // Background color of the progress bar + BackgroundColor string // Settings for rendering the numeric percentage. ShowPercentage bool @@ -312,7 +312,7 @@ func (m Model) barView(b *strings.Builder, percent float64, textWidth int) { fw = max(0, min(tw, fw)) - bg := m.color(m.BackgroundColor) + bg := m.color(m.BackgroundColor) if m.useRamp { // Gradient fill @@ -331,7 +331,7 @@ func (m Model) barView(b *strings.Builder, percent float64, textWidth int) { b.WriteString(termenv. String(string(m.Full)). Foreground(m.color(c)). - Background(bg). + Background(bg). String(), ) } @@ -371,9 +371,9 @@ func (m *Model) setRamp(colorA, colorB string, scaled bool) { } func (m Model) color(c string) termenv.Color { - if c == "" { - return termenv.NoColor{} - } + if c == "" { + return termenv.NoColor{} + } return m.colorProfile.Color(c) }