Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: show graph before n days #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ $ gh extension install aymanbagabas/gh-stars
## Usage

```bash
$ gh stars # while in a git repository
$ gh stars [GitHub repository] # to view a specific repository
$ gh stars # while in a GitHub repository
$ gh stars [GitHub repository] # to view a specific GitHub repository
```

### Keybindings

* <kbd>tab</kbd> - Switch to table view.
* <kbd>?</kbd> - Show help.
* <kbd>q</kbd> - Quit.
* <kbd>↑↓</kbd> - Navigate table view.
- <kbd>tab</kbd> - Switch between graph &table view.
- <kbd>a</kbd> - toggle all stargazers.
- <kbd>left</kbd> - increase time.
- <kbd>right</kbd> - decrease time.
- <kbd>?</kbd> - Show help.
- <kbd>q</kbd> - Quit.
- <kbd>↑↓</kbd> - Navigate table view.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/charmbracelet/bubbletea v0.23.2
github.com/charmbracelet/lipgloss v0.7.0
github.com/cli/go-gh v1.2.1
github.com/dustin/go-humanize v1.0.1
github.com/guptarohit/asciigraph v0.5.5
github.com/spf13/pflag v1.0.5
golang.org/x/sync v0.1.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARu
github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/guptarohit/asciigraph v0.5.5 h1:ccFnUF8xYIOUPPY3tmdvRyHqmn1MYI9iv1pLKX+/ZkQ=
github.com/guptarohit/asciigraph v0.5.5/go.mod h1:dYl5wwK4gNsnFf9Zp+l06rFiDZ5YtXM6x7SRWZ3KGag=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
Expand Down
73 changes: 62 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/charmbracelet/lipgloss"
"github.com/cli/go-gh"
"github.com/cli/go-gh/pkg/api"
"github.com/dustin/go-humanize"
"github.com/guptarohit/asciigraph"
"github.com/spf13/pflag"
"golang.org/x/sync/errgroup"
Expand All @@ -31,6 +32,10 @@ const (
stargazersPath = "repos/%s/stargazers"
)

const (
defaultTimeFormat = "2006-01-02"
)

type view int

const (
Expand All @@ -54,6 +59,26 @@ type Stargazer struct {

type StargazersMsg map[string]int

func newStargazersMap(s []Stargazer) StargazersMsg {
result := make(map[string]int)
for _, v := range s {
t := v.StarredAt.Format(defaultTimeFormat)
result[t]++
}
return result
}

func (s StargazersMsg) after(t time.Time) map[string]int {
result := make(map[string]int)
for k, v := range s {
kt, _ := time.Parse(defaultTimeFormat, k)
if t.Before(kt) {
result[k] = v
}
}
return result
}

type RepoMsg struct {
StargazersCount int `json:"stargazers_count"`
}
Expand All @@ -73,6 +98,8 @@ type Repo struct {
help help.Model
showHelp bool
mu sync.Mutex
last int
all bool
}

func NewRepo(name string) (*Repo, error) {
Expand Down Expand Up @@ -103,6 +130,7 @@ func NewRepo(name string) (*Repo, error) {
spinner: s,
table: t,
help: h,
last: 30, // default to 30 days
}, nil
}

Expand Down Expand Up @@ -144,13 +172,25 @@ func (r *Repo) GetStargazers() ([]Stargazer, error) {

func (r *Repo) ShortHelp() []key.Binding {
return []key.Binding{
key.NewBinding(
key.WithKeys("a"),
key.WithHelp("a", "toggle all"),
),
key.NewBinding(
key.WithKeys("h", "left"),
key.WithHelp("left", "before"),
),
key.NewBinding(
key.WithKeys("l", "right"),
key.WithHelp("right", "after"),
),
key.NewBinding(
key.WithKeys("tab", "shift+tab"),
key.WithHelp("tab", "section"),
),
key.NewBinding(
key.WithKeys("?"),
key.WithHelp("?", "help"),
key.WithHelp("?", "toggle help"),
),
key.NewBinding(
key.WithKeys("q", "ctrl+c"),
Expand Down Expand Up @@ -199,6 +239,14 @@ func (r *Repo) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
r.table.SetHeight(r.height - 1)
case tea.KeyMsg:
switch msg.String() {
case "a":
r.all = !r.all
case "h", "left":
r.last += 30
case "l", "right":
if r.last > 30 {
r.last -= 30
}
case "q", "ctrl+c":
return r, tea.Quit
case "tab", "shift+tab":
Expand Down Expand Up @@ -228,12 +276,7 @@ func (r *Repo) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if err != nil {
return ErrorMsg(err)
}
stars := make(map[string]int)
for _, s := range stargazers {
t := s.StarredAt.Format("2006-01-02")
stars[t]++
}
return StargazersMsg(stars)
return newStargazersMap(stargazers)
})
}
return r, tea.Batch(cmds...)
Expand All @@ -256,7 +299,11 @@ func (r *Repo) View() string {
)
}
keys := make([]string, 0)
for k := range r.stargazers {
stargazers := r.stargazers
if r.last > 0 && !r.all {
stargazers = StargazersMsg(r.stargazers).after(time.Now().AddDate(0, 0, -r.last))
}
for k := range stargazers {
keys = append(keys, k)
}
sort.Strings(keys)
Expand All @@ -268,18 +315,22 @@ func (r *Repo) View() string {
offset := 3
plot := make([]float64, len(keys))
for i, k := range keys {
o := fmt.Sprintf("%d", r.stargazers[k])
o := fmt.Sprintf("%d", stargazers[k])
if len(o) > offset {
offset = len(o)
}
plot[i] = float64(r.stargazers[k])
plot[i] = float64(stargazers[k])
}
caption := fmt.Sprintf("%s %d stargazers (%s)", r.name, r.stars, humanize.Time(time.Now().AddDate(0, 0, -r.last)))
if r.all {
caption = fmt.Sprintf("%s %d stargazers (since %s)", r.name, r.stars, keys[0])
}
graph := asciigraph.Plot(
plot,
asciigraph.SeriesColors(asciigraph.Blue),
asciigraph.Width(r.width-offset-1),
asciigraph.Height(r.height-2),
asciigraph.Caption(fmt.Sprintf("%s %d stargazers over time", r.name, r.stars)),
asciigraph.Caption(caption),
asciigraph.Precision(0),
asciigraph.Offset(offset),
)
Expand Down