Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
kardolus
GitHub Repository: kardolus/chatgpt-cli
Path: blob/main/vendor/github.com/chzyer/readline/operation.go
2875 views
1
package readline
2
3
import (
4
"errors"
5
"io"
6
"sync"
7
)
8
9
var (
10
ErrInterrupt = errors.New("Interrupt")
11
)
12
13
type InterruptError struct {
14
Line []rune
15
}
16
17
func (*InterruptError) Error() string {
18
return "Interrupted"
19
}
20
21
type Operation struct {
22
m sync.Mutex
23
cfg *Config
24
t *Terminal
25
buf *RuneBuffer
26
outchan chan []rune
27
errchan chan error
28
w io.Writer
29
30
history *opHistory
31
*opSearch
32
*opCompleter
33
*opPassword
34
*opVim
35
}
36
37
func (o *Operation) SetBuffer(what string) {
38
o.buf.Set([]rune(what))
39
}
40
41
type wrapWriter struct {
42
r *Operation
43
t *Terminal
44
target io.Writer
45
}
46
47
func (w *wrapWriter) Write(b []byte) (int, error) {
48
if !w.t.IsReading() {
49
return w.target.Write(b)
50
}
51
52
var (
53
n int
54
err error
55
)
56
w.r.buf.Refresh(func() {
57
n, err = w.target.Write(b)
58
})
59
60
if w.r.IsSearchMode() {
61
w.r.SearchRefresh(-1)
62
}
63
if w.r.IsInCompleteMode() {
64
w.r.CompleteRefresh()
65
}
66
return n, err
67
}
68
69
func NewOperation(t *Terminal, cfg *Config) *Operation {
70
width := cfg.FuncGetWidth()
71
op := &Operation{
72
t: t,
73
buf: NewRuneBuffer(t, cfg.Prompt, cfg, width),
74
outchan: make(chan []rune),
75
errchan: make(chan error, 1),
76
}
77
op.w = op.buf.w
78
op.SetConfig(cfg)
79
op.opVim = newVimMode(op)
80
op.opCompleter = newOpCompleter(op.buf.w, op, width)
81
op.opPassword = newOpPassword(op)
82
op.cfg.FuncOnWidthChanged(func() {
83
newWidth := cfg.FuncGetWidth()
84
op.opCompleter.OnWidthChange(newWidth)
85
op.opSearch.OnWidthChange(newWidth)
86
op.buf.OnWidthChange(newWidth)
87
})
88
go op.ioloop()
89
return op
90
}
91
92
func (o *Operation) SetPrompt(s string) {
93
o.buf.SetPrompt(s)
94
}
95
96
func (o *Operation) SetMaskRune(r rune) {
97
o.buf.SetMask(r)
98
}
99
100
func (o *Operation) GetConfig() *Config {
101
o.m.Lock()
102
cfg := *o.cfg
103
o.m.Unlock()
104
return &cfg
105
}
106
107
func (o *Operation) ioloop() {
108
for {
109
keepInSearchMode := false
110
keepInCompleteMode := false
111
r := o.t.ReadRune()
112
113
if o.GetConfig().FuncFilterInputRune != nil {
114
var process bool
115
r, process = o.GetConfig().FuncFilterInputRune(r)
116
if !process {
117
o.t.KickRead()
118
o.buf.Refresh(nil) // to refresh the line
119
continue // ignore this rune
120
}
121
}
122
123
if r == 0 { // io.EOF
124
if o.buf.Len() == 0 {
125
o.buf.Clean()
126
select {
127
case o.errchan <- io.EOF:
128
}
129
break
130
} else {
131
// if stdin got io.EOF and there is something left in buffer,
132
// let's flush them by sending CharEnter.
133
// And we will got io.EOF int next loop.
134
r = CharEnter
135
}
136
}
137
isUpdateHistory := true
138
139
if o.IsInCompleteSelectMode() {
140
keepInCompleteMode = o.HandleCompleteSelect(r)
141
if keepInCompleteMode {
142
continue
143
}
144
145
o.buf.Refresh(nil)
146
switch r {
147
case CharEnter, CharCtrlJ:
148
o.history.Update(o.buf.Runes(), false)
149
fallthrough
150
case CharInterrupt:
151
o.t.KickRead()
152
fallthrough
153
case CharBell:
154
continue
155
}
156
}
157
158
if o.IsEnableVimMode() {
159
r = o.HandleVim(r, o.t.ReadRune)
160
if r == 0 {
161
continue
162
}
163
}
164
165
switch r {
166
case CharBell:
167
if o.IsSearchMode() {
168
o.ExitSearchMode(true)
169
o.buf.Refresh(nil)
170
}
171
if o.IsInCompleteMode() {
172
o.ExitCompleteMode(true)
173
o.buf.Refresh(nil)
174
}
175
case CharTab:
176
if o.GetConfig().AutoComplete == nil {
177
o.t.Bell()
178
break
179
}
180
if o.OnComplete() {
181
keepInCompleteMode = true
182
} else {
183
o.t.Bell()
184
break
185
}
186
187
case CharBckSearch:
188
if !o.SearchMode(S_DIR_BCK) {
189
o.t.Bell()
190
break
191
}
192
keepInSearchMode = true
193
case CharCtrlU:
194
o.buf.KillFront()
195
case CharFwdSearch:
196
if !o.SearchMode(S_DIR_FWD) {
197
o.t.Bell()
198
break
199
}
200
keepInSearchMode = true
201
case CharKill:
202
o.buf.Kill()
203
keepInCompleteMode = true
204
case MetaForward:
205
o.buf.MoveToNextWord()
206
case CharTranspose:
207
o.buf.Transpose()
208
case MetaBackward:
209
o.buf.MoveToPrevWord()
210
case MetaDelete:
211
o.buf.DeleteWord()
212
case CharLineStart:
213
o.buf.MoveToLineStart()
214
case CharLineEnd:
215
o.buf.MoveToLineEnd()
216
case CharBackspace, CharCtrlH:
217
if o.IsSearchMode() {
218
o.SearchBackspace()
219
keepInSearchMode = true
220
break
221
}
222
223
if o.buf.Len() == 0 {
224
o.t.Bell()
225
break
226
}
227
o.buf.Backspace()
228
if o.IsInCompleteMode() {
229
o.OnComplete()
230
}
231
case CharCtrlZ:
232
o.buf.Clean()
233
o.t.SleepToResume()
234
o.Refresh()
235
case CharCtrlL:
236
ClearScreen(o.w)
237
o.Refresh()
238
case MetaBackspace, CharCtrlW:
239
o.buf.BackEscapeWord()
240
case CharCtrlY:
241
o.buf.Yank()
242
case CharEnter, CharCtrlJ:
243
if o.IsSearchMode() {
244
o.ExitSearchMode(false)
245
}
246
o.buf.MoveToLineEnd()
247
var data []rune
248
if !o.GetConfig().UniqueEditLine {
249
o.buf.WriteRune('\n')
250
data = o.buf.Reset()
251
data = data[:len(data)-1] // trim \n
252
} else {
253
o.buf.Clean()
254
data = o.buf.Reset()
255
}
256
o.outchan <- data
257
if !o.GetConfig().DisableAutoSaveHistory {
258
// ignore IO error
259
_ = o.history.New(data)
260
} else {
261
isUpdateHistory = false
262
}
263
case CharBackward:
264
o.buf.MoveBackward()
265
case CharForward:
266
o.buf.MoveForward()
267
case CharPrev:
268
buf := o.history.Prev()
269
if buf != nil {
270
o.buf.Set(buf)
271
} else {
272
o.t.Bell()
273
}
274
case CharNext:
275
buf, ok := o.history.Next()
276
if ok {
277
o.buf.Set(buf)
278
} else {
279
o.t.Bell()
280
}
281
case CharDelete:
282
if o.buf.Len() > 0 || !o.IsNormalMode() {
283
o.t.KickRead()
284
if !o.buf.Delete() {
285
o.t.Bell()
286
}
287
break
288
}
289
290
// treat as EOF
291
if !o.GetConfig().UniqueEditLine {
292
o.buf.WriteString(o.GetConfig().EOFPrompt + "\n")
293
}
294
o.buf.Reset()
295
isUpdateHistory = false
296
o.history.Revert()
297
o.errchan <- io.EOF
298
if o.GetConfig().UniqueEditLine {
299
o.buf.Clean()
300
}
301
case CharInterrupt:
302
if o.IsSearchMode() {
303
o.t.KickRead()
304
o.ExitSearchMode(true)
305
break
306
}
307
if o.IsInCompleteMode() {
308
o.t.KickRead()
309
o.ExitCompleteMode(true)
310
o.buf.Refresh(nil)
311
break
312
}
313
o.buf.MoveToLineEnd()
314
o.buf.Refresh(nil)
315
hint := o.GetConfig().InterruptPrompt + "\n"
316
if !o.GetConfig().UniqueEditLine {
317
o.buf.WriteString(hint)
318
}
319
remain := o.buf.Reset()
320
if !o.GetConfig().UniqueEditLine {
321
remain = remain[:len(remain)-len([]rune(hint))]
322
}
323
isUpdateHistory = false
324
o.history.Revert()
325
o.errchan <- &InterruptError{remain}
326
default:
327
if o.IsSearchMode() {
328
o.SearchChar(r)
329
keepInSearchMode = true
330
break
331
}
332
o.buf.WriteRune(r)
333
if o.IsInCompleteMode() {
334
o.OnComplete()
335
keepInCompleteMode = true
336
}
337
}
338
339
listener := o.GetConfig().Listener
340
if listener != nil {
341
newLine, newPos, ok := listener.OnChange(o.buf.Runes(), o.buf.Pos(), r)
342
if ok {
343
o.buf.SetWithIdx(newPos, newLine)
344
}
345
}
346
347
o.m.Lock()
348
if !keepInSearchMode && o.IsSearchMode() {
349
o.ExitSearchMode(false)
350
o.buf.Refresh(nil)
351
} else if o.IsInCompleteMode() {
352
if !keepInCompleteMode {
353
o.ExitCompleteMode(false)
354
o.Refresh()
355
} else {
356
o.buf.Refresh(nil)
357
o.CompleteRefresh()
358
}
359
}
360
if isUpdateHistory && !o.IsSearchMode() {
361
// it will cause null history
362
o.history.Update(o.buf.Runes(), false)
363
}
364
o.m.Unlock()
365
}
366
}
367
368
func (o *Operation) Stderr() io.Writer {
369
return &wrapWriter{target: o.GetConfig().Stderr, r: o, t: o.t}
370
}
371
372
func (o *Operation) Stdout() io.Writer {
373
return &wrapWriter{target: o.GetConfig().Stdout, r: o, t: o.t}
374
}
375
376
func (o *Operation) String() (string, error) {
377
r, err := o.Runes()
378
return string(r), err
379
}
380
381
func (o *Operation) Runes() ([]rune, error) {
382
o.t.EnterRawMode()
383
defer o.t.ExitRawMode()
384
385
listener := o.GetConfig().Listener
386
if listener != nil {
387
listener.OnChange(nil, 0, 0)
388
}
389
390
o.buf.Refresh(nil) // print prompt
391
o.t.KickRead()
392
select {
393
case r := <-o.outchan:
394
return r, nil
395
case err := <-o.errchan:
396
if e, ok := err.(*InterruptError); ok {
397
return e.Line, ErrInterrupt
398
}
399
return nil, err
400
}
401
}
402
403
func (o *Operation) PasswordEx(prompt string, l Listener) ([]byte, error) {
404
cfg := o.GenPasswordConfig()
405
cfg.Prompt = prompt
406
cfg.Listener = l
407
return o.PasswordWithConfig(cfg)
408
}
409
410
func (o *Operation) GenPasswordConfig() *Config {
411
return o.opPassword.PasswordConfig()
412
}
413
414
func (o *Operation) PasswordWithConfig(cfg *Config) ([]byte, error) {
415
if err := o.opPassword.EnterPasswordMode(cfg); err != nil {
416
return nil, err
417
}
418
defer o.opPassword.ExitPasswordMode()
419
return o.Slice()
420
}
421
422
func (o *Operation) Password(prompt string) ([]byte, error) {
423
return o.PasswordEx(prompt, nil)
424
}
425
426
func (o *Operation) SetTitle(t string) {
427
o.w.Write([]byte("\033[2;" + t + "\007"))
428
}
429
430
func (o *Operation) Slice() ([]byte, error) {
431
r, err := o.Runes()
432
if err != nil {
433
return nil, err
434
}
435
return []byte(string(r)), nil
436
}
437
438
func (o *Operation) Close() {
439
select {
440
case o.errchan <- io.EOF:
441
default:
442
}
443
o.history.Close()
444
}
445
446
func (o *Operation) SetHistoryPath(path string) {
447
if o.history != nil {
448
o.history.Close()
449
}
450
o.cfg.HistoryFile = path
451
o.history = newOpHistory(o.cfg)
452
}
453
454
func (o *Operation) IsNormalMode() bool {
455
return !o.IsInCompleteMode() && !o.IsSearchMode()
456
}
457
458
func (op *Operation) SetConfig(cfg *Config) (*Config, error) {
459
op.m.Lock()
460
defer op.m.Unlock()
461
if op.cfg == cfg {
462
return op.cfg, nil
463
}
464
if err := cfg.Init(); err != nil {
465
return op.cfg, err
466
}
467
old := op.cfg
468
op.cfg = cfg
469
op.SetPrompt(cfg.Prompt)
470
op.SetMaskRune(cfg.MaskRune)
471
op.buf.SetConfig(cfg)
472
width := op.cfg.FuncGetWidth()
473
474
if cfg.opHistory == nil {
475
op.SetHistoryPath(cfg.HistoryFile)
476
cfg.opHistory = op.history
477
cfg.opSearch = newOpSearch(op.buf.w, op.buf, op.history, cfg, width)
478
}
479
op.history = cfg.opHistory
480
481
// SetHistoryPath will close opHistory which already exists
482
// so if we use it next time, we need to reopen it by `InitHistory()`
483
op.history.Init()
484
485
if op.cfg.AutoComplete != nil {
486
op.opCompleter = newOpCompleter(op.buf.w, op, width)
487
}
488
489
op.opSearch = cfg.opSearch
490
return old, nil
491
}
492
493
func (o *Operation) ResetHistory() {
494
o.history.Reset()
495
}
496
497
// if err is not nil, it just mean it fail to write to file
498
// other things goes fine.
499
func (o *Operation) SaveHistory(content string) error {
500
return o.history.New([]rune(content))
501
}
502
503
func (o *Operation) Refresh() {
504
if o.t.IsReading() {
505
o.buf.Refresh(nil)
506
}
507
}
508
509
func (o *Operation) Clean() {
510
o.buf.Clean()
511
}
512
513
func FuncListener(f func(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool)) Listener {
514
return &DumpListener{f: f}
515
}
516
517
type DumpListener struct {
518
f func(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool)
519
}
520
521
func (d *DumpListener) OnChange(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool) {
522
return d.f(line, pos, key)
523
}
524
525
type Listener interface {
526
OnChange(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool)
527
}
528
529
type Painter interface {
530
Paint(line []rune, pos int) []rune
531
}
532
533
type defaultPainter struct{}
534
535
func (p *defaultPainter) Paint(line []rune, _ int) []rune {
536
return line
537
}
538
539