Skip to content

Commit

Permalink
Wait for natural end for progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
cardil committed Mar 22, 2024
1 parent 0ff3298 commit dd86562
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
16 changes: 12 additions & 4 deletions pkg/output/tui/progress.go
Expand Up @@ -175,17 +175,23 @@ func (b bubbleProgressHandler) speedChange() (tea.Model, tea.Cmd) {
}

func (b bubbleProgressHandler) percentChange(event percentChange) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd

cmds := make([]tea.Cmd, 0, 1)
cmds = append(cmds, b.prog.SetPercent(float64(event)))

if event >= 1.0 {
cmds = append(cmds, tea.Sequence(b.finalPause(), tea.Quit))
cmds = append(cmds, b.quitSignal())
}

return b, tea.Batch(cmds...)
}

func (b *BubbleProgress) quitSignal() tea.Cmd {
// The final pause is to give the progress bar a chance to finish its
// animation before quitting. Otherwise, it ends abruptly, and the user
// might not see the progress bar at 100%.
return tea.Sequence(b.finalPause(), tea.Quit)
}

func (b bubbleProgressHandler) progressFrame(event progress.FrameMsg) (tea.Model, tea.Cmd) {
progressModel, cmd := b.prog.Update(event)
if m, ok := progressModel.(progress.Model); ok {
Expand Down Expand Up @@ -252,8 +258,10 @@ func (b *BubbleProgress) stop() {
if b.tea == nil {
return
}
b.tea.Quit()

b.tea.Send(b.quitSignal())
<-b.quitChan

b.tea = nil
b.quitChan = nil
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/output/tui/spinner.go
Expand Up @@ -36,7 +36,6 @@ func (w *widgets) NewSpinner(message string) Spinner {
return &BubbleSpinner{
InputOutput: output.PrinterFrom(w.ctx),
Message: Message{Text: message},
quitChan: make(chan struct{}),
}
}

Expand Down Expand Up @@ -79,6 +78,7 @@ func (b *BubbleSpinner) start() {
tea.WithInput(b.InOrStdin()),
tea.WithOutput(out),
)
b.quitChan = make(chan struct{})
go func() {
t := b.tea
_, _ = t.Run()
Expand All @@ -93,9 +93,12 @@ func (b *BubbleSpinner) stop() {
if b.tea == nil {
return
}

b.tea.Quit()
<-b.quitChan

b.tea = nil
b.quitChan = nil
endMsg := fmt.Sprintf("%s %s\n",
b.Message.Text, spinnerStyle().Render("Done"))
_, _ = b.OutOrStdout().Write([]byte(endMsg))
Expand Down

0 comments on commit dd86562

Please sign in to comment.