-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.go
336 lines (298 loc) · 7.96 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
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
package ui
import (
"errors"
"fmt"
"github.com/gdamore/tcell/v2"
"os"
)
// Mode defines the behaviour of the UI: Navigate or Input. It controls how
// EventKeys are handled in the Tick() method.
type Mode int
// Modes for the UI are: Navigate, and Input.
const (
Navigate Mode = iota
Input
)
// CommandKey stores a tcell compatible key that can be compared to an EventKey
// event. The Rune for a non-alphabetical key must be set as the Rune value for
// that key. e.g. KeyEnter == rune(13)
type CommandKey struct {
Key tcell.Key
Rune rune
Mod tcell.ModMask
}
// Command stores a callback function and a description of the command for the
// footer.
type Command struct {
Callback func()
Description string
}
type cursor struct {
X, Y int
}
// PneumaUI represents a UI that the user can interact with. It contains a
// tcell.Screen as well as commands and content.
type PneumaUI struct {
Screen tcell.Screen
Cursor cursor
Exit bool
Style tcell.Style
Mode Mode
InputBuffer string
Content []Drawable
Commands map[CommandKey]Command
suspended bool
}
func check(e error) {
if e != nil {
panic(e)
}
}
// Init creates everything necessary for an interactive user-interface by
// creating and initialising a new tcell.Screen, blank command map and setting
// the cursor to 0,0. It returns a PneumaUI strict.
func Init() PneumaUI {
screen, err := tcell.NewScreen()
check(err)
err = screen.Init()
check(err)
screen.Clear()
commands := make(map[CommandKey]Command)
ui := PneumaUI{
Screen: screen,
Cursor: cursor{0, 0},
Exit: false,
Mode: Navigate,
Style: tcell.StyleDefault,
Commands: commands,
suspended: false,
}
return ui
}
// Redraw clears the screen and re-renders all of the widgets in content. It
// does not reset the cursor or any other state of the PneumaUI
func (ui *PneumaUI) Redraw() {
ui.Screen.Clear()
ui.Draw()
ui.Screen.Sync()
}
// Reset clears the screen, content and cursor. It essentially puts the ui into
// the same state as Init does.
func (ui *PneumaUI) Reset() {
ui.Screen.Clear()
ui.Screen.Sync()
ui.Cursor = cursor{0, 0}
ui.Mode = Navigate
ui.InputBuffer = ""
ui.Content = make([]Drawable, 0)
}
// Close finalises the tcell.Screen and exits the program with status code 0.
func (ui PneumaUI) Close() {
ui.Screen.Clear()
ui.Screen.Sync()
ui.Screen.Fini()
os.Exit(0)
ui.Exit = true
}
// WaitForInput blocks until the user finished entering a string, and returns
// the inputBuffer of the PneumaUI.
func (ui *PneumaUI) WaitForInput() string {
ui.Mode = Input
for {
ui.Tick()
if ui.Mode == Navigate {
break
}
}
return ui.InputBuffer
}
// Confirm stalls user interaction with a prompt and waits for the user to
// either accept or reject the prompt (default: reject). This then returns the
// choice as a boolean.
func (ui *PneumaUI) Confirm(prompt string) bool {
confirmed := false
ui.Style = ui.Style.Reverse(true)
ui.putString(prompt)
ui.Style = ui.Style.Reverse(false)
ui.Style = ui.Style.Dim(true)
ui.Draw()
ui.Mode = Input
for {
ui.Tick()
if len(ui.InputBuffer) > 0 {
if ui.InputBuffer[0] == 'y' || ui.InputBuffer[0] == 'Y' {
confirmed = true
break
} else if ui.InputBuffer[0] == 'n' || ui.InputBuffer[0] == 'N' {
break
} else if ui.Mode == Navigate {
break
} else {
ui.InputBuffer = ""
}
}
}
ui.Style = ui.Style.Dim(false)
ui.InputBuffer = ""
ui.Mode = Navigate
return confirmed
}
// SetCommands takes a map of CommandKeys and callback functions that will be
// checked against EventKeys in Tick().
func (ui *PneumaUI) SetCommands(commands map[CommandKey]Command) {
ui.Commands = commands
}
// Tick is the main method that applications using the ui should spin on. It
// makes sure to check against the Commands while in Navigate mode, executing
// callbacks if necessary. During Input mode, it ensures to capture
// alphanumerical letters to the input buffer (terminating on escape or enter)
// and rendering the character to screen at the cursor.
func (ui *PneumaUI) Tick() {
if ui.suspended {
return
}
ui.drawFooter()
ui.Screen.Sync()
switch ev := ui.Screen.PollEvent().(type) {
case *tcell.EventResize:
ui.Redraw()
case *tcell.EventKey:
if ui.Mode == Navigate {
key := CommandKey{Key: ev.Key(), Rune: ev.Rune(), Mod: ev.Modifiers()}
if cmd, ok := ui.Commands[key]; ok {
cmd.Callback()
ui.Redraw()
}
} else if ui.Mode == Input {
switch ev.Key() {
case tcell.KeyEnter:
ui.Mode = Navigate
case tcell.KeyEscape:
ui.Mode = Navigate
ui.InputBuffer = ""
case tcell.KeyBackspace, tcell.KeyBackspace2:
if len(ui.InputBuffer) > 0 {
ui.InputBuffer = ui.InputBuffer[:len(ui.InputBuffer)-1]
ui.Cursor.X--
ui.putRune(' ')
}
case tcell.KeyRune:
ui.InputBuffer += string(ev.Rune())
ui.putRune(ev.Rune())
ui.Cursor.X++
}
}
}
}
// MoveCursor attempts to place the cursor at a given location and checks for
// out of bounds errors.
func (ui *PneumaUI) MoveCursor(x, y int) error {
w, h := ui.Screen.Size()
if x < 0 || x > w || y < 0 || y > h {
return errors.New("cursor out of bounds")
}
ui.Cursor.X = x
ui.Cursor.Y = y
return nil
}
func (ui PneumaUI) putRune(r rune) {
//ui.Screen.SetContent(ui.Cursor.X, ui.Cursor.Y, r, []rune{}, tcell.StyleDefault)
ui.Screen.SetContent(ui.Cursor.X, ui.Cursor.Y, r, []rune{}, ui.Style)
}
func (ui *PneumaUI) putString(str string) {
for _, c := range str {
ui.putRune(rune(c))
ui.Cursor.X++
}
}
func (ui PneumaUI) drawFooter() {
var footerContent string
switch ui.Mode {
case Navigate:
for key, cmd := range ui.Commands {
footerContent += fmt.Sprintf("[%s]: %s ", string(key.Rune), cmd.Description)
}
case Input:
footerContent = "INPUT"
}
cursorYPos := ui.Cursor.Y
cursorXPos := ui.Cursor.X
w, h := ui.Screen.Size()
ui.hLine(0, w, h-2)
ui.Cursor.Y = h - 2
ui.Cursor.X = 0
ui.Cursor.Y = h - 1
ui.putString(fmt.Sprintf("%-*s", w, footerContent))
ui.Cursor.Y = cursorYPos
ui.Cursor.X = cursorXPos
}
func (ui *PneumaUI) hLine(startX, endX, y int) {
oldCursorPos := ui.Cursor
for x := startX; x < endX; x++ {
ui.MoveCursor(x, y)
ui.putString("─")
}
ui.Cursor = oldCursorPos
}
func (ui *PneumaUI) vLine(startY, endY, x int) {
oldCursorPos := ui.Cursor
for y := startY; y < endY; y++ {
ui.MoveCursor(x, y)
ui.putString("│")
}
ui.Cursor = oldCursorPos
}
func (ui *PneumaUI) box(x, y, w, h int) {
corners := []string{"╭", "╮", "╰", "╯"}
ui.hLine(x, x+w, y)
ui.hLine(x, x+w, y+h)
ui.vLine(y, y+h, x)
ui.vLine(y, y+h, x+w)
ui.MoveCursor(x, y)
ui.putString(corners[0])
ui.MoveCursor(x+w, y)
ui.putString(corners[1])
ui.MoveCursor(x, y+h)
ui.putString(corners[2])
ui.MoveCursor(x+w, y+h)
ui.putString(corners[3])
}
// Draw renders all of the content to the screen, in order. It does not care
// about clearing things that are already on screen, meaning that successive
// Draw() calls can be made with modified content without replacing anything on
// the screen.
func (ui *PneumaUI) Draw() {
for _, drawable := range ui.Content {
drawable.Draw(ui)
}
}
// AddLabel creates a new Label widget an adds it to the content.
func (ui *PneumaUI) AddLabel(x, y int, text string) *Label {
label := &Label{X: x, Y: y, Content: text}
ui.Content = append(ui.Content, label)
return label
}
// AddTable appends a new Table widget to the content.
func (ui *PneumaUI) AddTable(x, y int, headings []string, content [][]string) *Table {
table := &Table{X: x, Y: y, Headings: headings, Content: content, Active: true, Index: 0}
ui.Content = append(ui.Content, table)
return table
}
// Suspend stops and destroys the screen and allows another program to run
func (ui *PneumaUI) Suspend() {
ui.Screen.Fini()
ui.suspended = true
}
// Resume creates a new screen after the previous one was destroyed with Suspend
func (ui *PneumaUI) Resume() {
if ui.suspended {
screen, err := tcell.NewScreen()
check(err)
err = screen.Init()
check(err)
screen.Clear()
ui.Screen = screen
ui.suspended = false
ui.Redraw()
}
}