From 76e6c9b1056a1aeffcc04878637ea735af9854d0 Mon Sep 17 00:00:00 2001 From: dreamjz <25699818+dreamjz@users.noreply.github.com> Date: Sun, 24 Dec 2023 02:41:51 +0800 Subject: [PATCH] feat(dl): enable auto-quit (#20) --- cmd/download.go | 14 ++++++++++---- internal/tui/progressbar/update.go | 5 +++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/cmd/download.go b/cmd/download.go index db9055e..d9acd6d 100644 --- a/cmd/download.go +++ b/cmd/download.go @@ -91,11 +91,17 @@ func download(aniURL string, cfg *Config) error { group.Go(dl(ani, m)) } - if _, err := p.Run(); err != nil { - log.Errorf("Start progress bar %v", err) - } + go func() { + if err := group.Wait(); err != nil { + p.Send(progressbar.ProgressErrMsg{Err: err}) + return + } + p.Send(progressbar.ProgressSuccessMsg{}) + }() + + _, err = p.Run() - return group.Wait() //nolint:wrapcheck + return err //nolint:wrapcheck } func init() { diff --git a/internal/tui/progressbar/update.go b/internal/tui/progressbar/update.go index d9b5dd6..9a178f1 100644 --- a/internal/tui/progressbar/update.go +++ b/internal/tui/progressbar/update.go @@ -15,6 +15,8 @@ type ProgressMsg struct { Ratio float64 } +type ProgressSuccessMsg struct{} + type ProgressErrMsg struct{ Err error } type ProgressStatusMsg struct { @@ -38,6 +40,9 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { //nolint:cyclop m.width = w return m, nil + case ProgressSuccessMsg: + return m, tea.Quit + case ProgressErrMsg: m.err = msg.Err return m, tea.Quit