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)
This commit is contained in:
Noah 2024-03-21 18:51:28 -05:00
parent 3a0ff257f7
commit 2a8f2e46d3
2 changed files with 14 additions and 1 deletions

View File

@ -44,6 +44,14 @@ func (m Indicator) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case error: case error:
m.err = msg m.err = msg
return m, nil 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: case IndicatorInfo:
m.info = msg m.info = msg
m.quitting = msg.quitting m.quitting = msg.quitting

View File

@ -14,15 +14,19 @@ import (
) )
func times() { func times() {
finished := false
p := tea.NewProgram(initialIndicator("Fetching time logs...")) p := tea.NewProgram(initialIndicator("Fetching time logs..."))
go func() { go func() {
if _, err := p.Run(); err != nil { if _, err := p.Run(); err != nil {
fmt.Println(err) 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) 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) { times := getTimeLogs(since, func(repo gitea.Repository, took time.Duration) {
p.Send(IndicatorInfo{ p.Send(IndicatorInfo{
info: fmt.Sprintf("%s / %s", repo.Owner.UserName, repo.Name), info: fmt.Sprintf("%s / %s", repo.Owner.UserName, repo.Name),
@ -33,6 +37,7 @@ func times() {
info: "Done!", info: "Done!",
quitting: true, quitting: true,
}) })
finished = true
p.Quit() p.Quit()
var total_time time.Duration var total_time time.Duration