forked from reeflective/readline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.go
431 lines (352 loc) · 10.3 KB
/
history.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
package readline
import (
"bufio"
"errors"
"fmt"
"os"
"strconv"
"strings"
)
// History is an interface to allow you to write your own history logging
// tools. eg sqlite backend instead of a file system.
// By default readline will just use the dummyLineHistory interface which only
// logs the history to memory ([]string to be precise).
type History interface {
// Append takes the line and returns an updated number of lines or an error
Write(string) (int, error)
// GetLine takes the historic line number and returns the line or an error
GetLine(int) (string, error)
// Len returns the number of history lines
Len() int
// Dump returns everything in readline. The return is an interface{} because
// not all LineHistory implementations will want to structure the history in
// the same way. And since Dump() is not actually used by the readline API
// internally, this methods return can be structured in whichever way is most
// convenient for your own applications (or even just create an empty
// function which returns `nil` if you don't require Dump() either)
Dump() interface{}
}
// AddHistorySource adds a source of history lines bound to a given name
// (printed above this source when used). All history sources can be used
// when completing history, and repeatedly using the completion key will
// cycle through them.
// When only the default in-memory history is bound, it is replaced with
// the provided ones. All following sources are added to the list.
func (rl *Instance) AddHistorySource(name string, history History) {
if len(rl.histories) == 1 && rl.historyNames[0] == "local history" {
delete(rl.histories, "local history")
rl.historyNames = make([]string, 0)
}
rl.historyNames = append(rl.historyNames, name)
rl.histories[name] = history
}
// New creates a new History populated from, and writing to
// a file path passed as parameter.
func NewHistoryFromFile(filename string) (History, error) {
h := new(fileHistory)
h.filename = filename
h.list, _ = openHist(filename)
return h, nil
}
// GetLine returns a specific line from the history file.
func (h *fileHistory) GetLine(i int) (string, error) {
if i < 0 {
return "", errors.New("cannot use a negative index when requesting historic commands")
}
if i < len(h.list) {
return h.list[i], nil
}
return "", errors.New("index requested greater than number of items in history")
}
// Len returns the number of items in the history file.
func (h *fileHistory) Len() int {
return len(h.list)
}
// Dump returns the entire history file.
func (h *fileHistory) Dump() interface{} {
return h.list
}
// DeleteHistorySource deletes one or more history source by name.
// If no arguments are passed, all currently bound sources are removed.
func (rl *Instance) DeleteHistorySource(sources ...string) {
defer rl.initHistory()
if len(sources) == 0 {
rl.histories = make(map[string]History)
rl.historyNames = make([]string, 0)
return
}
for _, name := range sources {
delete(rl.histories, name)
for i, hname := range rl.historyNames {
if hname == name {
rl.historyNames = append(rl.historyNames[:i], rl.historyNames[i+1:]...)
break
}
}
}
}
// MemoryHistory is an in memory history.
// One such history is bound to the readline shell by default.
type MemoryHistory struct {
items []string
}
// Write to history.
func (h *MemoryHistory) Write(s string) (int, error) {
h.items = append(h.items, s)
return len(h.items), nil
}
// GetLine returns a line from history.
func (h *MemoryHistory) GetLine(i int) (string, error) {
return h.items[i], nil
}
// Len returns the number of lines in history.
func (h *MemoryHistory) Len() int {
return len(h.items)
}
// Dump returns the entire history.
func (h *MemoryHistory) Dump() interface{} {
return h.items
}
// NullHistory is a null History interface for when you don't want line
// entries remembered eg password input.
type NullHistory struct{}
// Write to history.
func (h *NullHistory) Write(s string) (int, error) {
return 0, nil
}
// GetLine returns a line from history.
func (h *NullHistory) GetLine(i int) (string, error) {
return "", nil
}
// Len returns the number of lines in history.
func (h *NullHistory) Len() int {
return 0
}
// Dump returns the entire history.
func (h *NullHistory) Dump() interface{} {
return []string{}
}
// initHistory is ran once at the beginning of an instance start.
func (rl *Instance) initHistory() {
rl.historySourcePos = 0
rl.undoHistory = []undoItem{{line: "", pos: 0}}
// Only reset the history position when we don't
// need it to retrieve a line before anything else.
if !rl.inferLine {
rl.histPos = 0
}
}
// when the last widget invoked accepted a line with a supplementary
// directive to retrieve a history line (by match or index), find it.
func (rl *Instance) initHistoryLine() {
if !rl.inferLine {
return
}
switch rl.histPos {
case -1:
rl.histPos = 0
case 0:
rl.inferNextHistory()
default:
rl.walkHistory(-1)
}
rl.inferLine = false
}
func (rl *Instance) nextHistorySource() {
rl.historySourcePos++
if rl.historySourcePos == len(rl.historyNames) {
rl.historySourcePos = 0
}
}
func (rl *Instance) prevHistorySource() {
rl.historySourcePos--
if rl.historySourcePos < 0 {
rl.historySourcePos = len(rl.historyNames) - 1
}
}
func (rl *Instance) currentHistory() History {
if len(rl.histories) == 0 {
return nil
}
return rl.histories[rl.historyNames[rl.historySourcePos]]
}
// walkHistory - Browse historic lines.
func (rl *Instance) walkHistory(pos int) {
var (
next string
err error
)
// Always use the main/first history.
rl.historySourcePos = 0
history := rl.currentHistory()
if history == nil || history.Len() == 0 {
return
}
// When we are exiting the current line buffer to move around
// the history, we make buffer the current line
if rl.histPos == 0 && (rl.histPos+pos) == 1 {
rl.lineBuf = string(rl.line)
}
rl.histPos += pos
switch {
case rl.histPos > history.Len():
// The history is greater than the length of history: maintain
// it at the last index, to keep the same line in the buffer.
rl.histPos = history.Len()
case rl.histPos < 0:
// We can never go lower than the last history line, which is our current line.
rl.histPos = 0
case rl.histPos == 0:
// The 0 index is our current line
rl.line = []rune(rl.lineBuf)
rl.pos = len(rl.lineBuf)
}
// We now have the correct history index. Use it to find the history line.
// If the history position is not zero, we need to use a history line.
if rl.histPos > 0 {
next, err = history.GetLine(history.Len() - rl.histPos)
if err != nil {
rl.resetHelpers()
print("\r\n" + err.Error() + "\r\n")
// NOTE: Same here ? Should we print the prompt more properly ?
print(rl.Prompt.primary)
return
}
rl.clearLine()
rl.line = []rune(next)
rl.pos = len(rl.line)
}
}
// historySearchLine inserts the first line in the main history
// that matches the current input line as a prefix.
func (rl *Instance) historySearchLine(forward bool) {
if len(rl.histories) == 0 {
return
}
history := rl.currentHistory()
if history == nil {
return
}
// Set up iteration clauses
var histPos int
var done func(i int) bool
var move func(inc int) int
if forward {
histPos = -1
done = func(i int) bool { return i < history.Len() }
move = func(pos int) int { return pos + 1 }
} else {
histPos = history.Len()
done = func(i int) bool { return i > 0 }
move = func(pos int) int { return pos - 1 }
}
for done(histPos) {
histPos = move(histPos)
histline, err := history.GetLine(histPos)
if err != nil {
return
}
// If too short
if len(histline) < len(rl.line) {
continue
}
// Or if not fully matching
if !strings.HasPrefix(string(rl.line), string(histline)) {
continue
}
// Else we have our new history index position.
rl.histPos = histPos
rl.lineBuf = string(rl.line)
rl.line = []rune(rl.lineBuf)
}
}
// completeHistory - Populates a CompletionGroup with history and returns it the shell
// we populate only one group, so as to pass it to the main completion engine.
func (rl *Instance) completeHistory(forward bool) Completions {
if len(rl.histories) == 0 {
return Completions{}
}
history := rl.currentHistory()
if history == nil {
return Completions{}
}
rl.histHint = []rune(rl.historyNames[rl.historySourcePos])
// Set the hint line with everything
rl.histHint = []rune(seqBold + seqFgCyanBright + string(rl.histHint) + seqReset)
compLines := make([]Completion, 0)
// Set up iteration clauses
var histPos int
var done func(i int) bool
var move func(inc int) int
if forward {
histPos = -1
done = func(i int) bool { return i < history.Len() }
move = func(pos int) int { return pos + 1 }
} else {
histPos = history.Len()
done = func(i int) bool { return i > 0 }
move = func(pos int) int { return pos - 1 }
}
// And generate the completions.
nextLine:
for done(histPos) {
histPos = move(histPos)
line, err := history.GetLine(histPos)
if err != nil {
continue
}
if !strings.HasPrefix(line, rl.tcPrefix) || strings.TrimSpace(line) == "" {
continue
}
line = strings.ReplaceAll(line, "\n", ` `)
for _, comp := range compLines {
if comp.Display == line {
continue nextLine
}
}
// Proper pad for indexes
indexStr := strconv.Itoa(histPos)
pad := strings.Repeat(" ", len(strconv.Itoa(history.Len()))-len(indexStr))
display := fmt.Sprintf("%s%s %s%s", seqDim, indexStr+pad, seqDimReset, line)
value := Completion{
Display: display,
Value: line,
}
compLines = append(compLines, value)
}
comps := CompleteRaw(compLines)
comps = comps.NoSort()
comps.PREFIX = string(rl.line)
return comps
}
// fileHistory provides a history source based on a file.
type fileHistory struct {
filename string
list []string
}
func openHist(filename string) (list []string, err error) {
file, err := os.Open(filename)
if err != nil {
return list, err
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
list = append(list, scanner.Text())
}
file.Close()
return list, nil
}
// Write item to history file. eg ~/.murex_history.
func (h *fileHistory) Write(s string) (int, error) {
block := strings.TrimSpace(s)
if len(h.list) == 0 || h.list[len(h.list)-1] != block {
h.list = append(h.list, block)
}
f, err := os.OpenFile(h.filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o600)
if err != nil {
return 0, err
}
_, err = f.WriteString(block + "\n")
f.Close()
return h.Len(), err
}