From cbd13a7d865175ecc36563e77867c17b5ef68d49 Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Thu, 21 Nov 2024 14:45:53 -0500 Subject: [PATCH] chore(progress)!: migrate progress to lipgloss This removes the dependency on termenv and replaces it with lipgloss. This change also removes the WithColorProfile option, as it is no longer needed. The new API uses `color.Color` types for colors, which are more flexible and allow for more advanced color manipulation. --- progress/progress.go | 45 ++++++++++++--------------------------- progress/progress_test.go | 14 ++++++------ 2 files changed, 21 insertions(+), 38 deletions(-) diff --git a/progress/progress.go b/progress/progress.go index b4c91295..b596c30c 100644 --- a/progress/progress.go +++ b/progress/progress.go @@ -2,6 +2,7 @@ package progress import ( "fmt" + "image/color" "math" "strings" "sync/atomic" @@ -12,7 +13,6 @@ import ( "github.com/charmbracelet/lipgloss/v2" "github.com/charmbracelet/x/ansi" "github.com/lucasb-eyer/go-colorful" - "github.com/muesli/termenv" ) // Internal ID management. Used during animating to assure that frame messages @@ -65,7 +65,7 @@ func WithScaledGradient(colorA, colorB string) Option { } // WithSolidFill sets the progress to use a solid fill with the given color. -func WithSolidFill(color string) Option { +func WithSolidFill(color color.Color) Option { return func(m *Model) { m.FullColor = color m.useRamp = false @@ -108,13 +108,6 @@ func WithSpringOptions(frequency, damping float64) Option { } } -// WithColorProfile sets the color profile to use for the progress bar. -func WithColorProfile(p termenv.Profile) Option { - return func(m *Model) { - m.colorProfile = p - } -} - // FrameMsg indicates that an animation step should occur. type FrameMsg struct { id int @@ -135,11 +128,11 @@ type Model struct { // "Filled" sections of the progress bar. Full rune - FullColor string + FullColor color.Color // "Empty" sections of the progress bar. Empty rune - EmptyColor string + EmptyColor color.Color // Settings for rendering the numeric percentage. ShowPercentage bool @@ -162,9 +155,6 @@ type Model struct { // of the progress bar. When false, the width of the gradient will be set // to the full width of the progress bar. scaleRamp bool - - // Color profile for the progress bar. - colorProfile termenv.Profile } // New returns a model with default values. @@ -173,12 +163,11 @@ func New(opts ...Option) Model { id: nextID(), width: defaultWidth, Full: '█', - FullColor: "#7571F9", + FullColor: lipgloss.Color("#7571F9"), Empty: '░', - EmptyColor: "#606060", + EmptyColor: lipgloss.Color("#606060"), ShowPercentage: true, PercentFormat: " %3.0f%%", - colorProfile: termenv.ColorProfile(), } for _, opt := range opts { @@ -316,23 +305,21 @@ func (m Model) barView(b *strings.Builder, percent float64, textWidth int) { } else { p = float64(i) / float64(tw-1) } - c := m.rampColorA.BlendLuv(m.rampColorB, p).Hex() - b.WriteString(termenv. - String(string(m.Full)). - Foreground(m.color(c)). - String(), - ) + c := m.rampColorA.BlendLuv(m.rampColorB, p) + b.WriteString(lipgloss.NewStyle().Foreground(c).Render(string(m.Full))) } } else { // Solid fill - s := termenv.String(string(m.Full)).Foreground(m.color(m.FullColor)).String() - b.WriteString(strings.Repeat(s, fw)) + b.WriteString(lipgloss.NewStyle(). + Foreground(m.FullColor). + Render(strings.Repeat(string(m.Full), fw))) } // Empty fill - e := termenv.String(string(m.Empty)).Foreground(m.color(m.EmptyColor)).String() n := max(0, tw-fw) - b.WriteString(strings.Repeat(e, n)) + b.WriteString(lipgloss.NewStyle(). + Foreground(m.EmptyColor). + Render(strings.Repeat(string(m.Empty), n))) } func (m Model) percentageView(percent float64) string { @@ -358,10 +345,6 @@ func (m *Model) setRamp(colorA, colorB string, scaled bool) { m.rampColorB = b } -func (m Model) color(c string) termenv.Color { - return m.colorProfile.Color(c) -} - func max(a, b int) int { if a > b { return a diff --git a/progress/progress_test.go b/progress/progress_test.go index a4ba720b..324d417c 100644 --- a/progress/progress_test.go +++ b/progress/progress_test.go @@ -4,15 +4,14 @@ import ( "strings" "testing" - "github.com/muesli/termenv" + "github.com/charmbracelet/lipgloss/v2" ) const ( - AnsiReset = "\x1b[0m" + AnsiReset = "\x1b[m" ) func TestGradient(t *testing.T) { - colA := "#FF0000" colB := "#00FF00" @@ -21,7 +20,7 @@ func TestGradient(t *testing.T) { for _, scale := range []bool{false, true} { opts := []Option{ - WithColorProfile(termenv.TrueColor), WithoutPercentage(), + WithoutPercentage(), } if scale { descr = "progress bar with scaled gradient" @@ -36,10 +35,12 @@ func TestGradient(t *testing.T) { // build the expected colors by colorizing an empty string and then cutting off the following reset sequence sb := strings.Builder{} - sb.WriteString(termenv.String("").Foreground(p.color(colA)).String()) + // sb.WriteString(termenv.String("").Foreground(p.color(colA)).String()) + sb.WriteString(lipgloss.NewStyle().Foreground(lipgloss.Color(colA)).String()) expFirst := strings.Split(sb.String(), AnsiReset)[0] sb.Reset() - sb.WriteString(termenv.String("").Foreground(p.color(colB)).String()) + // sb.WriteString(termenv.String("").Foreground(p.color(colB)).String()) + sb.WriteString(lipgloss.NewStyle().Foreground(lipgloss.Color(colB)).String()) expLast := strings.Split(sb.String(), AnsiReset)[0] for _, width := range []int{3, 5, 50} { @@ -62,5 +63,4 @@ func TestGradient(t *testing.T) { } }) } - }