refactor(times): Move times() to the top of times.go

I've seen a lot of examples / test code for Bubble Tea have the main function at the top of their code, and the Update / View / Init / Model functions and structs defined later.

This way, I can edit easily edit the more important times() function.
This commit is contained in:
Noah 2024-03-21 18:40:46 -05:00
parent d63adde44b
commit 3a0ff257f7

150
times.go
View File

@ -13,6 +13,81 @@ import (
"code.gitea.io/sdk/gitea"
)
func times() {
p := tea.NewProgram(initialIndicator("Fetching time logs..."))
go func() {
if _, err := p.Run(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}()
since := time.Now().AddDate(0, -6, 0)
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,
})
})
p.Send(IndicatorInfo{
info: "Done!",
quitting: true,
})
p.Quit()
var total_time time.Duration
columns := []table.Column{
{Title: "Server", Width: 10},
{Title: "User", Width: 8},
{Title: "Time", Width: 8},
{Title: "Created at", Width: 11},
}
rows := []table.Row{}
for _, t := range times {
dur, err := time.ParseDuration(fmt.Sprint(t.Time) + "s")
if err != nil {
panic(err)
}
rows = append(rows, table.Row{
t.server.servername,
t.UserName,
dur.String(),
t.Created.Format(time.DateOnly),
})
total_time += dur
}
tv := timesviewer{
total_time: total_time,
length: 50,
}
tab := table.New(
table.WithColumns(columns),
table.WithRows(rows),
table.WithFocused(true),
table.WithHeight(10),
table.WithWidth(tv.length),
)
s := table.DefaultStyles()
s.Header = s.Header.
Foreground(colors.overlay[0]).
BorderStyle(lipgloss.DoubleBorder()).
BorderForeground(colors.green).
BorderBottom(true).
Bold(false)
s.Selected = s.Selected.
Foreground(colors.surface[1]).
Background(colors.green).
Bold(false)
tab.SetStyles(s)
tv.table = tab
if _, err := tea.NewProgram(tv).Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
}
// TrackedTime is an extended gitea.TrackedTime struct
type TrackedTime struct {
gitea.TrackedTime
@ -105,78 +180,3 @@ func (m timesviewer) View() string {
styles.text.Render("\nUse Up and Down arrows to navigate") +
totalTextStyle.Render(fmt.Sprintf("\nTotal - %s\n", m.total_time.String()))
}
func times() {
p := tea.NewProgram(initialIndicator("Fetching time logs..."))
go func() {
if _, err := p.Run(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}()
since := time.Now().AddDate(0, -6, 0)
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,
})
})
p.Send(IndicatorInfo{
info: "Done!",
quitting: true,
})
p.Quit()
var total_time time.Duration
columns := []table.Column{
{Title: "Server", Width: 10},
{Title: "User", Width: 8},
{Title: "Time", Width: 8},
{Title: "Created at", Width: 11},
}
rows := []table.Row{}
for _, t := range times {
dur, err := time.ParseDuration(fmt.Sprint(t.Time) + "s")
if err != nil {
panic(err)
}
rows = append(rows, table.Row{
t.server.servername,
t.UserName,
dur.String(),
t.Created.Format(time.DateOnly),
})
total_time += dur
}
tv := timesviewer{
total_time: total_time,
length: 50,
}
tab := table.New(
table.WithColumns(columns),
table.WithRows(rows),
table.WithFocused(true),
table.WithHeight(10),
table.WithWidth(tv.length),
)
s := table.DefaultStyles()
s.Header = s.Header.
Foreground(colors.overlay[0]).
BorderStyle(lipgloss.DoubleBorder()).
BorderForeground(colors.green).
BorderBottom(true).
Bold(false)
s.Selected = s.Selected.
Foreground(colors.surface[1]).
Background(colors.green).
Bold(false)
tab.SetStyles(s)
tv.table = tab
if _, err := tea.NewProgram(tv).Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
}