-
Notifications
You must be signed in to change notification settings - Fork 0
/
garden.go
executable file
·509 lines (438 loc) · 10.9 KB
/
garden.go
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
package garden
import (
"bytes"
"errors"
"fmt"
"math/rand"
"net/http"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
"github.com/ungtb10d/cli/v2/api"
"github.com/ungtb10d/cli/v2/internal/config"
"github.com/ungtb10d/cli/v2/internal/ghrepo"
"github.com/ungtb10d/cli/v2/pkg/cmdutil"
"github.com/ungtb10d/cli/v2/pkg/iostreams"
"github.com/ungtb10d/cli/v2/utils"
"github.com/spf13/cobra"
"golang.org/x/term"
)
type Geometry struct {
Width int
Height int
Density float64
Repository ghrepo.Interface
}
type Player struct {
X int
Y int
Char string
Geo *Geometry
ShoeMoistureContent int
}
type Commit struct {
Email string
Handle string
Sha string
Char string
}
type Cell struct {
Char string
StatusLine string
}
const (
DirUp Direction = iota
DirDown
DirLeft
DirRight
Quit
)
type Direction = int
func (p *Player) move(direction Direction) bool {
switch direction {
case DirUp:
if p.Y == 0 {
return false
}
p.Y--
case DirDown:
if p.Y == p.Geo.Height-1 {
return false
}
p.Y++
case DirLeft:
if p.X == 0 {
return false
}
p.X--
case DirRight:
if p.X == p.Geo.Width-1 {
return false
}
p.X++
}
return true
}
type GardenOptions struct {
HttpClient func() (*http.Client, error)
IO *iostreams.IOStreams
BaseRepo func() (ghrepo.Interface, error)
Config func() (config.Config, error)
RepoArg string
}
func NewCmdGarden(f *cmdutil.Factory, runF func(*GardenOptions) error) *cobra.Command {
opts := GardenOptions{
IO: f.IOStreams,
HttpClient: f.HttpClient,
BaseRepo: f.BaseRepo,
Config: f.Config,
}
cmd := &cobra.Command{
Use: "garden [<repository>]",
Short: "Explore a git repository as a garden",
Long: "Use arrow keys, WASD or vi keys to move. q to quit.",
Hidden: true,
RunE: func(c *cobra.Command, args []string) error {
if len(args) > 0 {
opts.RepoArg = args[0]
}
if runF != nil {
return runF(&opts)
}
return gardenRun(&opts)
},
}
return cmd
}
func gardenRun(opts *GardenOptions) error {
cs := opts.IO.ColorScheme()
out := opts.IO.Out
if runtime.GOOS == "windows" {
return errors.New("sorry :( this command only works on linux and macos")
}
if !opts.IO.IsStdoutTTY() {
return errors.New("must be connected to a terminal")
}
httpClient, err := opts.HttpClient()
if err != nil {
return err
}
var toView ghrepo.Interface
apiClient := api.NewClientFromHTTP(httpClient)
if opts.RepoArg == "" {
var err error
toView, err = opts.BaseRepo()
if err != nil {
return err
}
} else {
var err error
viewURL := opts.RepoArg
if !strings.Contains(viewURL, "/") {
cfg, err := opts.Config()
if err != nil {
return err
}
hostname, _ := cfg.DefaultHost()
currentUser, err := api.CurrentLoginName(apiClient, hostname)
if err != nil {
return err
}
viewURL = currentUser + "/" + viewURL
}
toView, err = ghrepo.FromFullName(viewURL)
if err != nil {
return fmt.Errorf("argument error: %w", err)
}
}
seed := computeSeed(ghrepo.FullName(toView))
rand.Seed(seed)
termWidth, termHeight, err := utils.TerminalSize(out)
if err != nil {
return err
}
termWidth -= 10
termHeight -= 10
geo := &Geometry{
Width: termWidth,
Height: termHeight,
Repository: toView,
// TODO based on number of commits/cells instead of just hardcoding
Density: 0.3,
}
maxCommits := (geo.Width * geo.Height) / 2
opts.IO.StartProgressIndicator()
fmt.Fprintln(out, "gathering commits; this could take a minute...")
commits, err := getCommits(httpClient, toView, maxCommits)
opts.IO.StopProgressIndicator()
if err != nil {
return err
}
player := &Player{0, 0, cs.Bold("@"), geo, 0}
garden := plantGarden(commits, geo)
if len(garden) < geo.Height {
geo.Height = len(garden)
}
if geo.Height > 0 && len(garden[0]) < geo.Width {
geo.Width = len(garden[0])
} else if len(garden) == 0 {
geo.Width = 0
}
clear(opts.IO)
drawGarden(opts.IO, garden, player)
// TODO: use opts.IO instead of os.Stdout
oldTermState, err := term.MakeRaw(int(os.Stdout.Fd()))
if err != nil {
return fmt.Errorf("term.MakeRaw: %w", err)
}
dirc := make(chan Direction)
go func() {
b := make([]byte, 3)
for {
_, _ = opts.IO.In.Read(b)
switch {
case isLeft(b):
dirc <- DirLeft
case isRight(b):
dirc <- DirRight
case isUp(b):
dirc <- DirUp
case isDown(b):
dirc <- DirDown
case isQuit(b):
dirc <- Quit
}
}
}()
mainLoop:
for {
oldX := player.X
oldY := player.Y
d := <-dirc
if d == Quit {
break mainLoop
} else if !player.move(d) {
continue mainLoop
}
underPlayer := garden[player.Y][player.X]
previousCell := garden[oldY][oldX]
// print whatever was just under player
fmt.Fprint(out, "\033[;H") // move to top left
for x := 0; x < oldX && x < player.Geo.Width; x++ {
fmt.Fprint(out, "\033[C")
}
for y := 0; y < oldY && y < player.Geo.Height; y++ {
fmt.Fprint(out, "\033[B")
}
fmt.Fprint(out, previousCell.Char)
// print player character
fmt.Fprint(out, "\033[;H") // move to top left
for x := 0; x < player.X && x < player.Geo.Width; x++ {
fmt.Fprint(out, "\033[C")
}
for y := 0; y < player.Y && y < player.Geo.Height; y++ {
fmt.Fprint(out, "\033[B")
}
fmt.Fprint(out, player.Char)
// handle stream wettening
if strings.Contains(underPlayer.StatusLine, "stream") {
player.ShoeMoistureContent = 5
} else {
if player.ShoeMoistureContent > 0 {
player.ShoeMoistureContent--
}
}
// status line stuff
sl := statusLine(garden, player, opts.IO)
fmt.Fprint(out, "\033[;H") // move to top left
for y := 0; y < player.Geo.Height-1; y++ {
fmt.Fprint(out, "\033[B")
}
fmt.Fprintln(out)
fmt.Fprintln(out)
fmt.Fprint(out, cs.Bold(sl))
}
clear(opts.IO)
fmt.Fprint(out, "\033[?25h")
// TODO: use opts.IO instead of os.Stdout
_ = term.Restore(int(os.Stdout.Fd()), oldTermState)
fmt.Fprintln(out, cs.Bold("You turn and walk away from the wildflower garden..."))
return nil
}
func isLeft(b []byte) bool {
left := []byte{27, 91, 68}
r := rune(b[0])
return bytes.EqualFold(b, left) || r == 'a' || r == 'h'
}
func isRight(b []byte) bool {
right := []byte{27, 91, 67}
r := rune(b[0])
return bytes.EqualFold(b, right) || r == 'd' || r == 'l'
}
func isDown(b []byte) bool {
down := []byte{27, 91, 66}
r := rune(b[0])
return bytes.EqualFold(b, down) || r == 's' || r == 'j'
}
func isUp(b []byte) bool {
up := []byte{27, 91, 65}
r := rune(b[0])
return bytes.EqualFold(b, up) || r == 'w' || r == 'k'
}
var ctrlC = []byte{0x3, 0x5b, 0x43}
func isQuit(b []byte) bool {
return rune(b[0]) == 'q' || bytes.Equal(b, ctrlC)
}
func plantGarden(commits []*Commit, geo *Geometry) [][]*Cell {
cellIx := 0
grassCell := &Cell{RGB(0, 200, 0, ","), "You're standing on a patch of grass in a field of wildflowers."}
garden := [][]*Cell{}
streamIx := rand.Intn(geo.Width - 1)
if streamIx == geo.Width/2 {
streamIx--
}
tint := 0
for y := 0; y < geo.Height; y++ {
if cellIx == len(commits)-1 {
break
}
garden = append(garden, []*Cell{})
for x := 0; x < geo.Width; x++ {
if (y > 0 && (x == 0 || x == geo.Width-1)) || y == geo.Height-1 {
garden[y] = append(garden[y], &Cell{
Char: RGB(0, 150, 0, "^"),
StatusLine: "You're standing under a tall, leafy tree.",
})
continue
}
if x == streamIx {
garden[y] = append(garden[y], &Cell{
Char: RGB(tint, tint, 255, "#"),
StatusLine: "You're standing in a shallow stream. It's refreshing.",
})
tint += 15
streamIx--
if rand.Float64() < 0.5 {
streamIx++
}
if streamIx < 0 {
streamIx = 0
}
if streamIx > geo.Width {
streamIx = geo.Width
}
continue
}
if y == 0 && (x < geo.Width/2 || x > geo.Width/2) {
garden[y] = append(garden[y], &Cell{
Char: RGB(0, 200, 0, ","),
StatusLine: "You're standing by a wildflower garden. There is a light breeze.",
})
continue
} else if y == 0 && x == geo.Width/2 {
garden[y] = append(garden[y], &Cell{
Char: RGB(139, 69, 19, "+"),
StatusLine: fmt.Sprintf("You're standing in front of a weather-beaten sign that says %s.", ghrepo.FullName(geo.Repository)),
})
continue
}
if cellIx == len(commits)-1 {
garden[y] = append(garden[y], grassCell)
continue
}
chance := rand.Float64()
if chance <= geo.Density {
commit := commits[cellIx]
garden[y] = append(garden[y], &Cell{
Char: commits[cellIx].Char,
StatusLine: fmt.Sprintf("You're standing at a flower called %s planted by %s.", commit.Sha[0:6], commit.Handle),
})
cellIx++
} else {
garden[y] = append(garden[y], grassCell)
}
}
}
return garden
}
func drawGarden(io *iostreams.IOStreams, garden [][]*Cell, player *Player) {
out := io.Out
cs := io.ColorScheme()
fmt.Fprint(out, "\033[?25l") // hide cursor. it needs to be restored at command exit.
sl := ""
for y, gardenRow := range garden {
for x, gardenCell := range gardenRow {
char := ""
underPlayer := (player.X == x && player.Y == y)
if underPlayer {
sl = gardenCell.StatusLine
char = cs.Bold(player.Char)
if strings.Contains(gardenCell.StatusLine, "stream") {
player.ShoeMoistureContent = 5
}
} else {
char = gardenCell.Char
}
fmt.Fprint(out, char)
}
fmt.Fprintln(out)
}
fmt.Println()
fmt.Fprintln(out, cs.Bold(sl))
}
func statusLine(garden [][]*Cell, player *Player, io *iostreams.IOStreams) string {
width := io.TerminalWidth()
statusLines := []string{garden[player.Y][player.X].StatusLine}
if player.ShoeMoistureContent > 1 {
statusLines = append(statusLines, "Your shoes squish with water from the stream.")
} else if player.ShoeMoistureContent == 1 {
statusLines = append(statusLines, "Your shoes seem to have dried out.")
} else {
statusLines = append(statusLines, "")
}
for i, line := range statusLines {
if len(line) < width {
paddingSize := width - len(line)
statusLines[i] = line + strings.Repeat(" ", paddingSize)
}
}
return strings.Join(statusLines, "\n")
}
func shaToColorFunc(sha string) func(string) string {
return func(c string) string {
red, err := strconv.ParseInt(sha[0:2], 16, 64)
if err != nil {
panic(err)
}
green, err := strconv.ParseInt(sha[2:4], 16, 64)
if err != nil {
panic(err)
}
blue, err := strconv.ParseInt(sha[4:6], 16, 64)
if err != nil {
panic(err)
}
return fmt.Sprintf("\033[38;2;%d;%d;%dm%s\033[0m", red, green, blue, c)
}
}
func computeSeed(seed string) int64 {
lol := ""
for _, r := range seed {
lol += fmt.Sprintf("%d", int(r))
}
result, err := strconv.ParseInt(lol[0:10], 10, 64)
if err != nil {
panic(err)
}
return result
}
func clear(io *iostreams.IOStreams) {
cmd := exec.Command("clear")
cmd.Stdout = io.Out
_ = cmd.Run()
}
func RGB(r, g, b int, x string) string {
return fmt.Sprintf("\033[38;2;%d;%d;%dm%s\033[0m", r, g, b, x)
}