Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Jun 2, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 18 additions & 5 deletions progress/progress.go
Expand Up @@ -37,8 +37,6 @@ const (
defaultDamping = 1.0
)

var color func(string) termenv.Color = termenv.ColorProfile().Color

// Option is used to set options in NewModel. For example:
//
// progress := NewModel(
Expand Down Expand Up @@ -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.
type FrameMsg struct {
id int
Expand Down Expand Up @@ -157,6 +162,9 @@ 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 @@ -170,6 +178,7 @@ func New(opts ...Option) Model {
EmptyColor: "#606060",
ShowPercentage: true,
PercentFormat: " %3.0f%%",
colorProfile: termenv.ColorProfile(),
}
if !m.springCustomized {
m.SetSpringOptions(defaultFrequency, defaultDamping)
Expand Down Expand Up @@ -299,18 +308,18 @@ func (m Model) barView(b *strings.Builder, percent float64, textWidth int) {
c := m.rampColorA.BlendLuv(m.rampColorB, p).Hex()
b.WriteString(termenv.
String(string(m.Full)).
Foreground(color(c)).
Foreground(m.color(c)).
String(),
)
}
} else {
// 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))
}

// 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)
b.WriteString(strings.Repeat(e, n))
}
Expand Down Expand Up @@ -338,6 +347,10 @@ 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