forked from lmorg/readline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtab.go
115 lines (93 loc) · 2.78 KB
/
tab.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
package readline
import (
"context"
)
// TabDisplayType defines how the autocomplete suggestions display
type TabDisplayType int
const (
// TabDisplayGrid is the default. It's where the screen below the prompt is
// divided into a grid with each suggestion occupying an individual cell.
TabDisplayGrid = iota
// TabDisplayList is where suggestions are displayed as a list with a
// description. The suggestion gets highlighted but both are searchable (ctrl+f)
TabDisplayList
// TabDisplayMap is where suggestions are displayed as a list with a
// description however the description is what gets highlighted and only
// that is searchable (ctrl+f). The benefit of TabDisplayMap is when your
// autocomplete suggestions are IDs rather than human terms.
TabDisplayMap
)
func (rl *Instance) getTabCompletion() {
rl.tcOffset = 0
if rl.TabCompleter == nil {
return
}
if rl.delayedTabContext.cancel != nil {
rl.delayedTabContext.cancel()
}
rl.delayedTabContext = DelayedTabContext{rl: rl}
rl.delayedTabContext.Context, rl.delayedTabContext.cancel = context.WithCancel(context.Background())
rl.tcPrefix, rl.tcSuggestions, rl.tcDescriptions, rl.tcDisplayType = rl.TabCompleter(rl.line, rl.pos, rl.delayedTabContext)
/*if len(rl.tcSuggestions) == 0 && delayed {
return
}*/
//panic(rl.tcDisplayType)
if len(rl.tcDescriptions) == 0 {
// probably not needed, but just in case someone doesn't initialise the
// map in their API call.
rl.tcDescriptions = make(map[string]string)
}
/*if len(rl.tcSuggestions) == 1 && !rl.modeTabCompletion {
if len(rl.tcSuggestions[0]) == 0 || rl.tcSuggestions[0] == " " || rl.tcSuggestions[0] == "\t" {
return
}
rl.insert([]byte(rl.tcSuggestions[0]))
return
}*/
rl.initTabCompletion()
}
func (rl *Instance) initTabCompletion() {
if rl.tcDisplayType == TabDisplayGrid {
rl.initTabGrid()
} else {
rl.initTabMap()
}
}
func (rl *Instance) moveTabCompletionHighlight(x, y int) {
if rl.tcDisplayType == TabDisplayGrid {
rl.moveTabGridHighlight(x, y)
} else {
rl.moveTabMapHighlight(x, y)
}
}
func (rl *Instance) writeTabCompletion(resetCursorPos bool) {
if !rl.modeTabCompletion {
return
}
_, posY := lineWrapPos(rl.promptLen, rl.pos, rl.termWidth)
_, lineY := lineWrapPos(rl.promptLen, len(rl.line), rl.termWidth)
moveCursorDown(rl.hintY + lineY - posY)
print("\r\n" + seqClearScreenBelow)
switch rl.tcDisplayType {
case TabDisplayGrid:
rl.writeTabGrid()
case TabDisplayMap:
rl.writeTabMap()
case TabDisplayList:
rl.writeTabMap()
default:
rl.writeTabGrid()
}
if resetCursorPos {
moveCursorUp(rl.hintY + rl.tcUsedY)
print("\r")
rl.moveCursorFromStartToLinePos()
}
}
func (rl *Instance) resetTabCompletion() {
rl.modeTabCompletion = false
rl.tcOffset = 0
rl.tcUsedY = 0
rl.modeTabFind = false
rl.tfLine = []rune{}
}