Progress's NewModel no longer returns an error for better usability

To that end, bad hex colors also default to black. This is also how we
handle bad hex notation in Lip Gloss.
This commit is contained in:
Christian Rocha 2021-07-21 19:24:34 -04:00
parent 446433d680
commit 202d5a9f9d

View File

@ -46,7 +46,7 @@ var color func(string) termenv.Color = termenv.ColorProfile().Color
// WithoutPercentage(), // WithoutPercentage(),
// ) // )
// //
type Option func(*Model) error type Option func(*Model)
// WithDefaultGradient sets a gradient fill with default colors. // WithDefaultGradient sets a gradient fill with default colors.
func WithDefaultGradient() Option { func WithDefaultGradient() Option {
@ -55,8 +55,8 @@ func WithDefaultGradient() Option {
// WithGradient sets a gradient fill blending between two colors. // WithGradient sets a gradient fill blending between two colors.
func WithGradient(colorA, colorB string) Option { func WithGradient(colorA, colorB string) Option {
return func(m *Model) error { return func(m *Model) {
return m.setRamp(colorA, colorB, false) m.setRamp(colorA, colorB, false)
} }
} }
@ -69,25 +69,23 @@ func WithDefaultScaledGradient() Option {
// WithScaledGradient scales the gradient to fit the width of the filled portion of // WithScaledGradient scales the gradient to fit the width of the filled portion of
// the progress bar. // the progress bar.
func WithScaledGradient(colorA, colorB string) Option { func WithScaledGradient(colorA, colorB string) Option {
return func(m *Model) error { return func(m *Model) {
return m.setRamp(colorA, colorB, true) m.setRamp(colorA, colorB, true)
} }
} }
// WithSolidFill sets the progress to use a solid fill with the given color. // WithSolidFill sets the progress to use a solid fill with the given color.
func WithSolidFill(color string) Option { func WithSolidFill(color string) Option {
return func(m *Model) error { return func(m *Model) {
m.FullColor = color m.FullColor = color
m.useRamp = false m.useRamp = false
return nil
} }
} }
// WithoutPercentage hides the numeric percentage. // WithoutPercentage hides the numeric percentage.
func WithoutPercentage() Option { func WithoutPercentage() Option {
return func(m *Model) error { return func(m *Model) {
m.ShowPercentage = false m.ShowPercentage = false
return nil
} }
} }
@ -95,9 +93,8 @@ func WithoutPercentage() Option {
// set the width via the Width property, which can come in handy if you're // set the width via the Width property, which can come in handy if you're
// waiting for a tea.WindowSizeMsg. // waiting for a tea.WindowSizeMsg.
func WithWidth(w int) Option { func WithWidth(w int) Option {
return func(m *Model) error { return func(m *Model) {
m.Width = w m.Width = w
return nil
} }
} }
@ -150,7 +147,7 @@ type Model struct {
} }
// NewModel returns a model with default values. // NewModel returns a model with default values.
func NewModel(opts ...Option) (Model, error) { func NewModel(opts ...Option) Model {
m := Model{ m := Model{
id: nextID(), id: nextID(),
Width: defaultWidth, Width: defaultWidth,
@ -161,16 +158,12 @@ func NewModel(opts ...Option) (Model, error) {
ShowPercentage: true, ShowPercentage: true,
PercentFormat: " %3.0f%%", PercentFormat: " %3.0f%%",
} }
m.SetSpringOptions(defaultFrequency, defaultDamping) m.SetSpringOptions(defaultFrequency, defaultDamping)
for _, opt := range opts { for _, opt := range opts {
if err := opt(&m); err != nil { opt(&m)
return Model{}, err
}
} }
return m
return m, nil
} }
// Update is used to animation the progress bar during transitons. Use // Update is used to animation the progress bar during transitons. Use
@ -306,22 +299,17 @@ func (m Model) percentageView(percent float64) string {
return percentage return percentage
} }
func (m *Model) setRamp(colorA, colorB string, scaled bool) error { func (m *Model) setRamp(colorA, colorB string, scaled bool) {
a, err := colorful.Hex(colorA) // In the event of an error colors here will default to black. For
if err != nil { // usability's sake, and because such an error is only cosmetic, we're
return err // ignoring the error for sake of usability.
} a, _ := colorful.Hex(colorA)
b, _ := colorful.Hex(colorB)
b, err := colorful.Hex(colorB)
if err != nil {
return err
}
m.useRamp = true m.useRamp = true
m.scaleRamp = scaled m.scaleRamp = scaled
m.rampColorA = a m.rampColorA = a
m.rampColorB = b m.rampColorB = b
return nil
} }
func max(a, b int) int { func max(a, b int) int {