mirror of
https://github.com/zoriya/bubbles.git
synced 2026-08-16 01:43:21 +00:00
chore(progress)!: migrate progress to lipgloss (#676)
* 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. * Update progress/progress_test.go Co-authored-by: Christian Rocha <christian@rocha.is> * Update progress/progress_test.go Co-authored-by: Christian Rocha <christian@rocha.is> * chore: go mod tidy --------- Co-authored-by: Christian Rocha <christian@rocha.is>
This commit is contained in:
co-authored by
Christian Rocha
parent
bd415b4eba
commit
2e3a42396e
+14
-31
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user