Reduce progress component to (more or less) a pure view function

We really only need the View function in this case since the progress
value will come from the program that implements this.
This commit is contained in:
Christian Rocha 2020-11-18 14:00:02 -05:00 committed by Christian Rocha
parent 68e87e08c5
commit 3dea7d036e

View File

@ -3,14 +3,10 @@ package progress
import ( import (
"strings" "strings"
tea "github.com/charmbracelet/bubbletea"
"github.com/lucasb-eyer/go-colorful" "github.com/lucasb-eyer/go-colorful"
"github.com/muesli/termenv" "github.com/muesli/termenv"
) )
// Value is explicit type of, you know, progression. Can be 0.0 < x < 1.0.
type Value float64
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 StartColor colorful.Color
@ -21,10 +17,6 @@ type Model struct {
// Width of progress bar in symbols. // Width of progress bar in symbols.
Width int Width int
// Which part of bar need to visualise. Can be 0.0 < Progress < 1.0, if value is bigger or smaller, it'll
// be mapped to this values
Progress float64
// filament rune of done part of progress bar. it's █ by default // filament rune of done part of progress bar. it's █ by default
FilamentSymbol rune FilamentSymbol rune
@ -49,28 +41,8 @@ func NewModel(size int) *Model {
} }
} }
func (m *Model) Init() tea.Cmd { func (m *Model) View(percent float64) string {
return nil ramp := make([]string, int(float64(m.Width)*percent))
}
func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch cmd := msg.(type) {
case Value:
if cmd > 1 {
cmd = 1
}
if cmd < 0 {
cmd = 0
}
m.Progress = float64(cmd)
}
return m, nil
}
func (m *Model) View() string {
ramp := make([]string, int(float64(m.Width)*m.Progress))
for i := 0; i < len(ramp); i++ { for i := 0; i < len(ramp); i++ {
gradientPart := float64(m.Width) gradientPart := float64(m.Width)
if m.FullGradientMode { if m.FullGradientMode {