From 2a8f2e46d3aeda0e1cd09971bede7417a961bb57 Mon Sep 17 00:00:00 2001 From: Noah Date: Thu, 21 Mar 2024 18:51:28 -0500 Subject: [PATCH] feat(times): Allow user to quit out of an Indicator Let user press "CTRL+C" or "q" to quit out of the program while fetching time logs (fix #12) --- indicator.go | 8 ++++++++ times.go | 7 ++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/indicator.go b/indicator.go index 0d2b9cb..f286002 100644 --- a/indicator.go +++ b/indicator.go @@ -44,6 +44,14 @@ func (m Indicator) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case error: m.err = msg return m, nil + case tea.KeyMsg: + switch msg.String() { + case "ctrl+c", "q": + m.quitting = true + return m, tea.Quit + default: + return m, nil + } case IndicatorInfo: m.info = msg m.quitting = msg.quitting diff --git a/times.go b/times.go index d3dd419..362da7a 100644 --- a/times.go +++ b/times.go @@ -14,15 +14,19 @@ import ( ) func times() { + finished := false p := tea.NewProgram(initialIndicator("Fetching time logs...")) go func() { if _, err := p.Run(); err != nil { fmt.Println(err) + } + // If the indicator exited before we got all the time logs, it was either an error or the user press CTRL + C / q + if !finished { os.Exit(1) } }() - since := time.Now().AddDate(0, -6, 0) + since := time.Now().AddDate(0, -1, 0) times := getTimeLogs(since, func(repo gitea.Repository, took time.Duration) { p.Send(IndicatorInfo{ info: fmt.Sprintf("%s / %s", repo.Owner.UserName, repo.Name), @@ -33,6 +37,7 @@ func times() { info: "Done!", quitting: true, }) + finished = true p.Quit() var total_time time.Duration