forked from c-bata/go-prompt
-
Notifications
You must be signed in to change notification settings - Fork 2
/
render.go
466 lines (387 loc) · 14.7 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
package prompt
import (
"fmt"
"runtime"
"strings"
"github.com/confluentinc/go-prompt/internal/debug"
runewidth "github.com/mattn/go-runewidth"
"github.com/sourcegraph/go-lsp"
)
// Render to render prompt information from state of Buffer.
type Render struct {
out ConsoleWriter
prefix string
livePrefixCallback func() (prefix string, useLivePrefix bool)
breakLineCallback func(*Document)
title string
row uint16
col uint16
hideCompletion bool
previousCursor int
// colors,
prefixTextColor Color
prefixBGColor Color
inputTextColor Color
inputBGColor Color
previewSuggestionTextColor Color
previewSuggestionBGColor Color
suggestionTextColor Color
suggestionBGColor Color
diagnosticsMaxRow uint16
diagnosticsTextColor Color
diagnosticsBGColor Color
diagnosticsDetailsTextColor Color
diagnosticsDetailsBGColor Color
selectedSuggestionTextColor Color
selectedSuggestionBGColor Color
descriptionTextColor Color
descriptionBGColor Color
selectedDescriptionTextColor Color
selectedDescriptionBGColor Color
scrollbarThumbColor Color
scrollbarBGColor Color
}
// Setup to initialize console output.
func (r *Render) Setup() {
if r.title != "" {
r.out.SetTitle(r.title)
debug.AssertNoError(r.out.Flush())
}
}
// getCurrentPrefix to get current prefix.
// If live-prefix is enabled, return live-prefix.
func (r *Render) getCurrentPrefix() string {
if prefix, ok := r.livePrefixCallback(); ok {
return prefix
}
return r.prefix
}
func (r *Render) renderPrefix() {
r.out.SetColor(r.prefixTextColor, r.prefixBGColor, false)
r.out.WriteStr(r.getCurrentPrefix())
r.out.SetColor(DefaultColor, DefaultColor, false)
}
// TearDown to clear title and erasing.
func (r *Render) TearDown() {
r.out.ClearTitle()
r.out.EraseDown()
debug.AssertNoError(r.out.Flush())
}
func (r *Render) prepareArea(lines int) {
for i := 0; i < lines; i++ {
r.out.ScrollDown()
}
for i := 0; i < lines; i++ {
r.out.ScrollUp()
}
}
// UpdateWinSize called when window size is changed.
func (r *Render) UpdateWinSize(ws *WinSize) {
r.row = ws.Row
r.col = ws.Col
}
// Render completions in the dropdown and returns the lenth that the cursor has to be moved back
func (r *Render) renderCompletion(completions *CompletionManager, cursorPos int) int {
suggestions := completions.GetSuggestions()
if len(suggestions) == 0 || r.hideCompletion {
return 0
}
completionsSelectedIdx := completions.GetSelectedIdx()
completionsVerticalScroll := completions.GetVerticalScroll()
prefix := r.getCurrentPrefix()
formatted, width := formatSuggestions(
suggestions,
int(r.col)-runewidth.StringWidth(prefix)-1, // -1 means a width of scrollbar
)
// +1 means a width of scrollbar.
width++
windowHeight := len(formatted)
if windowHeight > int(completions.max) {
windowHeight = int(completions.max)
}
formatted = formatted[completionsVerticalScroll : completionsVerticalScroll+windowHeight]
r.prepareArea(windowHeight)
cursor := cursorPos
x, _ := r.toPos(cursor)
if x+width >= int(r.col) {
cursor = r.backward(cursor, x+width-int(r.col))
}
contentHeight := len(suggestions)
fractionVisible := float64(windowHeight) / float64(contentHeight)
fractionAbove := float64(completionsVerticalScroll) / float64(contentHeight)
scrollbarHeight := int(clamp(float64(windowHeight), 1, float64(windowHeight)*fractionVisible))
scrollbarTop := int(float64(windowHeight) * fractionAbove)
isScrollThumb := func(row int) bool {
return scrollbarTop <= row && row <= scrollbarTop+scrollbarHeight
}
selected := completionsSelectedIdx - completionsVerticalScroll
r.out.SetColor(White, Cyan, false)
for i := 0; i < windowHeight; i++ {
r.out.CursorDown(1)
if i == selected {
r.out.SetColor(r.selectedSuggestionTextColor, r.selectedSuggestionBGColor, true)
} else {
r.out.SetColor(r.suggestionTextColor, r.suggestionBGColor, false)
}
r.out.WriteStr(formatted[i].Text)
if i == selected {
r.out.SetColor(r.selectedDescriptionTextColor, r.selectedDescriptionBGColor, false)
} else {
r.out.SetColor(r.descriptionTextColor, r.descriptionBGColor, false)
}
r.out.WriteStr(formatted[i].Description)
if isScrollThumb(i) {
r.out.SetColor(DefaultColor, r.scrollbarThumbColor, false)
} else {
r.out.SetColor(DefaultColor, r.scrollbarBGColor, false)
}
r.out.WriteStr(" ")
r.out.SetColor(DefaultColor, DefaultColor, false)
r.lineWrap(cursor + width)
r.backward(cursor+width, width) // we go back to the same position before doing r.out.CursorDown(1) above - completions are rendered on the same pos below eacho other
}
if x+width >= int(r.col) {
r.out.CursorForward(x + width - int(r.col))
}
r.out.SetColor(DefaultColor, DefaultColor, false)
return int(r.col) * windowHeight // the number of characters to go up
}
// ClearScreen :: Clears the screen and moves the cursor to home
func (r *Render) ClearScreen() {
r.out.EraseScreen()
r.out.CursorGoTo(0, 0)
}
// Render renders to the console.
func (r *Render) Render(buffer *Buffer, lastKeyStroke Key, completionManager *CompletionManager, lexer *Lexer, diagnostics []lsp.Diagnostic) (tracedBackLines int) {
// In situations where a pseudo tty is allocated (e.g. within a docker container),
// window size via TIOCGWINSZ is not immediately available and will result in 0,0 dimensions.
if r.col == 0 {
return 0
}
defer func() { debug.AssertNoError(r.out.Flush()) }()
prefix := r.getCurrentPrefix()
line := buffer.Text()
// Down, ControlN
traceBackLines := r.previousCursor / int(r.col) // calculate number of lines we had before
// if the new buffer is empty and we are not browsing the history using the Down/controlDown keys
// then we reset the traceBackLines to 0 since there's nothing to trace back/erase.
if len(line) == 0 && lastKeyStroke != ControlDown && lastKeyStroke != Down {
traceBackLines = 0
}
debug.Log(fmt.Sprintln(line))
debug.Log(fmt.Sprintln(traceBackLines))
// prepare area by getting the end position the console cursor will be at after rendering
cursorEndPos := r.getCursorEndPos(prefix+line, 0)
// Clear screen
r.clear(r.previousCursor)
// Render new text
r.renderPrefix()
r.out.SetColor(DefaultColor, DefaultColor, false)
// if diagnostics is on, we have to redefine lexer here
r.renderLine(line, lexer, diagnostics)
r.out.SetColor(DefaultColor, DefaultColor, false)
// At this point the rendering is done and the cursor has moved to its end position we calculated earlier.
// We now need to find out where the console cursor would be if it had the same position as the buffer cursor.
translatedBufferCursorPos := r.getCursorEndPos(prefix+buffer.Document().TextBeforeCursor(), 0)
cursorPos := r.move(cursorEndPos, translatedBufferCursorPos)
// If suggestion is select for preview
if suggest, ok := completionManager.GetSelectedSuggestion(); ok {
cursorPos = r.backward(cursorPos, runewidth.StringWidth(buffer.Document().GetWordBeforeCursorUntilSeparator(completionManager.wordSeparator)))
r.out.SetColor(r.previewSuggestionTextColor, r.previewSuggestionBGColor, false)
r.out.WriteStr(suggest.Text)
r.out.SetColor(DefaultColor, DefaultColor, false)
rest := buffer.Document().TextAfterCursor()
if lexer.IsEnabled {
processed := lexer.Process(rest)
var s = rest
for _, v := range processed {
a := strings.SplitAfter(s, v.Text)
s = strings.TrimPrefix(s, a[0])
r.out.SetColor(v.Color, r.inputBGColor, false)
r.out.WriteStr(a[0])
}
} else {
r.out.WriteStr(rest)
}
cursorPosBehindSuggestion := cursorPos + runewidth.StringWidth(suggest.Text)
cursorEndPosWithInsertedSuggestion := r.getCursorEndPos(suggest.Text+rest, cursorPos)
r.out.SetColor(DefaultColor, DefaultColor, false)
cursorPos = r.move(cursorEndPosWithInsertedSuggestion, cursorPosBehindSuggestion)
}
// Render completions - We have to store completionLen to move back the cursor to the right position after rendering the completionManager or completionManager + diagnostics
completionLen := r.renderCompletion(completionManager, cursorPos)
// Render diagnostics messages - showing error detail at the bottom of the screen.
cursorPos = r.renderDiagnosticsMsg(cursorPos, completionLen, r.diagnosticsMaxRow, buffer.Document(), diagnostics)
r.previousCursor = cursorPos
return traceBackLines
}
func diagnosticsDetail(diagnostics []lsp.Diagnostic, rowsToDisplay, maxCol int) string {
formattedDiagnostic := ""
var messages []string
for _, diagnostic := range diagnostics {
if len(diagnostic.Message) > 0 {
lines := strings.Split(diagnostic.Message, "\n")
// We need to manually format each line here so that we append empty spaces to lines that doesn't fill the whole row, for it to look like "error box component" and not always have a different format depending on the error message.
for _, line := range lines {
rest := maxCol - runewidth.StringWidth(line)%maxCol // we use string width here because we need to know the width of the string, characters can have different widths in the terminal (e.g. a, け, あ)
lineWithBackground := line + strings.Repeat(" ", rest) // we fill the rest, regardless of this fills one row, or two rows and so on
messages = append(messages, lineWithBackground)
}
}
}
if len(messages) > 0 {
formattedDiagnostic = "\n" + strings.Join(messages, "")
}
// We need to truncate messages which are too long. We will display the same amount of rows that completions are configured to use.
if runewidth.StringWidth(formattedDiagnostic) > maxCol*rowsToDisplay {
formattedDiagnostic = runewidth.Truncate(formattedDiagnostic, maxCol*rowsToDisplay, "...")
}
return formattedDiagnostic
}
func hasDiagnostic(line, col int, diagnostics []lsp.Diagnostic) bool {
for _, diagnostic := range diagnostics {
diagnosticLine := diagnostic.Range.Start.Line
start := diagnostic.Range.Start.Character
end := diagnostic.Range.End.Character
if line == diagnosticLine && col >= start && col <= end {
return true
}
}
return false
}
// Render diagnostics and returns the length that the cursor has to be moved back
func (r *Render) renderDiagnosticsMsg(cursorPos, completionLen int, diagnosticsMaxRows uint16, document *Document, diagnostics []lsp.Diagnostic) int {
if document != nil && document.Text != "" {
if line, col := document.TranslateIndexToPosition(document.cursorPosition); hasDiagnostic(line, col, diagnostics) {
diagnosticsText := diagnosticsDetail(diagnostics, int(diagnosticsMaxRows), int(r.col))
// Why we do -1 here: We currently fill each new diagnostic line with empty spaces to create the colored background.
// However, the terminal is lazy and will not move the cursor to the next position/line (which would create a new line).
// Because of that, we adjust the cursor position doing -1 because that's the actual position of the terminal cursor.
cursorEndPosWithInsertedDiagnostics := r.getCursorEndPos(diagnosticsText, cursorPos) - 1
r.out.SetColor(White, r.diagnosticsDetailsBGColor, false)
r.out.WriteStr(diagnosticsText)
r.out.SetColor(White, DefaultColor, false)
return r.move(cursorEndPosWithInsertedDiagnostics+completionLen, cursorPos)
}
}
return r.move(cursorPos+completionLen, cursorPos)
}
func (r *Render) renderDiagnosticChar(s string) {
if len(s) < 1 {
return
}
r.out.SetColor(r.diagnosticsTextColor, r.diagnosticsBGColor, false)
r.out.WriteStr(s)
}
func (r *Render) renderLine(line string, lexer *Lexer, diagnostics []lsp.Diagnostic) {
if lexer != nil && lexer.IsEnabled {
processed := lexer.Process(line)
var s = line
line := 0
col := 0
for _, v := range processed {
a := strings.SplitAfter(s, v.Text)
s = strings.TrimPrefix(s, a[0])
r.renderWord(a[0], v, diagnostics, line, col)
if strings.Contains(a[0], "\n") || strings.Contains(a[0], "\r\n") {
line++
col = 0
} else {
// We have to convert to rune to count the characters and now the bytes. あ is one character, even though it's 3 bytes.
col += len([]rune(a[0]))
}
}
} else {
r.out.SetColor(r.inputTextColor, r.inputBGColor, false)
r.out.WriteStr(line)
}
}
func (r *Render) renderWord(word string, e LexerElement, diagnostics []lsp.Diagnostic, line, col int) {
runes := []rune(word)
for i, c := range runes {
if hasDiagnostic(line, col+i, diagnostics) {
r.renderDiagnosticChar(string(c))
} else {
r.out.SetColor(e.Color, r.inputBGColor, false)
r.out.WriteStr(string(c))
}
}
}
// BreakLine to break line.
func (r *Render) BreakLine(buffer *Buffer, lexer *Lexer) {
// Erasing and Render
r.clear(r.getCursorEndPos(r.getCurrentPrefix()+buffer.Document().TextBeforeCursor(), 0))
r.renderPrefix()
if lexer.IsEnabled {
processed := lexer.Process(buffer.Document().Text + "\n")
var s = buffer.Document().Text + "\n"
for _, v := range processed {
a := strings.SplitAfter(s, v.Text)
s = strings.TrimPrefix(s, a[0])
r.out.SetColor(v.Color, r.inputBGColor, false)
r.out.WriteStr(a[0])
}
} else {
r.out.SetColor(r.inputTextColor, r.inputBGColor, false)
r.out.WriteStr(buffer.Document().Text + "\n")
}
r.out.SetColor(DefaultColor, DefaultColor, false)
debug.AssertNoError(r.out.Flush())
if r.breakLineCallback != nil {
r.breakLineCallback(buffer.Document())
}
r.previousCursor = 0
}
func (r *Render) getCursorEndPos(text string, startPos int) int {
lines := strings.SplitAfter(text, "\n")
cursor := startPos
for _, line := range lines {
filledCols := runewidth.StringWidth(line)
cursor += filledCols
if len(line) > 0 && line[len(line)-1:] == "\n" {
remainingChars := int(r.col) - (cursor % int(r.col))
cursor += remainingChars
}
}
return cursor
}
// clear erases the screen from a beginning of input
// even if there is line break which means input length exceeds a window's width.
func (r *Render) clear(cursor int) {
r.move(cursor, 0)
r.out.EraseDown()
}
// backward moves cursor to backward from a current cursor position
// regardless there is a line break.
func (r *Render) backward(from, n int) int {
return r.move(from, from-n)
}
// move moves cursor to specified position from the beginning of input
// even if there is a line break.
func (r *Render) move(from, to int) int {
fromX, fromY := r.toPos(from)
toX, toY := r.toPos(to)
r.out.CursorUp(fromY - toY)
r.out.CursorBackward(fromX - toX)
return to
}
// toPos returns the relative position from the beginning of the string.
func (r *Render) toPos(cursor int) (x, y int) {
col := int(r.col)
return cursor % col, cursor / col
}
func (r *Render) lineWrap(cursor int) {
if runtime.GOOS != "windows" && cursor > 0 && cursor%int(r.col) == 0 {
r.out.WriteRaw([]byte{'\n'})
}
}
func clamp(high, low, x float64) float64 {
switch {
case high < x:
return high
case x < low:
return low
default:
return x
}
}