mirror of
https://github.com/Maks1mS/bubbles.git
synced 2025-03-14 04:53:43 +03:00
Set ramp in NewModel via functional argument
This commit is contained in:
parent
d81e5713d4
commit
1e16eca939
@ -11,20 +11,43 @@ import (
|
|||||||
|
|
||||||
var color func(string) termenv.Color = termenv.ColorProfile().Color
|
var color func(string) termenv.Color = termenv.ColorProfile().Color
|
||||||
|
|
||||||
|
// Option is used to set options in NewModel. For example:
|
||||||
|
//
|
||||||
|
// progress := NewModel(
|
||||||
|
// WithRamp("#ff0000", "#0000ff"),
|
||||||
|
// )
|
||||||
|
type Option func(*Model)
|
||||||
|
|
||||||
|
// WithDefaultRamp sets a gradient fill with default colors.
|
||||||
|
func WithDefaultRamp() Option {
|
||||||
|
return WithRamp("#00dbde", "#fc00ff")
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithRamp sets a gradient fill blending between two colors.
|
||||||
|
func WithRamp(colorA, colorB string) Option {
|
||||||
|
a, _ := colorful.Hex(colorA)
|
||||||
|
b, _ := colorful.Hex(colorB)
|
||||||
|
return func(m *Model) {
|
||||||
|
m.useRamp = true
|
||||||
|
m.colorA = a
|
||||||
|
m.colorB = b
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type Model struct {
|
type Model struct {
|
||||||
// Left side color of progress bar. By default, it's #00dbde
|
// Left side color of progress bar. By default, it's #00dbde
|
||||||
StartColor colorful.Color
|
colorA colorful.Color
|
||||||
|
|
||||||
// Left side color of progress bar. By default, it's #fc00ff
|
// Left side color of progress bar. By default, it's #fc00ff
|
||||||
EndColor colorful.Color
|
colorB colorful.Color
|
||||||
|
|
||||||
// Width of progress bar in symbols.
|
// Total width of the progress bar, including percentage, if set.
|
||||||
Width int
|
Width int
|
||||||
|
|
||||||
// filament rune of done part of progress bar. it's █ by default
|
// Rune for "filled" sections of the progress bar.
|
||||||
Full rune
|
Full rune
|
||||||
|
|
||||||
// empty rune of pending part of progress bar. it's ░ by default
|
// Rune for "empty" sections of progress bar.
|
||||||
Empty rune
|
Empty rune
|
||||||
|
|
||||||
// if true, gradient will be setted from start to end of filled part. Instead, it'll work
|
// if true, gradient will be setted from start to end of filled part. Instead, it'll work
|
||||||
@ -33,21 +56,25 @@ type Model struct {
|
|||||||
|
|
||||||
ShowPercent bool
|
ShowPercent bool
|
||||||
PercentFormat string
|
PercentFormat string
|
||||||
|
|
||||||
|
useRamp bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewModel returns a model with default values.
|
// NewModel returns a model with default values.
|
||||||
func NewModel() Model {
|
func NewModel(opts ...Option) *Model {
|
||||||
startColor, _ := colorful.Hex("#00dbde")
|
m := &Model{
|
||||||
endColor, _ := colorful.Hex("#fc00ff")
|
|
||||||
return Model{
|
|
||||||
StartColor: startColor,
|
|
||||||
EndColor: endColor,
|
|
||||||
Width: 40,
|
Width: 40,
|
||||||
Full: '█',
|
Full: '█',
|
||||||
Empty: '░',
|
Empty: '░',
|
||||||
ShowPercent: true,
|
ShowPercent: true,
|
||||||
PercentFormat: " %3.0f%%",
|
PercentFormat: " %3.0f%%",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m Model) View(percent float64) string {
|
func (m Model) View(percent float64) string {
|
||||||
@ -69,7 +96,7 @@ func (m Model) bar(percent float64, textWidth int) string {
|
|||||||
gradientPart = float64(len(ramp))
|
gradientPart = float64(len(ramp))
|
||||||
}
|
}
|
||||||
percent := float64(i) / gradientPart
|
percent := float64(i) / gradientPart
|
||||||
c := m.StartColor.BlendLuv(m.EndColor, percent)
|
c := m.colorA.BlendLuv(m.colorB, percent)
|
||||||
ramp[i] = c.Hex()
|
ramp[i] = c.Hex()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user