forked from c-bata/go-prompt
-
Notifications
You must be signed in to change notification settings - Fork 2
/
prompt.go
665 lines (583 loc) · 18 KB
/
prompt.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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
package prompt
import (
"bytes"
"os"
"strings"
"time"
"unicode"
"unicode/utf8"
"github.com/elk-language/go-prompt/debug"
istrings "github.com/elk-language/go-prompt/strings"
)
const inputBufferSize = 1024
// Executor is called when the user
// inputs a line of text.
type Executor func(string)
// ExitChecker is called after user input to check if prompt must stop and exit go-prompt Run loop.
// User input means: selecting/typing an entry, then, if said entry content matches the ExitChecker function criteria:
// - immediate exit (if breakline is false) without executor called
// - exit after typing <return> (meaning breakline is true), and the executor is called first, before exit.
// Exit means exit go-prompt (not the overall Go program)
type ExitChecker func(in string, breakline bool) bool
// ExecuteOnEnterCallback is a function that receives
// user input after Enter has been pressed
// and determines whether the input should be executed.
// If this function returns true, the Executor callback will be called
// otherwise a newline will be added to the buffer containing user input
// and optionally indentation made up of `indentSize * indent` spaces.
type ExecuteOnEnterCallback func(prompt *Prompt, indentSize int) (indent int, execute bool)
// Completer is a function that returns
// a slice of suggestions for the given Document.
//
// startChar and endChar represent the indices of the first and last rune of the text
// that the suggestions were generated for and that should be replaced by the selected suggestion.
type Completer func(Document) (suggestions []Suggest, startChar, endChar istrings.RuneNumber)
// Prompt is a core struct of go-prompt.
type Prompt struct {
reader Reader
buffer *Buffer
renderer *Renderer
executor Executor
history *History
lexer Lexer
completion *CompletionManager
keyBindings []KeyBind
ASCIICodeBindings []ASCIICodeBind
keyBindMode KeyBindMode
completionOnDown bool
exitChecker ExitChecker
executeOnEnterCallback ExecuteOnEnterCallback
skipClose bool
completionReset bool
}
// UserInput is the struct that contains the user input context.
type UserInput struct {
input string
}
// Run starts the prompt.
func (p *Prompt) Run() {
p.skipClose = false
defer debug.Close()
debug.Log("start prompt")
p.setup()
defer p.Close()
if p.completion.showAtStart {
p.completion.Update(*p.buffer.Document())
}
p.renderer.Render(p.buffer, p.completion, p.lexer)
bufCh := make(chan []byte, 128)
stopReadBufCh := make(chan struct{})
go p.readBuffer(bufCh, stopReadBufCh)
exitCh := make(chan int)
winSizeCh := make(chan *WinSize)
stopHandleSignalCh := make(chan struct{})
go p.handleSignals(exitCh, winSizeCh, stopHandleSignalCh)
for {
select {
case b := <-bufCh:
if shouldExit, rerender, input := p.feed(b); shouldExit {
p.renderer.BreakLine(p.buffer, p.lexer)
stopReadBufCh <- struct{}{}
stopHandleSignalCh <- struct{}{}
return
} else if input != nil {
// Stop goroutine to run readBuffer function
stopReadBufCh <- struct{}{}
stopHandleSignalCh <- struct{}{}
// Unset raw mode
// Reset to Blocking mode because returned EAGAIN when still set non-blocking mode.
debug.AssertNoError(p.reader.Close())
p.executor(input.input)
p.completion.Update(*p.buffer.Document())
p.renderer.Render(p.buffer, p.completion, p.lexer)
if p.exitChecker != nil && p.exitChecker(input.input, true) {
p.skipClose = true
return
}
// Set raw mode
debug.AssertNoError(p.reader.Open())
go p.readBuffer(bufCh, stopReadBufCh)
go p.handleSignals(exitCh, winSizeCh, stopHandleSignalCh)
} else if rerender {
if p.completion.shouldUpdate {
p.completion.Update(*p.buffer.Document())
}
p.renderer.Render(p.buffer, p.completion, p.lexer)
}
case w := <-winSizeCh:
p.renderer.UpdateWinSize(w)
p.buffer.resetStartLine()
p.buffer.recalculateStartLine(p.renderer.UserInputColumns(), int(p.renderer.row))
p.renderer.Render(p.buffer, p.completion, p.lexer)
case code := <-exitCh:
p.renderer.BreakLine(p.buffer, p.lexer)
p.Close()
os.Exit(code)
default:
time.Sleep(10 * time.Millisecond)
}
}
}
// func Log(format string, a ...any) {
// f, err := os.OpenFile("log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
// if err != nil {
// log.Fatalf("error opening file: %v", err)
// }
// defer f.Close()
// fmt.Fprintf(f, format+"\n", a...)
// }
// Returns the configured indent size.
func (p *Prompt) IndentSize() int {
return p.renderer.indentSize
}
// Get the number of columns that are available
// for user input.
func (p *Prompt) UserInputColumns() istrings.Width {
return p.renderer.UserInputColumns()
}
// Returns the current amount of columns that the terminal can display.
func (p *Prompt) TerminalColumns() istrings.Width {
return p.renderer.col
}
// Returns the current amount of rows that the terminal can display.
func (p *Prompt) TerminalRows() int {
return p.renderer.row
}
// Returns the buffer struct.
func (p *Prompt) Buffer() *Buffer {
return p.buffer
}
func (p *Prompt) feed(b []byte) (shouldExit bool, rerender bool, userInput *UserInput) {
key := GetKey(b)
p.buffer.lastKeyStroke = key
// completion
completing := p.completion.Completing()
if p.handleCompletionKeyBinding(b, key, completing) {
return false, true, nil
}
cols := p.renderer.UserInputColumns()
rows := p.renderer.row
switch key {
case Enter, ControlJ, ControlM:
indent, execute := p.executeOnEnterCallback(p, p.renderer.indentSize)
if !execute {
p.buffer.NewLine(cols, rows, false)
var indentStrBuilder strings.Builder
indentUnitCount := indent * p.renderer.indentSize
for i := 0; i < indentUnitCount; i++ {
indentStrBuilder.WriteRune(IndentUnit)
}
p.buffer.InsertTextMoveCursor(indentStrBuilder.String(), cols, rows, false)
break
}
p.renderer.BreakLine(p.buffer, p.lexer)
userInput = &UserInput{input: p.buffer.Text()}
p.buffer = NewBuffer()
if userInput.input != "" {
p.history.Add(userInput.input)
}
case ControlC:
p.renderer.BreakLine(p.buffer, p.lexer)
p.buffer = NewBuffer()
p.history.Clear()
case Up, ControlP:
line := p.buffer.Document().CursorPositionRow()
if line > 0 {
rerender = p.CursorUp(1)
return false, rerender, nil
}
if completing {
break
}
if newBuf, changed := p.history.Older(p.buffer, cols, rows); changed {
p.buffer = newBuf
}
case Down, ControlN:
endOfTextRow := p.buffer.Document().TextEndPositionRow()
row := p.buffer.Document().CursorPositionRow()
if endOfTextRow > row {
rerender = p.CursorDown(1)
return false, rerender, nil
}
if completing {
break
}
if newBuf, changed := p.history.Newer(p.buffer, cols, rows); changed {
p.buffer = newBuf
}
return false, true, nil
case ControlD:
if p.buffer.Text() == "" {
return true, true, nil
}
case NotDefined:
var checked bool
checked, rerender = p.handleASCIICodeBinding(b, cols, rows)
if checked {
return false, rerender, nil
}
char, _ := utf8.DecodeRune(b)
if unicode.IsControl(char) {
return false, false, nil
}
p.buffer.InsertTextMoveCursor(string(b), cols, rows, false)
}
shouldExit, rerender = p.handleKeyBinding(key, cols, rows)
return shouldExit, rerender, userInput
}
func (p *Prompt) handleCompletionKeyBinding(b []byte, key Key, completing bool) (handled bool) {
p.completion.shouldUpdate = true
cols := p.renderer.UserInputColumns()
rows := p.renderer.row
completionLen := len(p.completion.tmp)
p.completionReset = false
keySwitch:
switch key {
case Down:
if completing || p.completionOnDown {
p.updateSuggestions(func() {
p.completion.Next()
})
return true
}
case ControlI:
p.updateSuggestions(func() {
p.completion.Next()
})
return true
case Up:
if completing {
p.updateSuggestions(func() {
p.completion.Previous()
})
return true
}
case Tab:
if completionLen > 0 {
// If there are any suggestions, select the next one
p.updateSuggestions(func() {
p.completion.Next()
})
return true
}
// if there are no suggestions insert indentation
newBytes := make([]byte, 0, len(b))
for _, byt := range b {
switch byt {
case '\t':
for i := 0; i < p.renderer.indentSize; i++ {
newBytes = append(newBytes, IndentUnit)
}
default:
newBytes = append(newBytes, byt)
}
}
p.buffer.InsertTextMoveCursor(string(newBytes), cols, rows, false)
return true
case BackTab:
if completionLen > 0 {
// If there are any suggestions, select the previous one
p.updateSuggestions(func() {
p.completion.Previous()
})
return true
}
text := p.buffer.Document().CurrentLineBeforeCursor()
for _, char := range text {
if char != IndentUnit {
break keySwitch
}
}
p.buffer.DeleteBeforeCursorRunes(istrings.RuneNumber(p.renderer.indentSize), cols, rows)
return true
default:
if s, ok := p.completion.GetSelectedSuggestion(); ok {
w := p.buffer.Document().GetWordBeforeCursorUntilSeparator(p.completion.wordSeparator)
if w != "" {
p.buffer.DeleteBeforeCursorRunes(istrings.RuneCountInString(w), cols, rows)
}
p.buffer.InsertTextMoveCursor(s.Text, cols, rows, false)
}
if completionLen > 0 {
p.completionReset = true
}
p.completion.Reset()
}
return false
}
func (p *Prompt) updateSuggestions(fn func()) {
cols := p.renderer.UserInputColumns()
rows := p.renderer.row
prevStart := p.completion.startCharIndex
prevEnd := p.completion.endCharIndex
prevSuggestion, prevSelected := p.completion.GetSelectedSuggestion()
fn()
p.completion.shouldUpdate = false
newSuggestion, newSelected := p.completion.GetSelectedSuggestion()
// do nothing
if !prevSelected && !newSelected {
return
}
// insert the new selection
if !prevSelected {
p.buffer.DeleteBeforeCursorRunes(p.completion.endCharIndex-p.completion.startCharIndex, cols, rows)
p.buffer.InsertTextMoveCursor(newSuggestion.Text, cols, rows, false)
return
}
// delete the previous selection
if !newSelected {
p.buffer.DeleteBeforeCursorRunes(
istrings.RuneCountInString(prevSuggestion.Text)-(prevEnd-prevStart),
cols,
rows,
)
return
}
// delete previous selection and render the new one
p.buffer.DeleteBeforeCursorRunes(
istrings.RuneCountInString(prevSuggestion.Text),
cols,
rows,
)
p.buffer.InsertTextMoveCursor(newSuggestion.Text, cols, rows, false)
}
func (p *Prompt) handleKeyBinding(key Key, cols istrings.Width, rows int) (shouldExit bool, rerender bool) {
var executed bool
for i := range commonKeyBindings {
kb := commonKeyBindings[i]
if kb.Key == key {
result := kb.Fn(p)
executed = true
if !rerender {
rerender = result
}
}
}
switch p.keyBindMode {
case EmacsKeyBind:
for i := range emacsKeyBindings {
kb := emacsKeyBindings[i]
if kb.Key == key {
result := kb.Fn(p)
executed = true
if !rerender {
rerender = result
}
}
}
}
// Custom key bindings
for i := range p.keyBindings {
kb := p.keyBindings[i]
if kb.Key == key {
result := kb.Fn(p)
executed = true
if !rerender {
rerender = result
}
}
}
if p.exitChecker != nil && p.exitChecker(p.buffer.Text(), false) {
shouldExit = true
}
if !executed && !rerender {
rerender = true
}
return shouldExit, rerender
}
func (p *Prompt) handleASCIICodeBinding(b []byte, cols istrings.Width, rows int) (checked, rerender bool) {
for _, kb := range p.ASCIICodeBindings {
if bytes.Equal(kb.ASCIICode, b) {
result := kb.Fn(p)
if !rerender {
rerender = result
}
checked = true
}
}
return checked, rerender
}
// Input starts the prompt, lets the user
// input a single line and returns this line as a string.
func (p *Prompt) Input() string {
defer debug.Close()
debug.Log("start prompt")
p.setup()
defer p.Close()
if p.completion.showAtStart {
p.completion.Update(*p.buffer.Document())
}
p.renderer.Render(p.buffer, p.completion, p.lexer)
bufCh := make(chan []byte, 128)
stopReadBufCh := make(chan struct{})
go p.readBuffer(bufCh, stopReadBufCh)
for {
select {
case b := <-bufCh:
if shouldExit, rerender, input := p.feed(b); shouldExit {
p.renderer.BreakLine(p.buffer, p.lexer)
stopReadBufCh <- struct{}{}
return ""
} else if input != nil {
// Stop goroutine to run readBuffer function
stopReadBufCh <- struct{}{}
return input.input
} else if rerender {
p.completion.Update(*p.buffer.Document())
p.renderer.Render(p.buffer, p.completion, p.lexer)
}
default:
time.Sleep(10 * time.Millisecond)
}
}
}
const IndentUnit = ' '
const IndentUnitString = string(IndentUnit)
func (p *Prompt) readBuffer(bufCh chan []byte, stopCh chan struct{}) {
debug.Log("start reading buffer")
for {
select {
case <-stopCh:
debug.Log("stop reading buffer")
return
default:
bytes := make([]byte, inputBufferSize)
n, err := p.reader.Read(bytes)
if err != nil {
break
}
bytes = bytes[:n]
// Log("%#v", bytes)
if len(bytes) == 1 && bytes[0] == '\t' {
// if only a single Tab key has been pressed
// handle it as a keybind
bufCh <- bytes
} else if len(bytes) != 1 || bytes[0] != 0 {
newBytes := make([]byte, 0, len(bytes))
for _, byt := range bytes {
switch byt {
// translate raw mode \r into \n
// to make pasting multiline text
// work properly
case '\r':
newBytes = append(newBytes, '\n')
// translate \t into two spaces
// to avoid problems with cursor positions
case '\t':
for i := 0; i < p.renderer.indentSize; i++ {
newBytes = append(newBytes, IndentUnit)
}
default:
newBytes = append(newBytes, byt)
}
}
bufCh <- newBytes
}
}
time.Sleep(10 * time.Millisecond)
}
}
func (p *Prompt) setup() {
debug.AssertNoError(p.reader.Open())
p.renderer.Setup()
p.renderer.UpdateWinSize(p.reader.GetWinSize())
}
// Move to the left on the current line by the given amount of graphemes (visible characters).
// Returns true when the view should be rerendered.
func (p *Prompt) CursorLeft(count istrings.GraphemeNumber) bool {
return promptCursorHorizontalMove(p, p.buffer.CursorLeft, count)
}
// Move to the left on the current line by the given amount of runes.
// Returns true when the view should be rerendered.
func (p *Prompt) CursorLeftRunes(count istrings.RuneNumber) bool {
return promptCursorHorizontalMove(p, p.buffer.CursorLeftRunes, count)
}
// Move the cursor to the right on the current line by the given amount of graphemes (visible characters).
// Returns true when the view should be rerendered.
func (p *Prompt) CursorRight(count istrings.GraphemeNumber) bool {
return promptCursorHorizontalMove(p, p.buffer.CursorRight, count)
}
// Move the cursor to the right on the current line by the given amount of runes.
// Returns true when the view should be rerendered.
func (p *Prompt) CursorRightRunes(count istrings.RuneNumber) bool {
return promptCursorHorizontalMove(p, p.buffer.CursorRightRunes, count)
}
type horizontalCursorModifier[CountT ~int] func(CountT, istrings.Width, int) bool
// Move to the left or right on the current line.
// Returns true when the view should be rerendered.
func promptCursorHorizontalMove[CountT ~int](p *Prompt, modifierFunc horizontalCursorModifier[CountT], count CountT) bool {
b := p.buffer
cols := p.renderer.UserInputColumns()
previousCursor := b.DisplayCursorPosition(cols)
rerender := modifierFunc(count, cols, p.renderer.row) || p.completionReset || len(p.completion.tmp) > 0
if rerender {
return true
}
newCursor := b.DisplayCursorPosition(cols)
p.renderer.previousCursor = newCursor
p.renderer.move(previousCursor, newCursor)
p.renderer.flush()
return false
}
// Move the cursor up.
// Returns true when the view should be rerendered.
func (p *Prompt) CursorUp(count int) bool {
b := p.buffer
cols := p.renderer.UserInputColumns()
previousCursor := b.DisplayCursorPosition(cols)
rerender := p.buffer.CursorUp(count, cols, p.renderer.row) || p.completionReset || len(p.completion.tmp) > 0
if rerender {
return true
}
newCursor := b.DisplayCursorPosition(cols)
p.renderer.previousCursor = newCursor
p.renderer.move(previousCursor, newCursor)
p.renderer.flush()
return false
}
// Move the cursor down.
// Returns true when the view should be rerendered.
func (p *Prompt) CursorDown(count int) bool {
b := p.buffer
cols := p.renderer.UserInputColumns()
previousCursor := b.DisplayCursorPosition(cols)
rerender := p.buffer.CursorDown(count, cols, p.renderer.row) || p.completionReset || len(p.completion.tmp) > 0
if rerender {
return true
}
newCursor := b.DisplayCursorPosition(cols)
p.renderer.previousCursor = newCursor
p.renderer.move(previousCursor, newCursor)
p.renderer.flush()
return false
}
// Deletes the specified number of graphemes before the cursor and returns the deleted text.
func (p *Prompt) DeleteBeforeCursor(count istrings.GraphemeNumber) string {
return p.buffer.DeleteBeforeCursor(count, p.UserInputColumns(), p.renderer.row)
}
// Deletes the specified number of runes before the cursor and returns the deleted text.
func (p *Prompt) DeleteBeforeCursorRunes(count istrings.RuneNumber) string {
return p.buffer.DeleteBeforeCursorRunes(count, p.UserInputColumns(), p.renderer.row)
}
// Deletes the specified number of graphemes and returns the deleted text.
func (p *Prompt) Delete(count istrings.GraphemeNumber) string {
return p.buffer.Delete(count, p.UserInputColumns(), p.renderer.row)
}
// Deletes the specified number of runes and returns the deleted text.
func (p *Prompt) DeleteRunes(count istrings.RuneNumber) string {
return p.buffer.DeleteRunes(count, p.UserInputColumns(), p.renderer.row)
}
// Insert string into the buffer without moving the cursor.
func (p *Prompt) InsertText(text string, overwrite bool) {
p.buffer.InsertText(text, overwrite)
}
// Insert string into the buffer and move the cursor.
func (p *Prompt) InsertTextMoveCursor(text string, overwrite bool) {
p.buffer.InsertTextMoveCursor(text, p.UserInputColumns(), p.renderer.row, overwrite)
}
func (p *Prompt) Close() {
if !p.skipClose {
debug.AssertNoError(p.reader.Close())
}
p.renderer.Close()
}