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.
This commit is contained in:
Noah 2024-03-17 19:09:13 -05:00
parent 5b445eb29a
commit 677047a7c5

View File

@ -3,6 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"os" "os"
"strings"
"time" "time"
"github.com/charmbracelet/bubbles/spinner" "github.com/charmbracelet/bubbles/spinner"
@ -13,7 +14,7 @@ import (
"code.gitea.io/sdk/gitea" "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 { type timesviewer struct {
table table.Model table table.Model
@ -41,40 +42,62 @@ func (m timesviewer) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, cmd return m, cmd
} }
func (m timesviewer) View() string { 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 spinner spinner.Model
quitting bool quitting bool
err error 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.New()
s.Spinner = spinner.Dot s.Spinner = spinner.Dot
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("205")) 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 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) { switch msg := msg.(type) {
case error: case error:
m.err = msg m.err = msg
return m, nil return m, nil
case IndicatorInfo:
m.info = msg
m.quitting = msg.quitting
return m, nil
default: default:
var cmd tea.Cmd var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg) m.spinner, cmd = m.spinner.Update(msg)
return m, cmd return m, cmd
} }
} }
func (m indicator) View() string { func (m Indicator) View() string {
if m.err != nil { if m.err != nil {
return m.err.Error() 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 return str
} }
@ -106,6 +129,7 @@ func times() {
if repo.Fork { if repo.Fork {
continue continue
} }
duration_start := time.Now()
repo_times, _, _ := client.ListRepoTrackedTimes( repo_times, _, _ := client.ListRepoTrackedTimes(
repo.Owner.UserName, repo.Owner.UserName,
repo.Name, repo.Name,
@ -117,10 +141,18 @@ func times() {
for _, t := range repo_times { for _, t := range repo_times {
times = append(times, *t) times = append(times, *t)
} }
p.Send(IndicatorInfo{
info: fmt.Sprintf("%s / %s", repo.Owner.UserName, repo.Name),
duration: time.Now().Sub(duration_start),
})
} }
page++ page++
} }
} }
p.Send(IndicatorInfo{
info: "Done!",
quitting: true,
})
p.Quit() p.Quit()
columns := []table.Column{ columns := []table.Column{