/
console.go
389 lines (328 loc) · 9.7 KB
/
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
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
package clingon
import (
"strings"
"sync"
"time"
)
const (
CARRIAGE_RETURN = 0x000d
BACKSPACE = 0x0008
DELETE = 0x007f
CURSOR_BLINK_TIME = 5e8
HISTORY_NEXT = iota
HISTORY_PREV
CURSOR_LEFT
CURSOR_RIGHT
)
type Evaluator interface {
Run(console *Console, command string) error
}
type Cmd_UpdateConsole struct {
console *Console
}
type Cmd_UpdateCursor struct {
commandLine *commandLine
enabled bool
}
type Cmd_UpdateCommandLine struct {
console *Console
commandLine *commandLine
}
type Renderer interface {
// Receive Cmd_* objects from the console
EventCh() chan<- interface{}
}
type commandLine struct {
prompt string
// The prompt line as a string vector
content []string
// The history of the command line
history [][]string
// Cursor position on current line
cursorPosition int
historyPosition int
}
func newCommandLine(prompt string) *commandLine {
return &commandLine{
prompt: prompt,
content: nil,
cursorPosition: 0,
historyPosition: 0,
}
}
func (commandLine *commandLine) setPrompt(prompt string) {
commandLine.prompt = prompt
}
// Check if the command line is empty
func (commandLine *commandLine) empty() bool {
return len(commandLine.toString()) == 0
}
// Insert a string in the prompt line
func (commandLine *commandLine) insertChar(str string) {
// Insert 'str' at commandLine.cursorPosition
commandLine.content = append(commandLine.content, "")
pos := commandLine.cursorPosition
copy(commandLine.content[pos+1:], commandLine.content[pos:])
commandLine.content[pos] = str
commandLine.incCursorPosition(len(str))
}
// Delete the character on the left of the current cursor position
func (commandLine *commandLine) key_backspace() {
if commandLine.cursorPosition >= 1 {
commandLine.decCursorPosition(1)
// Delete the element at 'commandLine.cursorPosition'
pos := commandLine.cursorPosition
copy(commandLine.content[pos:], commandLine.content[pos+1:])
commandLine.content = commandLine.content[0 : len(commandLine.content)-1]
}
}
// Delete the character at the current cursor position
func (commandLine *commandLine) key_delete() {
if commandLine.cursorPosition < len(commandLine.content) {
// Delete the element at 'commandLine.cursorPosition'
pos := commandLine.cursorPosition
copy(commandLine.content[pos:], commandLine.content[pos+1:])
commandLine.content = commandLine.content[0 : len(commandLine.content)-1]
}
}
// Clear the prompt line
func (commandLine *commandLine) clear() {
commandLine.content = []string(nil)
commandLine.cursorPosition = 0
commandLine.historyPosition = len(commandLine.history)
}
// Browse history on the command line
func (commandLine *commandLine) browseHistory(direction int) {
if direction == HISTORY_NEXT {
commandLine.historyPosition++
} else {
commandLine.historyPosition--
}
if commandLine.historyPosition < 0 {
commandLine.historyPosition = 0
return
}
if commandLine.historyPosition >= len(commandLine.history) {
commandLine.clear()
return
}
var newContent []string
newContent = append(newContent, commandLine.history[commandLine.historyPosition]...)
commandLine.content = newContent
commandLine.incCursorPosition(len(commandLine.toString()))
}
// Push current command line on the history and reset the history counter
func (commandLine *commandLine) push() string {
if commandLine.contentToString() != "" && commandLine.notInHistory(commandLine.contentToString()) {
commandLine.history = append(commandLine.history, commandLine.content)
}
line := commandLine.contentToString()
commandLine.clear()
return line
}
// Return the current prompt line as a single string (including prompt)
func (commandLine *commandLine) toString() string {
return commandLine.prompt + commandLine.contentToString()
}
// Move the cursor left/right on the command line
func (commandLine *commandLine) moveCursor(dir int) {
if dir == CURSOR_LEFT {
commandLine.decCursorPosition(1)
} else {
commandLine.incCursorPosition(1)
}
}
func (commandLine *commandLine) contentToString() string {
return strings.Join(commandLine.content, "")
}
func (commandLine *commandLine) decCursorPosition(dec int) {
commandLine.cursorPosition -= dec
if commandLine.cursorPosition < 0 {
commandLine.cursorPosition = 0
}
}
func (commandLine *commandLine) incCursorPosition(inc int) {
commandLine.cursorPosition += inc
if commandLine.cursorPosition > len(commandLine.content) {
commandLine.cursorPosition = len(commandLine.content)
}
}
func (commandLine *commandLine) notInHistory(line string) bool {
for _, h := range commandLine.history {
historyEntry := strings.Join(h, "")
if line == historyEntry {
return false
}
}
return true
}
type Console struct {
commandLine *commandLine // An instance of the commandline
lines []string // Lines of text above the commandline
renderer_orNil Renderer
evaluator_orNil Evaluator
mu sync.RWMutex
}
// Initialize a new console object passing to it an evaluator
func NewConsole(evaluator_orNil Evaluator) *Console {
console := &Console{
lines: make([]string, 0),
commandLine: newCommandLine("console> "),
renderer_orNil: nil,
evaluator_orNil: evaluator_orNil,
}
go console.loop()
return console
}
// Return the current renderer.
func (console *Console) RendererOrNil() Renderer {
console.mu.RLock()
renderer := console.renderer_orNil
console.mu.RUnlock()
return renderer
}
// Replace the current renderer with a new one
func (console *Console) SetRenderer(renderer_orNil Renderer) {
console.mu.Lock()
console.renderer_orNil = renderer_orNil
console.mu.Unlock()
if renderer_orNil != nil {
renderer_orNil.EventCh() <- Cmd_UpdateConsole{console}
}
}
// Dump the console content as a string.
func (console *Console) String() string {
var result string
result += strings.Join(console.lines, "\n")
result += "\n"
result += console.commandLine.toString()
return result
}
// Set the prompt string
func (console *Console) SetPrompt(prompt string) {
console.commandLine.setPrompt(prompt)
}
// Print a slice of strings on the console.
// The strings should not contain '\n'.
func (console *Console) PrintLines(strings []string) {
if len(strings) > 0 {
console.appendLines(strings)
}
console.mu.RLock()
renderer := console.renderer_orNil
console.mu.RUnlock()
if renderer != nil {
renderer.EventCh() <- Cmd_UpdateConsole{console}
}
}
// Print a string on the console and go to the next line.
// This function can handle strings containing '\n'.
func (console *Console) Print(str string) {
if strings.HasSuffix(str, "\n") {
str = str[0 : len(str)-1]
}
console.PrintLines(strings.Split(str, "\n"))
}
// Get the current command line as string.
func (console *Console) Commandline() string {
return console.commandLine.toString()
}
// Put a command and execute it
func (console *Console) PutCommand(command string) {
console.PutString(command)
console.PutUnicode(CARRIAGE_RETURN)
}
// Put an unicode value on the command-line at the current cursor position.
func (console *Console) PutUnicode(value uint16) {
var event interface{}
switch value {
case BACKSPACE:
console.commandLine.key_backspace()
event = Cmd_UpdateCommandLine{console, console.commandLine}
case DELETE:
console.commandLine.key_delete()
event = Cmd_UpdateCommandLine{console, console.commandLine}
case CARRIAGE_RETURN:
command := console.carriageReturn()
if console.evaluator_orNil != nil {
if err := console.evaluator_orNil.Run(console, command); err != nil {
console.Print(err.Error())
}
}
event = Cmd_UpdateConsole{console}
default:
console.commandLine.insertChar(string(value))
event = Cmd_UpdateCommandLine{console, console.commandLine}
}
console.mu.RLock()
renderer := console.renderer_orNil
console.mu.RUnlock()
if renderer != nil {
renderer.EventCh() <- event
}
}
// Put a readline-like command on the commandline (e.g. history
// browsing, cursor movements, etc.). Readline's commands emulation is
// incomplete.
func (console *Console) PutReadline(command int) {
switch command {
case HISTORY_NEXT, HISTORY_PREV:
console.commandLine.browseHistory(command)
case CURSOR_LEFT, CURSOR_RIGHT:
console.commandLine.moveCursor(command)
}
console.mu.RLock()
renderer := console.renderer_orNil
console.mu.RUnlock()
if renderer != nil {
renderer.EventCh() <- Cmd_UpdateCommandLine{console, console.commandLine}
}
}
// Put the given string on the command line at the current cursor
// position.
func (console *Console) PutString(str string) {
for _, c := range str {
console.commandLine.insertChar(string(c))
}
console.mu.RLock()
renderer := console.renderer_orNil
console.mu.RUnlock()
if renderer != nil {
renderer.EventCh() <- Cmd_UpdateCommandLine{console, console.commandLine}
}
}
// Clear the commandline.
func (console *Console) ClearCommandline() {
console.commandLine.clear()
console.mu.RLock()
renderer := console.renderer_orNil
console.mu.RUnlock()
if renderer != nil {
renderer.EventCh() <- Cmd_UpdateCommandLine{console, console.commandLine}
}
}
// Push lines of text on the console
func (console *Console) appendLines(lines []string) {
console.lines = append(console.lines, lines...)
}
// Push the current commandline in the console history.
// Return it as a string.
func (console *Console) carriageReturn() string {
commandLine := console.commandLine.push()
console.lines = append(console.lines, console.commandLine.prompt+commandLine)
return commandLine
}
func (console *Console) loop() {
var toggleCursor bool
ticker := time.NewTicker(CURSOR_BLINK_TIME)
for {
<-ticker.C // Blink cursor
console.mu.RLock()
renderer := console.renderer_orNil
console.mu.RUnlock()
if renderer != nil {
renderer.EventCh() <- Cmd_UpdateCursor{console.commandLine, toggleCursor}
}
toggleCursor = !toggleCursor
}
}