Skip to content

Commit e045274

Browse files
committed
setup tcell
1 parent 29c61ba commit e045274

File tree

3 files changed

+78
-2
lines changed

3 files changed

+78
-2
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/briandowns/spinner v1.11.1
99
github.com/charmbracelet/glamour v0.2.1-0.20200724174618-1246d13c1684
1010
github.com/enescakir/emoji v1.0.0
11+
github.com/gdamore/tcell/v2 v2.0.0
1112
github.com/google/go-cmp v0.5.2
1213
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
1314
github.com/hashicorp/go-version v1.2.1

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ github.com/enescakir/emoji v1.0.0/go.mod h1:Bt1EKuLnKDTYpLALApstIkAjdDrS/8IAgTkK
6868
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
6969
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
7070
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
71+
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
72+
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
73+
github.com/gdamore/tcell v1.4.0 h1:vUnHwJRvcPQa3tzi+0QI4U9JINXYJlOz9yiaiPQ2wMU=
74+
github.com/gdamore/tcell/v2 v2.0.0 h1:GRWG8aLfWAlekj9Q6W29bVvkHENc6hp79XOqG4AWDOs=
75+
github.com/gdamore/tcell/v2 v2.0.0/go.mod h1:vSVL/GV5mCSlPC6thFP5kfOFdM9MGZcalipmpTxTgQA=
7176
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
7277
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
7378
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
@@ -341,6 +346,7 @@ golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7w
341346
golang.org/x/sys v0.0.0-20190530182044-ad28b68e88f1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
342347
golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
343348
golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
349+
golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
344350
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
345351
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8=
346352
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

pkg/cmd/protip/protip.go

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package protip
22

33
import (
4-
"fmt"
4+
"time"
55

66
"github.com/cli/cli/pkg/cmdutil"
77
"github.com/cli/cli/pkg/iostreams"
8+
"github.com/gdamore/tcell/v2"
9+
"github.com/mattn/go-runewidth"
810
"github.com/spf13/cobra"
911
)
1012

@@ -37,6 +39,73 @@ func NewCmdProtip(f *cmdutil.Factory, runF func(*ProtipOptions) error) *cobra.Co
3739
}
3840

3941
func protipRun(opts *ProtipOptions) error {
40-
fmt.Println("hey")
42+
tcell.SetEncodingFallback(tcell.EncodingFallbackASCII)
43+
44+
s, err := tcell.NewScreen()
45+
if err != nil {
46+
return err
47+
}
48+
if err = s.Init(); err != nil {
49+
return err
50+
}
51+
52+
s.SetStyle(tcell.StyleDefault.
53+
Foreground(tcell.ColorGreen).
54+
Background(tcell.ColorBlack))
55+
s.Clear()
56+
57+
quit := make(chan struct{})
58+
go func() {
59+
for {
60+
ev := s.PollEvent()
61+
switch ev := ev.(type) {
62+
case *tcell.EventKey:
63+
switch ev.Rune() {
64+
case 'q':
65+
close(quit)
66+
return
67+
}
68+
switch ev.Key() {
69+
case tcell.KeyEscape, tcell.KeyEnter:
70+
close(quit)
71+
return
72+
case tcell.KeyCtrlL:
73+
s.Sync()
74+
}
75+
case *tcell.EventResize:
76+
s.Sync()
77+
}
78+
}
79+
}()
80+
81+
loop:
82+
for {
83+
select {
84+
case <-quit:
85+
break loop
86+
case <-time.After(time.Millisecond * 50):
87+
}
88+
w, h := s.Size()
89+
s.Clear()
90+
emitStr(s, w/2-7, h/2, tcell.StyleDefault, "TODO a protip and stuff")
91+
s.Show()
92+
}
93+
94+
s.Fini()
95+
4196
return nil
4297
}
98+
99+
func emitStr(s tcell.Screen, x, y int, style tcell.Style, str string) {
100+
for _, c := range str {
101+
var comb []rune
102+
w := runewidth.RuneWidth(c)
103+
if w == 0 {
104+
comb = []rune{c}
105+
c = ' '
106+
w = 1
107+
}
108+
s.SetContent(x, y, c, comb, style)
109+
x += w
110+
}
111+
}

0 commit comments

Comments
 (0)