From 81c6bd53b95884d4a8cf1d0bf6b0e2019eb937bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chris=20Suszy=C5=84ski?= Date: Thu, 21 Mar 2024 20:23:11 +0100 Subject: [PATCH] Wait for natural end for progress bar --- pkg/output/tui/progress.go | 25 ++++++++++++++++--------- pkg/output/tui/spinner.go | 5 ++++- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/pkg/output/tui/progress.go b/pkg/output/tui/progress.go index 4bd8703b..07e4a3f6 100644 --- a/pkg/output/tui/progress.go +++ b/pkg/output/tui/progress.go @@ -175,15 +175,11 @@ func (b bubbleProgressHandler) speedChange() (tea.Model, tea.Cmd) { } func (b bubbleProgressHandler) percentChange(event percentChange) (tea.Model, tea.Cmd) { - var cmds []tea.Cmd - - cmds = append(cmds, b.prog.SetPercent(float64(event))) - - if event >= 1.0 { - cmds = append(cmds, tea.Sequence(b.finalPause(), tea.Quit)) + if event > 1.0 { + event = 1.0 } - return b, tea.Batch(cmds...) + return b, b.prog.SetPercent(float64(event)) } func (b bubbleProgressHandler) progressFrame(event progress.FrameMsg) (tea.Model, tea.Cmd) { @@ -252,8 +248,19 @@ func (b *BubbleProgress) stop() { if b.tea == nil { return } - b.tea.Quit() + + finalize := make(chan struct{}) + go func() { + b.tea.Wait() + close(finalize) + }() + b.tea.Send(tea.Sequence( + b.prog.SetPercent(1.0), + b.finalPause(), + )) <-b.quitChan + <-finalize + b.tea = nil b.quitChan = nil } @@ -268,7 +275,7 @@ func (b *BubbleProgress) finalPause() tea.Cmd { pause = speedInterval * 3 } return tea.Tick(pause, func(_ time.Time) tea.Msg { - return nil + return tea.Quit() }) } diff --git a/pkg/output/tui/spinner.go b/pkg/output/tui/spinner.go index 33f57f4c..438ef358 100644 --- a/pkg/output/tui/spinner.go +++ b/pkg/output/tui/spinner.go @@ -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{}), } } @@ -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() @@ -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))