Skip to content

Commit

Permalink
Merge pull request #6 from koki-develop/animation
Browse files Browse the repository at this point in the history
  • Loading branch information
koki-develop authored Apr 30, 2023
2 parents 9a4d8d5 + 3c4724b commit 5732601
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 37 deletions.
27 changes: 19 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ Usage:

Flags:
-u, --user string github username
--from string only contributions made at this time or later will be counted
--to string only contributions made before and up to (including) this time will be counted
-t, --theme string grass theme (dark|light) (default "dark")
-g, --grass string grass string (default "■")
-a, --animate animate grass
--total print total contributions
-h, --help help for gh
```
Expand Down Expand Up @@ -82,14 +85,6 @@ $ gh grass --theme light

![](./assets/theme.gif)

### Print Total Contributions

```sh
$ gh grass --total
```

![](./assets/total.gif)

### Custom Grass

```sh
Expand All @@ -100,6 +95,22 @@ $ gh grass --grass "●"

![](./assets/grass.gif)

### Animation

```sh
$ gh grass --animate
```

![](./assets/animate.gif)

### Print Total Contributions

```sh
$ gh grass --total
```

![](./assets/total.gif)

## LICENSE

[MIT](./LICENSE)
Binary file added assets/animate.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 11 additions & 8 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ import (
)

var (
flagUser string
flagFrom string
flagTo string
flagTheme string
flagGrass string
flagTotal bool
flagUser string
flagFrom string
flagTo string
flagTheme string
flagGrass string
flagAnimate bool
flagTotal bool
)

var rootCmd = &cobra.Command{
Expand Down Expand Up @@ -74,8 +75,9 @@ var rootCmd = &cobra.Command{
}

p := printer.New(&printer.Config{
Theme: t,
Grass: flagGrass,
Theme: t,
Grass: flagGrass,
Animate: flagAnimate,
})
if err := p.Print(os.Stdout, cal); err != nil {
return err
Expand All @@ -101,5 +103,6 @@ func init() {

rootCmd.Flags().StringVarP(&flagTheme, "theme", "t", "dark", fmt.Sprintf("grass theme (%s)", strings.Join(printer.ListThemes(), "|")))
rootCmd.Flags().StringVarP(&flagGrass, "grass", "g", "■", "grass string")
rootCmd.Flags().BoolVarP(&flagAnimate, "animate", "a", false, "animate grass")
rootCmd.Flags().BoolVar(&flagTotal, "total", false, "print total contributions")
}
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,35 @@ module github.com/koki-develop/gh-grass
go 1.20

require (
github.com/charmbracelet/bubbletea v0.23.2
github.com/charmbracelet/lipgloss v0.7.1
github.com/cli/go-gh v1.2.1
github.com/cli/shurcooL-graphql v0.0.3
github.com/jinzhu/now v1.1.5
github.com/mattn/go-runewidth v0.0.14
github.com/spf13/cobra v1.7.0
golang.org/x/text v0.9.0
)

require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/cli/safeexec v1.0.0 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/henvic/httpretty v0.0.6 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.1 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/thlib/go-timezone-local v0.0.0-20210907160436-ef149e42d28e // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/term v0.5.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
17 changes: 17 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ=
github.com/aymanbagabas/go-osc52 v1.2.1/go.mod h1:zT8H+Rk4VSabYN90pWyugflM3ZhpTZNC7cASDfUCdT4=
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/charmbracelet/bubbletea v0.23.2 h1:vuUJ9HJ7b/COy4I30e8xDVQ+VRDUEFykIjryPfgsdps=
github.com/charmbracelet/bubbletea v0.23.2/go.mod h1:FaP3WUivcTM0xOKNmhciz60M6I+weYLF76mr1JyI7sM=
github.com/charmbracelet/lipgloss v0.7.1 h1:17WMwi7N1b1rVWOjMT+rCh7sQkvDU75B2hbZpc5Kc1E=
github.com/charmbracelet/lipgloss v0.7.1/go.mod h1:yG0k3giv8Qj8edTCbbg6AlQ5e8KNWpFujkNawKNhE2c=
github.com/cli/go-gh v1.2.1 h1:xFrjejSsgPiwXFP6VYynKWwxLQcNJy3Twbu82ZDlR/o=
Expand All @@ -9,6 +12,8 @@ github.com/cli/safeexec v1.0.0 h1:0VngyaIyqACHdcMNWfo6+KdUYnqEr2Sg+bSP1pdF+dI=
github.com/cli/safeexec v1.0.0/go.mod h1:Z/D4tTN8Vs5gXYHDCbaM1S/anmEDnJb1iW0+EJ5zx3Q=
github.com/cli/shurcooL-graphql v0.0.3 h1:CtpPxyGDs136/+ZeyAfUKYmcQBjDlq5aqnrDCW5Ghh8=
github.com/cli/shurcooL-graphql v0.0.3/go.mod h1:tlrLmw/n5Q/+4qSvosT+9/W5zc8ZMjnJeYBxSdb4nWA=
github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=
github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand All @@ -26,11 +31,19 @@ github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=
github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b h1:1XF24mVaiu7u+CFywTdcDo2ie1pzzhwjt6RHqzpMU34=
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho=
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=
github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
github.com/muesli/termenv v0.14.0/go.mod h1:kG/pF1E7fh949Xhe156crRUrHNyK221IuGO7Ez60Uc8=
github.com/muesli/termenv v0.15.1 h1:UzuTb/+hhlBugQz28rpzey4ZuKcZ03MeKsoG7IJZIxs=
github.com/muesli/termenv v0.15.1/go.mod h1:HeAQPTzpfs016yGtA4g00CsdYnVLJvxsS4ANqrZs2sQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -48,8 +61,12 @@ github.com/thlib/go-timezone-local v0.0.0-20210907160436-ef149e42d28e/go.mod h1:
golang.org/x/net v0.0.0-20220923203811-8be639271d50/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210831042530-f4d43177bf5e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
Expand Down
64 changes: 64 additions & 0 deletions internal/printer/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package printer

import (
"time"

tea "github.com/charmbracelet/bubbletea"
"github.com/mattn/go-runewidth"
)

var (
_ tea.Model = (*model)(nil)
)

type model struct {
printer *Printer

index int
grasses []string
}

func newModel(p *Printer, grasses []string) *model {
return &model{
printer: p,

index: 0,
grasses: grasses,
}
}

func (m *model) Init() tea.Cmd {
runewidth.DefaultCondition.EastAsianWidth = false

return m.tick()
}

func (m *model) View() string {
return m.printer.grasses(m.grasses, m.index)
}

type tickMsg struct{}

func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyCtrlC:
return m, tea.Quit
}
case tickMsg:
m.index++
if m.index == len(m.grasses) {
return m, tea.Quit
}
return m, m.tick()
}

