/
widget_console.go
360 lines (331 loc) · 9.59 KB
/
widget_console.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
package zterm
import (
"context"
"errors"
"fmt"
"log"
"strings"
"github.com/awesome-gocui/gocui"
)
// WidgetConsole structure for GUI
type WidgetConsole struct {
Widget
lastView string
cmdHistory []string
histIndex int
cancel context.CancelFunc
}
var (
cmdView = "console"
cmdPrompt = "console-prompt"
cmdPromptPS1 = "console-prompt-ps1"
consoleHeight = 3
promptPS1 = colorText(">> ", cConsoleStr)
)
// NewWidgetConsole creates a widget for GUI which doesn't contribute to the layout.
// This type of widget is displayed on top over the layout.
func NewWidgetConsole() *WidgetConsole {
promptPS1 = colorText(">> ", cConsoleStr) // reload color (from theme)
return &WidgetConsole{Widget: Widget{name: cmdView, Enabled: false}}
}
// Layout setup for console widget
func (wc *WidgetConsole) Layout(g *gocui.Gui) error {
// do not display if disabled
if !wc.Enabled {
g.DeleteView(cmdView) // if doesn't exist, don't care
g.DeleteView(cmdPrompt) // ditto...
g.DeleteView(cmdPromptPS1) // ditto...
wc.gview = nil
// check if current view was pointing to this view before (just to be sure!)
if g.CurrentView() != nil && g.CurrentView().Name() == cmdPrompt {
if wc.lastView != "" {
g.SetCurrentView(wc.lastView)
wc.lastView = ""
} else {
setDefaultView(g)
}
}
return nil
}
// Enabled, display...
maxX, maxY := g.Size()
// compute correct position and width
maxHeight := consoleHeight
if wc.gview != nil {
if wc.gview.LinesHeight() > 0 {
maxHeight += wc.gview.LinesHeight() - 1
}
if maxHeight > int(float64(maxY)*0.6) {
maxHeight = int(float64(maxY) * 0.6)
}
}
yPos := maxY - 1 - maxHeight
width := maxX - 1
// set console "outer" window
v, err := g.SetView(cmdView, 0, yPos, width, yPos+maxHeight, 0)
if err != nil {
if !errors.Is(err, gocui.ErrUnknownView) {
return fmt.Errorf("view %v: %v", cmdView, err)
}
// wc.gview = v // set pointer to GUI View for wc.Clear() command
// wc.Clear()
}
wc.gview = v // set pointer to GUI View (only for view, not for input)
v.Wrap = true
// hardcoded colors for frame and title
v.FrameColor = cConsole
v.TitleColor = cConsole
v.FrameRunes = []rune{'═', '║', '╔', '╗', '╚', '╝'}
// set title
v.Title = fmt.Sprintf("< %v >", cmdView)
g.SetViewOnTop(cmdView)
// set consol prompt PS1
v, err = g.SetView(cmdPromptPS1, 0, yPos+maxHeight-2, 4, yPos+maxHeight, 0)
if err != nil {
if !errors.Is(err, gocui.ErrUnknownView) {
return fmt.Errorf("view %v: %v", cmdView, err)
}
fmt.Fprint(v, promptPS1)
}
v.Frame = false
g.SetViewOnTop(cmdPromptPS1)
// set console "input" line
v, err = g.SetView(cmdPrompt, 3, yPos+maxHeight-2, width, yPos+maxHeight, 0)
if err != nil {
if !errors.Is(err, gocui.ErrUnknownView) {
return fmt.Errorf("view %v: %v", cmdView, err)
}
}
// save last CurrentView
if cv := g.CurrentView(); cv != nil && cv.Name() != cmdPrompt {
wc.lastView = cv.Name()
}
// set editing
v.Editable = true
v.Autoscroll = false
v.Frame = false
g.SetViewOnTop(cmdPrompt)
g.SetCurrentView(cmdPrompt)
v.Editor = gocui.EditorFunc(consoleEditor)
return nil
}
// Clear override to not clear the console output (this is triggered everytime new command is issued in update)
func (wc *WidgetConsole) Clear() {
// wc.gview.Clear()
}
// Error print error message to the console output line (second line below prompt)
func (wc *WidgetConsole) Error(err error) {
wc.gview.Autoscroll = true
fmt.Fprintf(wc.gview, "%v %v\n\n", colorText("error:", cErrorStr), err.Error())
}
// Print message to the console output line
func (wc *WidgetConsole) Print(msg string) {
wc.gview.Autoscroll = true
fmt.Fprint(wc.gview, msg)
}
// Println prints message to the console output line and add new line at the end
func (wc *WidgetConsole) Println(msg string) {
wc.gview.Autoscroll = true
fmt.Fprintln(wc.gview, msg)
}
// Printf print formatted message to the console output line (second line below prompt)
func (wc *WidgetConsole) Printf(format string, a ...interface{}) {
wc.gview.Autoscroll = true
fmt.Fprintf(wc.gview, format, a...) // should there be ... ???
}
// Keybinds for specific widget
func (wc *WidgetConsole) Keybinds(g *gocui.Gui) {
// setup Tab for autocompletion (because it's global key, so to work in console, overwrite)
if err := g.SetKeybinding(cmdPrompt, gocui.KeyTab, gocui.ModNone, autoComplete); err != nil {
log.Panicln(err)
}
// cancel key
if err := g.SetKeybinding(cmdPrompt, gocui.KeyCtrlZ, gocui.ModNone, func(g *gocui.Gui, v *gocui.View) error {
if v.Name() == cmdPrompt && wc.cancel != nil {
wc.cancel()
}
return nil
}); err != nil {
log.Panicln(err)
}
}
// ExecCmd execute command in the Console Widget
func (wc *WidgetConsole) ExecCmd(cmd string) {
// add to history and update index
wc.cmdHistory = append(wc.cmdHistory, cmd)
wc.histIndex = len(wc.cmdHistory)
if err := commandExecute(wc, cmd); err != nil {
wc.Println(promptPS1 + cmd)
wc.Error(err)
} else {
wc.Println(promptPS1 + cmd)
}
}
// PrevHistory go back in history and return command from it
func (wc *WidgetConsole) PrevHistory() string {
if len(wc.cmdHistory) > 0 {
wc.histIndex--
if wc.histIndex >= len(wc.cmdHistory) {
wc.histIndex = len(wc.cmdHistory)
return ""
} else if wc.histIndex < 0 {
wc.histIndex = 0
return wc.cmdHistory[0]
} else {
return wc.cmdHistory[wc.histIndex]
}
}
return ""
}
// NextHistory go forward in history and return command from it
func (wc *WidgetConsole) NextHistory() string {
if len(wc.cmdHistory) > 0 {
wc.histIndex++
if wc.histIndex >= len(wc.cmdHistory) {
wc.histIndex = len(wc.cmdHistory)
return ""
} else if wc.histIndex < 0 {
wc.histIndex = 0
return wc.cmdHistory[0]
} else {
return wc.cmdHistory[wc.histIndex]
}
}
return ""
}
// ShowConsole is an update function which should be bound to a key
func showConsole(g *gocui.Gui, v *gocui.View) error {
if wc := getConsoleWidget(); wc != nil {
if wc.IsHidden() {
wc.Enabled = true
} else {
wc.Disconnect()
wc.Enabled = false
wc.Layout(g)
// check if current view was pointing to this view before (just to be sure!)
if g.CurrentView() != nil && g.CurrentView().Name() == cmdPrompt {
if wc.lastView != "" {
g.SetCurrentView(wc.lastView)
wc.lastView = ""
} else {
setDefaultView(g)
}
}
}
return nil
}
// WTF?? what did I do? Where is my console??
return gocui.ErrUnknownView // this error doesn't comfort the new errors in gocui (but whatev)
}
// Console Editor (special setup for keys)
func consoleEditor(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) {
var wc *WidgetConsole
if wc = getConsoleWidget(); wc == nil {
// if no console widget... wtf are we doing here??
return
}
switch {
case ch != 0 && mod == 0:
v.EditWrite(ch)
case key == gocui.KeySpace:
v.EditWrite(' ')
case key == gocui.KeyBackspace || key == gocui.KeyBackspace2:
v.EditDelete(true)
case key == gocui.KeyDelete:
v.EditDelete(false)
case key == gocui.KeyInsert:
v.Overwrite = !v.Overwrite
case key == gocui.KeyEnter:
// command exec
if line, err := v.Line(0); err == nil {
wc.ExecCmd(line)
v.Clear()
v.SetCursor(0, 0)
}
case key == gocui.KeyHome:
x, _ := v.Cursor()
v.MoveCursor(-x, 1)
case key == gocui.KeyEnd:
x, y := v.Cursor()
if line, err := v.Line(y); err == nil {
v.MoveCursor(len(line)-x, 0)
}
case key == gocui.KeyArrowDown:
// command history
if wc := getConsoleWidget(); wc != nil {
newcmd := wc.NextHistory()
v.Clear()
fmt.Fprint(v, newcmd)
v.SetCursor(len(newcmd), 0)
}
case key == gocui.KeyArrowUp:
// command history
if wc := getConsoleWidget(); wc != nil {
newcmd := wc.PrevHistory()
v.Clear()
fmt.Fprint(v, newcmd)
v.SetCursor(len(newcmd), 0)
}
case key == gocui.KeyArrowLeft:
v.MoveCursor(-1, 0)
case key == gocui.KeyArrowRight:
v.MoveCursor(1, 0)
// from awesome-gocui (new addition?)
case key == gocui.KeyCtrlU:
v.EditDeleteToStartOfLine()
case key == gocui.KeyCtrlA:
v.EditGotoToStartOfLine()
case key == gocui.KeyCtrlE:
v.EditGotoToEndOfLine()
// these are for console output view (not for the actual command line)
case key == gocui.KeyPgup:
if wc := getConsoleWidget(); wc != nil {
scrollView(wc.gview, -10)
}
case key == gocui.KeyPgdn:
if wc := getConsoleWidget(); wc != nil {
scrollView(wc.gview, 10)
}
default:
v.EditWrite(ch)
}
}
func autoComplete(g *gocui.Gui, v *gocui.View) error {
// autocompletion
if line, err := v.Line(0); err == nil {
cmdParts := strings.Split(line, " ")
clen := len(cmdParts)
var final []string
if clen == 1 {
autocomp := cmdParts[0]
for c := range cmdAuto {
if strings.HasPrefix(c, autocomp) {
final = append(final, c)
}
}
} else if clen == 2 {
autocomp := cmdParts[1]
for _, c := range cmdAuto[cmdParts[0]] {
if strings.HasPrefix(c, autocomp) {
final = append(final, c)
}
}
}
// finish command or process output for console message
if len(final) == 1 {
v.Clear()
var finalcmd string
if clen == 1 {
finalcmd = final[0] + " "
} else if clen == 2 {
finalcmd = cmdParts[0] + " " + final[0] + " "
}
fmt.Fprint(v, finalcmd)
v.SetCursor(len(finalcmd), 0)
} else if len(final) > 1 {
wc := getConsoleWidget()
wc.Println(strings.Join(final, " "))
}
}
return nil
}