-
Notifications
You must be signed in to change notification settings - Fork 3
/
tabgrid.go
153 lines (126 loc) · 2.88 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
package readline
import (
"sort"
"strconv"
)
func (rl *Instance) initTabGrid() {
var suggestions []string
if rl.modeTabFind {
suggestions = rl.tfSuggestions
} else {
suggestions = rl.tcSuggestions
}
sort.Strings(suggestions) // I don't like doing this here
rl.tcMaxLength = rl.MinTabItemLength
for i := range suggestions {
if len(rl.tcPrefix+suggestions[i]) > rl.tcMaxLength {
rl.tcMaxLength = len([]rune(rl.tcPrefix + suggestions[i]))
}
}
if rl.tcMaxLength > rl.MaxTabItemLength && rl.MaxTabItemLength > 0 && rl.MaxTabItemLength > rl.MinTabItemLength {
rl.tcMaxLength = rl.MaxTabItemLength
}
if rl.tcMaxLength == 0 {
rl.tcMaxLength = 20
}
rl.modeTabCompletion = true
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
}
func (rl *Instance) moveTabGridHighlight(x, y int) {
var suggestions []string
if rl.modeTabFind {
suggestions = rl.tfSuggestions
} else {
suggestions = 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 > len(suggestions) {
if x < 0 {
rl.tcPosX = len(suggestions) - (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) writeTabGrid() {
var suggestions []string
if rl.modeTabFind {
suggestions = rl.tfSuggestions
} else {
suggestions = rl.tcSuggestions
}
//print("\r" + strings.Repeat("\n", rl.hintY) + seqClearScreenBelow)
iCellWidth := (rl.termWidth / rl.tcMaxX) - 2
cellWidth := strconv.Itoa(iCellWidth)
x := 0
y := 1
for i := range suggestions {
x++
if x > rl.tcMaxX {
x = 1
y++
if y > rl.tcMaxY {
y--
break
} else {
print("\r\n")
}
}
if x == rl.tcPosX && y == rl.tcPosY {
print(seqBgWhite + seqFgBlack)
}
caption := cropCaption(rl.tcPrefix+suggestions[i], rl.tcMaxLength, iCellWidth)
printf(" %-"+cellWidth+"s %s", caption, seqReset)
}
rl.tcUsedY = y
}
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 len(caption) < tcMaxLength:
return caption
case len(caption) < 5:
return caption
case len(caption) <= iCellWidth:
return caption
case len(caption)-iCellWidth+6 < 1:
return caption[:iCellWidth-1] + "…"
case len(caption) > 5+len(caption)-iCellWidth+6:
return caption[:4] + "…" + caption[len(caption)-iCellWidth+6:]
default:
return caption[:iCellWidth-1] + "…"
}
}