return m, nil
}

func (m *model) tick() tea.Cmd {
return tea.Tick(10*time.Millisecond, func(time.Time) tea.Msg {
return tickMsg{}
})
}
67 changes: 47 additions & 20 deletions internal/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,31 @@ package printer
import (
"fmt"
"io"
"os"
"strings"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/koki-develop/gh-grass/internal/github"
)

type Printer struct {
theme Theme
grass string
theme Theme
grass string
animate bool
}

type Config struct {
Theme Theme
Grass string
Theme Theme
Grass string
Animate bool
}

func New(cfg *Config) *Printer {
return &Printer{
theme: cfg.Theme,
grass: cfg.Grass,
theme: cfg.Theme,
grass: cfg.Grass,
animate: cfg.Animate,
}
}

Expand All @@ -42,25 +48,46 @@ func (p *Printer) Print(w io.Writer, calendar github.Calendar) error {
}
}

if p.animate {
m := newModel(p, grasses)
p := tea.NewProgram(m, tea.WithOutput(os.Stderr))
if _, err := p.Run(); err != nil {
return err
}
} else {
if err := p.print(w, grasses); err != nil {
return err
}
}

return nil
}

func (p *Printer) print(w io.Writer, grasses []string) error {
if _, err := fmt.Fprintln(w, p.grasses(grasses, len(grasses))); err != nil {
return err
}
return nil
}

func (p *Printer) grasses(grasses []string, to int) string {
var v strings.Builder

rows := 7
columns := (len(grasses) + rows - 1) / rows

for i := 0; i < rows; i++ {
for j := 0; j < columns; j++ {
index := j*rows + i
if index < len(grasses) {
if j != 0 {
fmt.Fprint(w, " ")
columns := (to + rows - 1) / rows
for r := 0; r < rows; r++ {
for c := 0; c < columns; c++ {
pos := c*rows + r
if pos < to {
if c > 0 {
v.WriteRune(' ')
}
fmt.Fprint(w, grasses[index])
v.WriteString(grasses[pos])
}
}
if i < rows-1 && i < len(grasses)-1 {
fmt.Fprintln(w)
}
v.WriteRune('\n')
}

fmt.Fprintln(w)

return nil
return v.String()
}
23 changes: 23 additions & 0 deletions tapes/animate.tape
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Output assets/animate.gif

Require gh

Set Shell "bash"
Set FontSize 32
Set Width 2200
Set Height 600
Set PlaybackSpeed 0.5

Hide
Type "go build ." Enter
Type "gh extension remove grass" Enter
Type "gh extension install ." Enter
Type "gh grass --help" Enter
Ctrl+l
Sleep 3s
Show

Type "gh grass --animate" Sleep 500ms Enter
Sleep 3650ms

Sleep 5s

0 comments on commit 5732601

Please sign in to comment.