package main import ( "fmt" "time" "code.gitea.io/sdk/gitea" tea "github.com/charmbracelet/bubbletea" ) type summaryviewer struct { times []gitea.TrackedTime total_time time.Duration quitting bool } func (m summaryviewer) Init() tea.Cmd { return nil } func (m summaryviewer) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { case tea.KeyMsg: switch msg.String() { case "q", "ctrl+c": return m, tea.Quit default: return m, tea.Printf("You pressed: %s", msg.String()) } } return m, nil } func (m summaryviewer) View() string { s := "" if !m.quitting { s += "Showing statistics for user " return s } return s } func summary() { p := tea.NewProgram(initialIndicator(("Fetching time logs..."))) go func() { if _, err := p.Run(); err != nil { panic("An error occured.") } }() viewer := summaryviewer{} since := time.Now().AddDate(0, 0, -14) viewer.times = getTimeLogs(since, func(repo gitea.Repository, took time.Duration) { p.Send(IndicatorInfo{ info: fmt.Sprintf("%s / %s", repo.Owner.UserName, repo.Name), duration: took, }) }) for _, t := range viewer.times { viewer.total_time += time.Duration(t.Time * int64(time.Second)) } p.Send(IndicatorInfo{info: "Done", quitting: true}) p.RestoreTerminal() p.Quit() program := tea.NewProgram(viewer) if _, err := program.Run(); err != nil { panic("An error occurred.") } }