2024-03-16 21:17:40 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2024-03-17 17:59:42 -05:00
|
|
|
"github.com/charmbracelet/bubbles/table"
|
2024-03-16 21:17:40 -05:00
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
"github.com/charmbracelet/lipgloss"
|
|
|
|
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
|
|
)
|
|
|
|
|
2024-03-17 22:06:31 -05:00
|
|
|
// getTimeLogs gets every single time log possible.
|
|
|
|
func getTimeLogs(since time.Time, on_process_repo func(repo gitea.Repository, took time.Duration)) []gitea.TrackedTime {
|
|
|
|
var times []gitea.TrackedTime
|
|
|
|
for _, client := range Clients {
|
|
|
|
page := 1
|
|
|
|
user, _, err := client.GetMyUserInfo()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
repos, _, err := client.ListMyRepos(
|
|
|
|
gitea.ListReposOptions{
|
|
|
|
ListOptions: gitea.ListOptions{
|
|
|
|
Page: page,
|
|
|
|
PageSize: 10,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
} else if len(repos) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
for _, repo := range repos {
|
|
|
|
if repo.Fork {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
duration_start := time.Now()
|
|
|
|
repo_times, _, _ := client.ListRepoTrackedTimes(
|
|
|
|
repo.Owner.UserName,
|
|
|
|
repo.Name,
|
|
|
|
gitea.ListTrackedTimesOptions{
|
|
|
|
User: user.UserName,
|
|
|
|
Since: since,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
for _, t := range repo_times {
|
|
|
|
times = append(times, *t)
|
|
|
|
}
|
|
|
|
on_process_repo(*repo, time.Now().Sub(duration_start))
|
|
|
|
}
|
|
|
|
page++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return times
|
|
|
|
}
|
|
|
|
|
2024-03-17 21:42:40 -05:00
|
|
|
var textStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#5b6078"))
|
2024-03-16 21:17:40 -05:00
|
|
|
|
|
|
|
type timesviewer struct {
|
2024-03-17 21:42:40 -05:00
|
|
|
table table.Model
|
|
|
|
length int
|
|
|
|
total_time time.Duration
|
2024-03-16 21:17:40 -05:00
|
|
|
}
|
2024-03-17 17:59:42 -05:00
|
|
|
|
2024-03-16 21:17:40 -05:00
|
|
|
func (m timesviewer) Init() tea.Cmd {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (m timesviewer) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
|
|
var cmd tea.Cmd
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
case tea.KeyMsg:
|
|
|
|
switch msg.String() {
|
|
|
|
case "esc":
|
|
|
|
if m.table.Focused() {
|
|
|
|
m.table.Blur()
|
|
|
|
} else {
|
|
|
|
m.table.Focus()
|
|
|
|
}
|
|
|
|
case "q", "ctrl+c":
|
|
|
|
return m, tea.Quit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m.table, cmd = m.table.Update(msg)
|
|
|
|
return m, cmd
|
|
|
|
}
|
|
|
|
func (m timesviewer) View() string {
|
2024-03-17 21:42:40 -05:00
|
|
|
var totalTextStyle = lipgloss.NewStyle().
|
|
|
|
Foreground(lipgloss.Color("#6e738d")).
|
|
|
|
Bold(true)
|
|
|
|
return m.table.View() +
|
|
|
|
textStyle.Render("\nUse Up and Down arrows to navigate") +
|
|
|
|
totalTextStyle.Render(fmt.Sprintf("\nTotal - %s\n", m.total_time.String()))
|
2024-03-16 21:17:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func times() {
|
|
|
|
p := tea.NewProgram(initialIndicator())
|
|
|
|
go func() {
|
|
|
|
if _, err := p.Run(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
since := time.Now().AddDate(0, 0, -7)
|
2024-03-17 22:06:31 -05:00
|
|
|
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,
|
|
|
|
})
|
|
|
|
})
|
2024-03-17 19:09:13 -05:00
|
|
|
p.Send(IndicatorInfo{
|
|
|
|
info: "Done!",
|
|
|
|
quitting: true,
|
|
|
|
})
|
2024-03-16 21:17:40 -05:00
|
|
|
p.Quit()
|
|
|
|
|
2024-03-17 21:42:40 -05:00
|
|
|
var total_time time.Duration
|
|
|
|
|
2024-03-16 21:17:40 -05:00
|
|
|
columns := []table.Column{
|
|
|
|
{Title: "User", Width: 10},
|
|
|
|
{Title: "Time", Width: 8},
|
|
|
|
{Title: "Created at", Width: 15},
|
|
|
|
}
|
|
|
|
rows := []table.Row{}
|
|
|
|
for _, t := range times {
|
2024-03-17 17:59:42 -05:00
|
|
|
dur, err := time.ParseDuration(fmt.Sprint(t.Time) + "s")
|
2024-03-16 21:17:40 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
rows = append(rows, table.Row{
|
|
|
|
t.UserName,
|
|
|
|
dur.String(),
|
|
|
|
t.Created.String(),
|
|
|
|
})
|
2024-03-17 21:42:40 -05:00
|
|
|
total_time += dur
|
|
|
|
}
|
|
|
|
tv := timesviewer{
|
|
|
|
total_time: total_time,
|
|
|
|
length: 50,
|
2024-03-16 21:17:40 -05:00
|
|
|
}
|
|
|
|
tab := table.New(
|
|
|
|
table.WithColumns(columns),
|
|
|
|
table.WithRows(rows),
|
|
|
|
table.WithFocused(true),
|
|
|
|
table.WithHeight(10),
|
2024-03-17 21:42:40 -05:00
|
|
|
table.WithWidth(tv.length),
|
2024-03-16 21:17:40 -05:00
|
|
|
)
|
|
|
|
s := table.DefaultStyles()
|
|
|
|
s.Header = s.Header.
|
2024-03-17 21:42:40 -05:00
|
|
|
Foreground(lipgloss.Color("#6e738d")).
|
2024-03-16 21:17:40 -05:00
|
|
|
BorderStyle(lipgloss.DoubleBorder()).
|
2024-03-17 21:42:40 -05:00
|
|
|
BorderForeground(lipgloss.Color("#a6da95")).
|
2024-03-16 21:17:40 -05:00
|
|
|
BorderBottom(true).
|
|
|
|
Bold(false)
|
|
|
|
s.Selected = s.Selected.
|
2024-03-17 21:42:40 -05:00
|
|
|
Foreground(lipgloss.Color("#494d64")).
|
|
|
|
Background(lipgloss.Color("#a6da95")).
|
2024-03-16 21:17:40 -05:00
|
|
|
Bold(false)
|
|
|
|
tab.SetStyles(s)
|
2024-03-17 21:42:40 -05:00
|
|
|
tv.table = tab
|
2024-03-16 21:17:40 -05:00
|
|
|
|
|
|
|
if _, err := tea.NewProgram(tv).Run(); err != nil {
|
|
|
|
fmt.Println("Error running program:", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|