-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtabmap.go
More file actions
159 lines (127 loc) · 3.4 KB
/
tabmap.go
File metadata and controls
159 lines (127 loc) · 3.4 KB
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
package readline
import "fmt"
func (rl *Instance) initTabMap() {
rl.tabMutex.Lock()
defer rl.tabMutex.Unlock()
var suggestions *suggestionsT
if rl.modeTabFind {
suggestions = newSuggestionsT(rl, rl.tfSuggestions)
} else {
suggestions = newSuggestionsT(rl, rl.tcSuggestions)
}
rl.tcMaxLength = 1
//for i := range suggestions {
for i := 0; i < suggestions.Len(); i++ {
if rl.tcDisplayType == TabDisplayList {
if suggestions.ItemLen(i) > rl.tcMaxLength {
rl.tcMaxLength = suggestions.ItemLen(i)
}
} else {
if len(rl.tcDescriptions[suggestions.ItemLookupValue(i)]) > rl.tcMaxLength {
rl.tcMaxLength = len(rl.tcDescriptions[suggestions.ItemLookupValue(i)])
}
}
}
rl.tcPosX = 1
rl.tcPosY = 1
rl.tcOffset = 0
rl.tcMaxX = 1
if suggestions.Len() > rl.MaxTabCompleterRows {
rl.tcMaxY = rl.MaxTabCompleterRows
} else {
rl.tcMaxY = suggestions.Len()
}
}
func (rl *Instance) moveTabMapHighlight(x, y int) {
rl.tabMutex.Lock()
defer rl.tabMutex.Unlock()
var suggestions *suggestionsT
if rl.modeTabFind {
suggestions = newSuggestionsT(rl, rl.tfSuggestions)
} else {
suggestions = newSuggestionsT(rl, rl.tcSuggestions)
}
rl.tcPosY += x
rl.tcPosY += y
if rl.tcPosY < 1 {
rl.tcPosY = 1
rl.tcOffset--
}
if rl.tcPosY > rl.tcMaxY {
rl.tcPosY--
rl.tcOffset++
}
if rl.tcOffset+rl.tcPosY < 1 && suggestions.Len() > 0 {
rl.tcPosY = rl.tcMaxY
rl.tcOffset = suggestions.Len() - rl.tcMaxY
}
if rl.tcOffset < 0 {
rl.tcOffset = 0
}
if rl.tcOffset+rl.tcPosY > suggestions.Len() {
rl.tcPosY = 1
rl.tcOffset = 0
}
}
func (rl *Instance) writeTabMapStr() string {
rl.tabMutex.Lock()
defer rl.tabMutex.Unlock()
var suggestions *suggestionsT
if rl.modeTabFind {
suggestions = newSuggestionsT(rl, rl.tfSuggestions)
} else {
suggestions = newSuggestionsT(rl, rl.tcSuggestions)
}
if rl.termWidth() < 10 {
// terminal too small. Probably better we do nothing instead of crash
return ""
}
maxLength := rl.tcMaxLength
if maxLength > rl.termWidth()-9 {
maxLength = rl.termWidth() - 9
}
maxDescWidth := rl.termWidth() - maxLength - 4
y := 0
rl.previewItem = ""
// bit of a kludge. Really should find where the code is "\n"ing
output := moveCursorUpStr(1)
isTabDisplayList := rl.tcDisplayType == TabDisplayList
var item, description string
for i := rl.tcOffset; i < suggestions.Len(); i++ {
y++
if y > rl.tcMaxY {
break
}
if isTabDisplayList {
item = runeWidthTruncate(suggestions.ItemValue(i), maxLength)
description = runeWidthTruncate(rl.tcDescriptions[suggestions.ItemLookupValue(i)], maxDescWidth)
} else {
item = runeWidthTruncate(suggestions.ItemValue(i), maxDescWidth)
description = runeWidthTruncate(rl.tcDescriptions[suggestions.ItemLookupValue(i)], maxLength)
}
if isTabDisplayList {
output += fmt.Sprintf("\r\n%s %s %s %s",
highlight(rl, y), runeWidthFillRight(item, maxLength),
seqReset, description)
} else {
output += fmt.Sprintf("\r\n %s %s %s %s",
runeWidthFillRight(description, maxLength), highlight(rl, y),
runeWidthFillRight(item, maxDescWidth), seqReset)
}
if y == rl.tcPosY {
rl.previewItem = suggestions.ItemValue(i)
}
}
if suggestions.Len() < rl.tcMaxX {
rl.tcUsedY.Store(int32(suggestions.Len()))
} else {
rl.tcUsedY.Store(int32(rl.tcMaxY))
}
return output
}
func highlight(rl *Instance, y int) string {
if y == rl.tcPosY {
return seqBgWhite + seqFgBlack
}
return ""
}