From 677047a7c5f1d89c8e13a896f25f6c5d04be090e Mon Sep 17 00:00:00 2001 From: Noah Date: Sun, 17 Mar 2024 19:09:13 -0500 Subject: [PATCH] feat(times): Add information text to the loading indicator Show some information about the repositories we're getting the time logs from while the user waits. --- times.go | 50 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/times.go b/times.go index 1d4637a..d01e32b 100644 --- a/times.go +++ b/times.go @@ -3,6 +3,7 @@ package main import ( "fmt" "os" + "strings" "time" "github.com/charmbracelet/bubbles/spinner" @@ -13,7 +14,7 @@ import ( "code.gitea.io/sdk/gitea" ) -var helpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#626262")).Render +var textStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#626262")) type timesviewer struct { table table.Model @@ -41,40 +42,62 @@ func (m timesviewer) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return m, cmd } func (m timesviewer) View() string { - return m.table.View() + "\n" + return m.table.View() + "\n" + textStyle.Render("\n Use Up and Down arrows to navigate\n") } -type indicator struct { +type Indicator struct { spinner spinner.Model quitting bool err error + info IndicatorInfo } -func initialIndicator() indicator { +type IndicatorInfo struct { + duration time.Duration + quitting bool + info string +} + +func (i IndicatorInfo) String() string { + if i.duration == 0 { + return textStyle.Render(strings.Repeat(".", 30)) + } + return fmt.Sprintf("%s %s", textStyle.Render(i.duration.String()), i.info) +} + +func initialIndicator() Indicator { s := spinner.New() s.Spinner = spinner.Dot s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("205")) - return indicator{spinner: s} + return Indicator{spinner: s} } -func (m indicator) Init() tea.Cmd { +func (m Indicator) Init() tea.Cmd { return m.spinner.Tick } -func (m indicator) Update(msg tea.Msg) (tea.Model, tea.Cmd) { +func (m Indicator) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { case error: m.err = msg return m, nil + case IndicatorInfo: + m.info = msg + m.quitting = msg.quitting + return m, nil default: var cmd tea.Cmd m.spinner, cmd = m.spinner.Update(msg) return m, cmd } } -func (m indicator) View() string { +func (m Indicator) View() string { if m.err != nil { return m.err.Error() } - str := fmt.Sprintf("\n %s Loading time logs...", m.spinner.View()) + str := "" + if !m.quitting { + // str += fmt.Sprintf("\n %s Loading time logs...\n", m.spinner.View()) + str += fmt.Sprintf("\n %s Loading time logs...\n %s\n", m.spinner.View(), m.info.String()) + } return str } @@ -106,6 +129,7 @@ func times() { if repo.Fork { continue } + duration_start := time.Now() repo_times, _, _ := client.ListRepoTrackedTimes( repo.Owner.UserName, repo.Name, @@ -117,10 +141,18 @@ func times() { for _, t := range repo_times { times = append(times, *t) } + p.Send(IndicatorInfo{ + info: fmt.Sprintf("%s / %s", repo.Owner.UserName, repo.Name), + duration: time.Now().Sub(duration_start), + }) } page++ } } + p.Send(IndicatorInfo{ + info: "Done!", + quitting: true, + }) p.Quit() columns := []table.Column{