-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.go
208 lines (178 loc) · 4.68 KB
/
render.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
package chooser
import (
"fmt"
"io"
"strings"
)
const (
fixedDisplayAreaHeight = 3
)
type prompt struct {
text string
length int
}
func newPrompt(text string) prompt {
return prompt{
text: fmt.Sprintf("\x1b[1;34m%s\x1b[m ", text),
length: len(text) + 1,
}
}
type symbol struct {
normal string
cursor string
selected string
length int
}
func newSymbol() symbol {
return symbol{normal: " ", cursor: "> ", selected: "* ", length: 2}
}
type format struct {
normal string
cursor string
selected string
}
func newFormat() format {
return format{normal: "%s", cursor: "\x1b[1;37m%s\x1b[m", selected: "\x1b[1;34m%s\x1b[m"}
}
type render struct {
buffer *buffer
completion *completion
// The starting index of display suggestions.
// If The number of lines suggestions is larger than the height of the screen.
startingIndex int
winSize *winSize
heldCompletions []int
out io.Writer
headerFormat string
symbol symbol
format format
prompt prompt
}
func newRender(out io.Writer, len int) *render {
return &render{
buffer: newBuffer(),
startingIndex: 0,
heldCompletions: make([]int, 0),
out: out,
headerFormat: fmt.Sprintf("\x1b[32m%%d/%d (%%d)\x1b[m", len),
symbol: newSymbol(),
format: newFormat(),
prompt: newPrompt(">>>"),
}
}
func (r *render) clearScreen() {
r.print(cursorForward(1) + clearCursorEnd())
}
func (r *render) restoreCursorPosition(row, col int) {
r.print(cursorUp(row) + cursorForward(col))
}
func (r *render) print(i ...interface{}) {
fmt.Fprint(r.out, i...)
}
func (r *render) render(lines []string) {
r.clearScreen()
lines = append([]string{r.inputField(), r.header()}, lines...)
r.print(strings.Join(lines, "\n"))
r.restoreCursorPosition(len(lines)-1, r.cursorColPosition())
}
func (r *render) renderSuggestions() {
var suggestionsToDisplay []string
for i := r.startingIndex; i < r.endingIndex() && i < r.completion.length(); i++ {
suggestionsToDisplay = append(suggestionsToDisplay,
r.assignSymbol(i)+fmt.Sprintf(r.assignFormat(i), r.shortenLine(r.completion.suggestions[i])))
}
r.render(suggestionsToDisplay)
}
func (r *render) renderKeyBindings(isMultiple bool) {
var keyBindings []string
for _, v := range bufferKeyBindingCmds {
keyBindings = append(keyBindings,
r.shortenLine(fmt.Sprintf("%s: %s", v.key, v.description)))
}
for _, v := range renderKeyBindingCmds {
if isMultiple && v.key == "tab" {
continue
}
keyBindings = append(keyBindings,
r.shortenLine(fmt.Sprintf("%s: %s", v.key, v.description)))
}
for _, v := range keyBindingCmds {
keyBindings = append(keyBindings,
r.shortenLine(fmt.Sprintf("%s: %s", v.key, v.description)))
}
r.render(keyBindings)
}
func (r *render) next() {
r.completion.next()
if r.endingIndex() <= r.completion.target {
r.startingIndex += 1
}
}
func (r *render) previous() {
r.completion.previous()
if r.completion.target < r.startingIndex {
r.startingIndex -= 1
}
}
// The ending index of display suggestions.
func (r *render) endingIndex() int {
return r.startingIndex + int(r.winSize.row) - fixedDisplayAreaHeight
}
func (r *render) inputField() string {
return r.prompt.text + r.buffer.text
}
func (r *render) header() string {
if len(r.heldCompletions) > 0 {
return fmt.Sprintf(r.headerFormat, r.completion.length(), len(r.heldCompletions))
}
return fmt.Sprintf(r.headerFormat, r.completion.length(), 0)
}
func (r *render) shortenLine(line string) string {
displayableWidth := int(r.winSize.col) - r.symbol.length
runes := []rune(line)
if len(runes) <= displayableWidth {
return line
}
return string(runes[:displayableWidth:displayableWidth])
}
func (r *render) cursorColPosition() int {
return r.buffer.cursorPosition + r.prompt.length + 1
}
// cursorSymbol is the highest priority.
func (r *render) assignSymbol(i int) string {
if r.completion.target == i {
return r.symbol.cursor
}
for _, v := range r.heldCompletions {
if v == r.completion.indexes[i] {
return r.symbol.selected
}
}
return r.symbol.normal
}
// selectedFormat is the highest priority.
func (r *render) assignFormat(i int) string {
for _, v := range r.heldCompletions {
if v == r.completion.indexes[i] {
return r.format.selected
}
}
if r.completion.target == i {
return r.format.cursor
}
return r.format.normal
}
func (r *render) holdCompletion() {
index := r.completion.getIndex()
if index < 0 {
return
}
for i, v := range r.heldCompletions {
if v == index {
r.heldCompletions = append(r.heldCompletions[:i:i], r.heldCompletions[i+1:]...)
return
}
}
r.heldCompletions = append(r.heldCompletions, r.completion.indexes[r.completion.target])
r.next()
}