From 129116f187a433e2566a24e7b094172118ea3d8f Mon Sep 17 00:00:00 2001 From: Alex Goodman Date: Tue, 14 Nov 2023 16:57:46 -0500 Subject: [PATCH] update taskprogress model to implement VisibleModel Signed-off-by: Alex Goodman --- bubbles/taskprogress/model.go | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/bubbles/taskprogress/model.go b/bubbles/taskprogress/model.go index 9567c17..a88f87a 100644 --- a/bubbles/taskprogress/model.go +++ b/bubbles/taskprogress/model.go @@ -13,6 +13,8 @@ import ( tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" "github.com/wagoodman/go-progress" + + "github.com/anchore/bubbly" ) const ( @@ -20,6 +22,8 @@ const ( xMark = "✘" ) +var _ bubbly.VisibleModel = (*Model)(nil) + type Model struct { // ui components (view models) Spinner spinner.Model @@ -147,10 +151,6 @@ func (m Model) Init() tea.Cmd { m.ProgressBar.Init(), } - // if m.progressor != nil { - // cmds = append(cmds, m.ProgressBar.Init()) - //} - return tea.Batch( cmds..., ) @@ -161,7 +161,7 @@ func (m Model) ID() int { return m.id } -// ID returns the spinner's unique ID. +// Sequence returns the spinner's current sequence number. func (m Model) Sequence() int { return m.sequence } @@ -234,9 +234,21 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } } +func (m Model) IsVisible() bool { + isDoneAndHidden := m.completed && m.HideOnSuccess + if isDoneAndHidden { + // it might be that the consumer will not invoke View() again based on + // this response, in which case we need to ensure that the done() function + // in invoked to release resources + m.done() + } + + return !(isDoneAndHidden) +} + // View renders the model's view. func (m Model) View() string { - if m.completed && m.HideOnSuccess { + if !m.IsVisible() { m.done() return "" }