From 3a0ff257f7537c88b91d42d663544973f49ce3fc Mon Sep 17 00:00:00 2001 From: Noah Date: Thu, 21 Mar 2024 18:40:46 -0500 Subject: [PATCH] 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. --- times.go | 150 +++++++++++++++++++++++++++---------------------------- 1 file changed, 75 insertions(+), 75 deletions(-) diff --git a/times.go b/times.go index 7df0ddf..d3dd419 100644 --- a/times.go +++ b/times.go @@ -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) - } -}