forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.go
More file actions
304 lines (267 loc) · 6.04 KB
/
project.go
File metadata and controls
304 lines (267 loc) · 6.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package project
import (
"net/http"
"strings"
"time"
"github.com/cli/cli/api"
"github.com/cli/cli/internal/ghrepo"
"github.com/cli/cli/pkg/cmdutil"
"github.com/cli/cli/pkg/iostreams"
"github.com/gdamore/tcell/v2"
"github.com/mattn/go-runewidth"
"github.com/spf13/cobra"
)
type ProjectOptions struct {
HttpClient func() (*http.Client, error)
IO *iostreams.IOStreams
BaseRepo func() (ghrepo.Interface, error)
Selector string
}
func NewCmdProject(f *cmdutil.Factory, runF func(*ProjectOptions) error) *cobra.Command {
opts := ProjectOptions{
IO: f.IOStreams,
HttpClient: f.HttpClient,
BaseRepo: f.BaseRepo,
}
cmd := &cobra.Command{
Use: "project [<project id>]",
Short: "Interact with a GitHub project",
Long: "Enter an interactive UI for viewing and modifying a GitHub project",
Hidden: true,
Args: cobra.NoArgs,
RunE: func(c *cobra.Command, args []string) error {
// support `-R, --repo` override
opts.BaseRepo = f.BaseRepo
if runF != nil {
return runF(&opts)
}
return projectRun(&opts)
},
}
return cmd
}
type Content struct {
Title string
Body string
}
type Card struct {
Note string
ID int
ContentURL string `json:"content_url"`
Content *Content
}
type Column struct {
Name string
ID int
Cards []*Card
}
type Project struct {
Name string
ID int
Columns []*Column
}
func projectRun(opts *ProjectOptions) error {
// TODO interactively ask which project they want since these IDs are not easy to get
projectID := 3514315
c, err := opts.HttpClient()
if err != nil {
return err
}
client := api.NewClientFromHTTP(c)
baseRepo, err := opts.BaseRepo()
if err != nil {
return err
}
project, err := getProject(client, baseRepo, projectID)
if err != nil {
return err
}
style := tcell.StyleDefault
s, err := tcell.NewScreen()
if err != nil {
return err
}
if err = s.Init(); err != nil {
return err
}
s.SetStyle(style)
// TODO some kind of controller struct to track modal state?
colWidth := 30
colHeight := 40
colsToShow := 3
cardWidth := colWidth - 2
cardHeight := 5
cardsToShow := 7
selectedColumn := 0
selectedCard := 0
quit := make(chan struct{})
go func() {
for {
ev := s.PollEvent()
switch ev := ev.(type) {
case *tcell.EventKey:
switch ev.Rune() {
case 'q':
close(quit)
return
case 'w':
if selectedCard > 0 {
selectedCard--
}
case 's':
if selectedCard < cardsToShow {
selectedCard++
}
case 'a':
if selectedColumn > 0 {
selectedColumn--
}
case 'd':
if selectedColumn < colsToShow {
selectedColumn++
}
}
switch ev.Key() {
case tcell.KeyEscape:
close(quit)
return
case tcell.KeyCtrlL:
s.Sync()
}
case *tcell.EventResize:
s.Sync()
}
}
}()
var cardInfo *Card
loop:
for {
select {
case <-quit:
break loop
case <-time.After(time.Millisecond * 100):
}
s.Clear()
drawStr(s, 0, 0, style, project.Name)
for ix, col := range project.Columns {
if ix == colsToShow {
break
}
colX := colWidth * ix
colY := 1
drawRect(s, style, colX, colY, colWidth, colHeight, false)
drawStr(s, colX+1, colY+1, style, col.Name)
if selectedColumn == ix && selectedCard >= len(col.Cards) {
selectedCard = len(col.Cards) - 1
}
for ic, card := range col.Cards {
if ic == cardsToShow {
break
}
cardStyle := style
bold := false
if selectedColumn == ix && selectedCard == ic {
cardStyle = style.Foreground(tcell.ColorGreen)
bold = true
cardInfo = card
}
drawRect(s, cardStyle, colX+1, (ic*cardHeight)+colY+2, cardWidth, cardHeight, bold)
cardNote := card.Note
if cardNote == "" {
cardNote = card.Content.Title
}
if len(cardNote) > cardWidth-2 {
cardNote = cardNote[0 : cardWidth-2]
} else if cardNote == "" {
cardNote = `¯\_(ツ)_/¯`
}
drawStr(s, colX+2, (ic*cardHeight)+colY+3, style, cardNote)
}
}
// Info box
infoX := colWidth * colsToShow
infoY := 1
infoWidth := colWidth * 2
infoHeight := colHeight
drawRect(s, style, infoX, infoY, infoWidth, infoHeight, true)
if cardInfo.Content != nil {
title := cardInfo.Content.Title
if len(title) > infoWidth-2 {
title = title[0 : infoWidth-2]
}
drawStr(s, infoX+1, infoY+1, style, title)
// TODO markdown rendering did _not_ output correctly; lots of escaped ANSI
bodyLines := strings.Split(cardInfo.Content.Body, "\n")
for i, line := range bodyLines {
if infoY+2+i > infoHeight-2 {
break
}
outLine := line
if len(outLine) > infoWidth-2 {
outLine = outLine[0 : infoWidth-2]
}
drawStr(s, infoX+1, infoY+2+i, style, outLine)
}
} else {
drawStr(s, infoX+1, infoY+1, style, cardInfo.Note)
}
s.Show()
}
s.Fini()
return nil
}
func drawStr(s tcell.Screen, x, y int, style tcell.Style, str string) {
for _, c := range str {
var comb []rune
w := runewidth.RuneWidth(c)
if w == 0 {
comb = []rune{c}
c = ' '
w = 1
}
s.SetContent(x, y, c, comb, style)
x += w
}
}
func drawRect(s tcell.Screen, style tcell.Style, x, y, w, h int, bold bool) {
// TODO could consolidate these now that I have the correct unicode characters
topLeftCorner := '┌'
botLeftCorner := '└'
topRightCorner := '┐'
botRightCorner := '┘'
vertiLine := '│'
horizLine := '─'
if bold {
topLeftCorner = '┏'
botLeftCorner = '┗'
topRightCorner = '┓'
botRightCorner = '┛'
vertiLine = '┃'
horizLine = '━'
}
for ix := x; ix < x+w; ix++ {
for iy := y; iy < y+h; iy++ {
var c rune
if ix == x && iy == y {
c = topLeftCorner
} else if ix == x && iy == y+h-1 {
c = botLeftCorner
} else if ix == x {
c = vertiLine
} else if ix == x+w-1 && iy == y {
c = topRightCorner
} else if ix == x+w-1 && iy == y+h-1 {
c = botRightCorner
} else if ix == x+w-1 {
c = vertiLine
} else if iy == y {
c = horizLine
} else if iy == y+h-1 {
c = horizLine
} else {
continue
}
s.SetContent(ix, iy, c, nil, style)
}
}
}