-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtabgrid.go
209 lines (166 loc) · 4.08 KB
/
tabgrid.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
package readline
import (
"fmt"
"strconv"
"github.com/mattn/go-runewidth"
)
func (rl *Instance) initTabGrid() {
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 = rl.MinTabItemLength
for i := 0; i < suggestions.Len(); i++ {
l := suggestions.ItemLen(i)
if l > rl.tcMaxLength {
rl.tcMaxLength = l
}
}
if rl.tcMaxLength > rl.MaxTabItemLength && rl.MaxTabItemLength > 0 && rl.MaxTabItemLength > rl.MinTabItemLength {
rl.tcMaxLength = rl.MaxTabItemLength
}
if rl.tcMaxLength == 0 {
rl.tcMaxLength = 20
}
rl.tcPosX = 1
rl.tcPosY = 1
rl.tcMaxX = rl.termWidth / (rl.tcMaxLength + 2)
rl.tcOffset = 0
// avoid a divide by zero error
if rl.tcMaxX < 1 {
rl.tcMaxX = 1
}
rl.tcMaxY = rl.MaxTabCompleterRows
// pre-cache
max := rl.tcMaxX * rl.tcMaxY
if max > len(rl.tcSuggestions) {
max = len(rl.tcSuggestions)
}
subset := rl.tcSuggestions[:max]
if rl.tcr.HintCache == nil {
return
}
go rl.tabHintCache(subset)
}
func (rl *Instance) tabHintCache(subset []string) {
hints := rl.tcr.HintCache(rl.tcPrefix, subset)
if len(hints) != len(subset) {
return
}
rl.tabMutex.Lock()
for i := range subset {
rl.tcDescriptions[subset[i]] = hints[i]
}
rl.tabMutex.Unlock()
}
func (rl *Instance) moveTabGridHighlight(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.tcPosX += x
rl.tcPosY += y
if rl.tcPosX < 1 {
rl.tcPosX = rl.tcMaxX
rl.tcPosY--
}
if rl.tcPosX > rl.tcMaxX {
rl.tcPosX = 1
rl.tcPosY++
}
if rl.tcPosY < 1 {
rl.tcPosY = rl.tcUsedY
}
if rl.tcPosY > rl.tcUsedY {
rl.tcPosY = 1
}
if rl.tcPosY == rl.tcUsedY && (rl.tcMaxX*(rl.tcPosY-1))+rl.tcPosX > suggestions.Len() {
if x < 0 {
rl.tcPosX = suggestions.Len() - (rl.tcMaxX * (rl.tcPosY - 1))
}
if x > 0 {
rl.tcPosX = 1
rl.tcPosY = 1
}
if y < 0 {
rl.tcPosY--
}
if y > 0 {
rl.tcPosY = 1
}
}
}
func (rl *Instance) writeTabGridStr() 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)
}
iCellWidth := (rl.termWidth / rl.tcMaxX) - 2
cellWidth := strconv.Itoa(iCellWidth)
x := 0
y := 1
rl.previewItem = ""
var output string
for i := 0; i < suggestions.Len(); i++ {
x++
if x > rl.tcMaxX {
x = 1
y++
if y > rl.tcMaxY {
y--
break
} else {
output += "\r\n"
}
}
if x == rl.tcPosX && y == rl.tcPosY {
output += seqBgWhite + seqFgBlack
rl.previewItem = suggestions.ItemValue(i)
}
value := suggestions.ItemValue(i)
caption := cropCaption(value, rl.tcMaxLength, iCellWidth)
if caption != value {
rl.tcDescriptions[suggestions.ItemLookupValue(i)] = value
}
output += fmt.Sprintf(" %-"+cellWidth+"s %s", caption, seqReset)
}
rl.tcUsedY = y
return output
}
func cropCaption(caption string, tcMaxLength int, iCellWidth int) string {
switch {
case iCellWidth == 0:
// this condition shouldn't ever happen but lets cover it just in case
return ""
case runewidth.StringWidth(caption) != len(caption):
// string length != rune width. So lets not do anything too clever
//return runewidth.Truncate(caption, iCellWidth, "…")
return runeWidthTruncate(caption, iCellWidth)
case len(caption) < tcMaxLength,
len(caption) < 5,
len(caption) <= iCellWidth:
return caption
case len(caption)-iCellWidth+6 < 1:
// truncate the end
return caption[:iCellWidth-1] + "…"
case len(caption) > 5+len(caption)-iCellWidth+6:
// truncate long lines in the middle
return caption[:5] + "…" + caption[len(caption)-iCellWidth+6:]
default:
// edge case reached. lets truncate the most conservative way we can,
// just in case
return runewidth.Truncate(caption, iCellWidth, "…")
}
}