Skip to content

Commit

Permalink
chore(progress)!: migrate progress to lipgloss
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aymanbagabas committed Nov 21, 2024
1 parent bd415b4 commit cbd13a7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 38 deletions.
45 changes: 14 additions & 31 deletions progress/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package progress

import (
"fmt"
"image/color"
"math"
"strings"
"sync/atomic"
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
14 changes: 7 additions & 7 deletions progress/progress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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"
Expand All @@ -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} {
Expand All @@ -62,5 +63,4 @@ func TestGradient(t *testing.T) {
}
})
}

}

0 comments on commit cbd13a7

Please sign in to comment.