forked from reeflective/readline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautosuggestion.go
65 lines (54 loc) · 1.44 KB
/
autosuggestion.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
package readline
// autosuggestHistory returns the remainder of a
// history line if one matches the current input line.
func (rl *Instance) autosuggestHistory(line []rune) {
rl.histSuggested = make([]rune, 0)
// Don't mess with completion history indexes
oldSourcePos := rl.historySourcePos
rl.historySourcePos = 0
history := rl.currentHistory()
rl.historySourcePos = oldSourcePos
// Nothing happens if the history is nil or empty.
if history == nil || history.Len() == 0 {
return
}
for i := 1; i <= history.Len(); i++ {
histline, err := history.GetLine(history.Len() - i)
if err != nil {
return
}
// If too short
if len(histline) <= len(line) {
continue
}
// Or if not fully matching
match := false
for i, char := range line {
if byte(char) == histline[i] {
match = true
} else {
match = false
break
}
}
// If the line fully matches, we have our suggestion
if match {
rl.histSuggested = append(rl.histSuggested, []rune(histline[len(line):])...)
return
}
}
}
// historyAutosuggestInsert inserts the currently autosuggested history line.
// This does nothing if history autosuggestion is not enabled in the config.
func (rl *Instance) historyAutosuggestInsert() {
if !rl.config.HistoryAutosuggest {
return
}
if len(rl.histSuggested) == 0 {
return
}
rl.undoHistoryAppend()
rl.line = append(rl.line, rl.histSuggested...)
rl.pos = len(rl.line) - 1
rl.histSuggested = []rune{}
}