From ac024fff3ce70b435a39de83f7aa3339668fdc6b Mon Sep 17 00:00:00 2001 From: Benjamin Chausse Date: Sun, 24 Nov 2024 02:59:00 -0500 Subject: Instant slider animation --- internal/slider/slider.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'internal/slider') diff --git a/internal/slider/slider.go b/internal/slider/slider.go index 884d141..bfbc069 100644 --- a/internal/slider/slider.go +++ b/internal/slider/slider.go @@ -35,11 +35,20 @@ func New(label byte, maxVal int, opts ...progress.Option) Model { func (m Model) Title() string { return fmt.Sprintf("%c", m.label) } func (m Model) Init() tea.Cmd { - return m.progress.Init() + // Triggering a frame message Update here will force the progress bar to + // render immediately. This is necessary because progress bars only render + // when their state changes. Without this, there is a disrepancy between + // the initial state of the progress bar and the initial state of the slider. + + // There's no sugar-coating it: This is a hack. But it works... + _, cmd := m.Update(progress.FrameMsg{}) + return cmd } func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + cmds := []tea.Cmd{} keys := newKeybinds() + switch msg := msg.(type) { case tea.KeyMsg: switch { @@ -54,12 +63,15 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } return m, m.progress.SetPercent(m.Pcnt()) case progress.FrameMsg: + if m.progress.Percent() != m.Pcnt() { + cmds = append(cmds, m.progress.SetPercent(m.Pcnt())) + } progressModel, cmd := m.progress.Update(msg) m.progress = progressModel.(progress.Model) - return m, cmd + cmds = append(cmds, cmd) default: - return m, nil } + return m, tea.Batch(cmds...) } func (m Model) ViewValue(current int) string { -- cgit v1.2.3