162 lines
3.3 KiB
Go
162 lines
3.3 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/charmbracelet/bubbles/table"
|
|
"github.com/charmbracelet/bubbles/spinner"
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
"github.com/charmbracelet/lipgloss"
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
)
|
|
|
|
|
|
var helpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#626262")).Render
|
|
|
|
type timesviewer struct {
|
|
table table.Model
|
|
}
|
|
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 {
|
|
return m.table.View() + "\n"
|
|
}
|
|
|
|
type indicator struct {
|
|
spinner spinner.Model
|
|
quitting bool
|
|
err error
|
|
}
|
|
func initialIndicator() indicator {
|
|
s := spinner.New()
|
|
s.Spinner = spinner.Dot
|
|
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))
|
|
return indicator{spinner: s}
|
|
}
|
|
func (m indicator) Init() tea.Cmd {
|
|
return m.spinner.Tick
|
|
}
|
|
func (m indicator) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
switch msg := msg.(type) {
|
|
case error:
|
|
m.err = msg
|
|
return m, nil
|
|
default:
|
|
var cmd tea.Cmd
|
|
m.spinner, cmd = m.spinner.Update(msg)
|
|
return m, cmd
|
|
}
|
|
}
|
|
func (m indicator) View() string {
|
|
if m.err != nil {
|
|
return m.err.Error()
|
|
}
|
|
str := fmt.Sprintf("\n %s Loading time logs...", m.spinner.View())
|
|
return str
|
|
}
|
|
|
|
func times() {
|
|
p := tea.NewProgram(initialIndicator())
|
|
go func() {
|
|
if _, err := p.Run(); err != nil {
|
|
fmt.Println(err)
|
|
os.Exit(1)
|
|
}
|
|
}()
|
|
|
|
var times []gitea.TrackedTime
|
|
since := time.Now().AddDate(0, 0, -7)
|
|
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
|
|
}
|
|
repo_times, _, err := client.ListRepoTrackedTimes(repo.Owner.UserName, repo.Name, gitea.ListTrackedTimesOptions{User: user.UserName, Since: since})
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
for _, t := range repo_times {
|
|
times = append(times, *t)
|
|
}
|
|
}
|
|
page++
|
|
}
|
|
}
|
|
p.Quit()
|
|
|
|
columns := []table.Column{
|
|
{Title: "User", Width: 10},
|
|
{Title: "Time", Width: 8},
|
|
{Title: "Created at", Width: 15},
|
|
}
|
|
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.UserName,
|
|
dur.String(),
|
|
t.Created.String(),
|
|
})
|
|
}
|
|
tab := table.New(
|
|
table.WithColumns(columns),
|
|
table.WithRows(rows),
|
|
table.WithFocused(true),
|
|
table.WithHeight(10),
|
|
)
|
|
s := table.DefaultStyles()
|
|
s.Header = s.Header.
|
|
BorderStyle(lipgloss.DoubleBorder()).
|
|
BorderForeground(lipgloss.Color("240")).
|
|
BorderBottom(true).
|
|
Bold(false)
|
|
s.Selected = s.Selected.
|
|
Foreground(lipgloss.Color("229")).
|
|
Background(lipgloss.Color("57")).
|
|
Bold(false)
|
|
tab.SetStyles(s)
|
|
|
|
tv := timesviewer{tab}
|
|
if _, err := tea.NewProgram(tv).Run(); err != nil {
|
|
fmt.Println("Error running program:", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|