-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.go
220 lines (196 loc) · 4.95 KB
/
ui.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
package main
import (
"fmt"
"github.com/jroimartin/gocui"
"log"
)
func keybindings(g *gocui.Gui) error {
if err := g.SetKeybinding("", 'n', gocui.ModNone,
func(g *gocui.Gui, v *gocui.View) error {
return newView(g)
}); err != nil {
return err
}
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("", 'q', gocui.ModNone, quit); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("", 'w', gocui.ModNone, closeView); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("", gocui.KeyTab, gocui.ModNone,
func(g *gocui.Gui, v *gocui.View) error {
if err := nextView(g); err != nil {
return err
}
return ontop(g, v)
}); err != nil {
return err
}
if err := g.SetKeybinding("", gocui.KeyArrowLeft, gocui.ModNone,
func(g *gocui.Gui, v *gocui.View) error {
return moveView(g, v, -delta, 0)
}); err != nil {
return err
}
if err := g.SetKeybinding("", 'h', gocui.ModNone,
func(g *gocui.Gui, v *gocui.View) error {
return moveView(g, v, -delta, 0)
}); err != nil {
return err
}
if err := g.SetKeybinding("", gocui.KeyArrowRight, gocui.ModNone,
func(g *gocui.Gui, v *gocui.View) error {
return moveView(g, v, delta, 0)
}); err != nil {
return err
}
if err := g.SetKeybinding("", 'l', gocui.ModNone,
func(g *gocui.Gui, v *gocui.View) error {
return moveView(g, v, delta, 0)
}); err != nil {
return err
}
if err := g.SetKeybinding("", gocui.KeyArrowDown, gocui.ModNone,
func(g *gocui.Gui, v *gocui.View) error {
return moveView(g, v, 0, delta)
}); err != nil {
return err
}
if err := g.SetKeybinding("", 'j', gocui.ModNone,
func(g *gocui.Gui, v *gocui.View) error {
return moveView(g, v, 0, delta)
}); err != nil {
return err
}
if err := g.SetKeybinding("", gocui.KeyArrowUp, gocui.ModNone,
func(g *gocui.Gui, v *gocui.View) error {
return moveView(g, v, 0, -delta)
}); err != nil {
return err
}
if err := g.SetKeybinding("", 'k', gocui.ModNone,
func(g *gocui.Gui, v *gocui.View) error {
return moveView(g, v, 0, -delta)
}); err != nil {
return err
}
if err := g.SetKeybinding("", 't', gocui.ModNone, ontop); err != nil {
return err
}
if err := g.SetKeybinding("", '+', gocui.ModNone,
func(g *gocui.Gui, v *gocui.View) error {
return resizeView(g, v, delta, 0)
}); err != nil {
return err
}
if err := g.SetKeybinding("", '_', gocui.ModNone,
func(g *gocui.Gui, v *gocui.View) error {
return resizeView(g, v, -delta, 0)
}); err != nil {
return err
}
if err := g.SetKeybinding("", '=', gocui.ModNone,
func(g *gocui.Gui, v *gocui.View) error {
return resizeView(g, v, 0, delta)
}); err != nil {
return err
}
if err := g.SetKeybinding("", '-', gocui.ModNone,
func(g *gocui.Gui, v *gocui.View) error {
return resizeView(g, v, 0, -delta)
}); err != nil {
return err
}
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
log.Panicln(err)
}
return nil
}
func layout(g *gocui.Gui) error {
maxX, _ := g.Size()
v, err := g.SetView("legend", maxX-25, 0, maxX-1, 8)
if err != nil {
if err != gocui.ErrUnknownView {
return err
}
fmt.Fprintln(v, "KEYBINDINGS")
fmt.Fprintln(v, "n: New View")
fmt.Fprintln(v, "Tab: Next View")
fmt.Fprintln(v, "← ↑ → ↓: Move View")
fmt.Fprintln(v, "w: Delete View")
fmt.Fprintln(v, "^C or q: Exit")
}
return nil
}
func quit(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit
}
func newView(g *gocui.Gui) error {
maxX, maxY := g.Size()
name := fmt.Sprintf("v%v", idxGame)
v, err := g.SetView(name, 0, 0, maxX/10*9, maxY/10*9)
if err != nil {
if err != gocui.ErrUnknownView {
return err
}
}
if err := g.SetCurrentView(name); err != nil {
return err
}
w, h := v.Size()
v.Title = name
game := NewLife(name, w, h)
games = append(games, game)
go game.start(g)
curGame = len(games) - 1
idxGame += 1
return nil
}
func moveView(g *gocui.Gui, v *gocui.View, dx, dy int) error {
name := v.Name()
x0, y0, x1, y1, err := g.ViewPosition(name)
if err != nil {
return err
}
if _, err := g.SetView(name, x0+dx, y0+dy, x1+dx, y1+dy); err != nil {
return err
}
return nil
}
func nextView(g *gocui.Gui) error {
next := curGame + 1
if next > len(games)-1 {
next = 0
}
if err := g.SetCurrentView(games[next].name); err != nil {
return err
}
curGame = next
return nil
}
func closeView(g *gocui.Gui, v *gocui.View) error {
games[curGame].close <- true
if err := g.DeleteView(games[curGame].name); err != nil {
return err
}
// delete the game
games = append(games[:curGame], games[curGame+1:]...)
return nextView(g)
}
func ontop(g *gocui.Gui, v *gocui.View) error {
_, err := g.SetViewOnTop(games[curGame].name)
return err
}
func resizeView(g *gocui.Gui, v *gocui.View, xdelta, ydelta int) error {
x0, y0, x1, y1, err := g.ViewPosition(games[curGame].name)
x1 += xdelta
y1 += ydelta
_, err = g.SetView(games[curGame].name, x0, y0, x1, y1)
if err != nil {
return err
}
return nil
}