fix(progress): set a custom termenv color profile (#152)

This commit is contained in:
Ayman Bagabas 2022-06-01 21:04:33 -04:00 committed by GitHub
parent fd03b6195d
commit 7959eb4867
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -37,8 +37,6 @@ const (
defaultDamping = 1.0 defaultDamping = 1.0
) )
var color func(string) termenv.Color = termenv.ColorProfile().Color
// Option is used to set options in NewModel. For example: // Option is used to set options in NewModel. For example:
// //
// progress := NewModel( // progress := NewModel(
@ -110,6 +108,13 @@ 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. // FrameMsg indicates that an animation step should occur.
type FrameMsg struct { type FrameMsg struct {
id int id int
@ -157,6 +162,9 @@ type Model struct {
// of the progress bar. When false, the width of the gradient will be set // of the progress bar. When false, the width of the gradient will be set
// to the full width of the progress bar. // to the full width of the progress bar.
scaleRamp bool scaleRamp bool
// Color profile for the progress bar.
colorProfile termenv.Profile
} }
// New returns a model with default values. // New returns a model with default values.
@ -170,6 +178,7 @@ func New(opts ...Option) Model {
EmptyColor: "#606060", EmptyColor: "#606060",
ShowPercentage: true, ShowPercentage: true,
PercentFormat: " %3.0f%%", PercentFormat: " %3.0f%%",
colorProfile: termenv.ColorProfile(),
} }
if !m.springCustomized { if !m.springCustomized {
m.SetSpringOptions(defaultFrequency, defaultDamping) m.SetSpringOptions(defaultFrequency, defaultDamping)
@ -299,18 +308,18 @@ func (m Model) barView(b *strings.Builder, percent float64, textWidth int) {
c := m.rampColorA.BlendLuv(m.rampColorB, p).Hex() c := m.rampColorA.BlendLuv(m.rampColorB, p).Hex()
b.WriteString(termenv. b.WriteString(termenv.
String(string(m.Full)). String(string(m.Full)).
Foreground(color(c)). Foreground(m.color(c)).
String(), String(),
) )
} }
} else { } else {
// Solid fill // Solid fill
s := termenv.String(string(m.Full)).Foreground(color(m.FullColor)).String() s := termenv.String(string(m.Full)).Foreground(m.color(m.FullColor)).String()
b.WriteString(strings.Repeat(s, fw)) b.WriteString(strings.Repeat(s, fw))
} }
// Empty fill // Empty fill
e := termenv.String(string(m.Empty)).Foreground(color(m.EmptyColor)).String() e := termenv.String(string(m.Empty)).Foreground(m.color(m.EmptyColor)).String()
n := max(0, tw-fw) n := max(0, tw-fw)
b.WriteString(strings.Repeat(e, n)) b.WriteString(strings.Repeat(e, n))
} }
@ -338,6 +347,10 @@ func (m *Model) setRamp(colorA, colorB string, scaled bool) {
m.rampColorB = b m.rampColorB = b
} }
func (m Model) color(c string) termenv.Color {
return m.colorProfile.Color(c)
}
func max(a, b int) int { func max(a, b int) int {
if a > b { if a > b {
return a return a