forked from c-bata/go-prompt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.go
More file actions
360 lines (304 loc) · 9.57 KB
/
render.go
File metadata and controls
360 lines (304 loc) · 9.57 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
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
package prompt
import (
"runtime"
runewidth "github.com/mattn/go-runewidth"
"github.com/tarantool/go-prompt/internal/debug"
)
const (
// basicRenderEvent renders context and completion.
basicRenderEvent = iota
// breakLineRenderEvent renders context with break-line.
breakLineRenderEvent
windowResizeRenderEvent
)
// Render to render prompt information from state of Buffer.
type Render struct {
out ConsoleWriter
breakLineCallback func(*Document)
row uint16
col uint16
// Colors.
prefixTextColor Color
prefixBGColor Color
inputTextColor Color
inputBGColor Color
previewSuggestionTextColor Color
previewSuggestionBGColor Color
suggestionTextColor Color
suggestionBGColor 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(title string) {
if title != "" {
r.out.SetTitle(title)
debug.AssertNoError(r.out.Flush())
}
}
// 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
}
func (r *Render) renderWindowTooSmall() {
r.out.CursorGoTo(0, 0)
r.out.EraseScreen()
r.out.SetColor(DarkRed, White, false)
r.out.WriteStr("Your console window is too small...")
}
// renderCompletion renders completion.
func (r *Render) renderCompletion(ctx renderCtx) {
suggestions := ctx.completion.GetSuggestions()
if len(suggestions) == 0 {
return
}
prefix := ctx.prefix
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(ctx.completion.max) {
windowHeight = int(ctx.completion.max)
}
formatted = formatted[ctx.completion.verticalScroll : ctx.completion.verticalScroll+
windowHeight]
r.prepareArea(windowHeight)
cursor := runewidth.StringWidth(ctx.cmd.Document().TextBeforeCursor())
x, _ := r.toPos(cursor)
if x+width >= int(r.col) {
cursor = r.backward(cursor, x+width-int(r.col))
}
contentHeight := len(ctx.completion.tmp)
fractionVisible := float64(windowHeight) / float64(contentHeight)
fractionAbove := float64(ctx.completion.verticalScroll) / 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 := ctx.completion.selected - ctx.completion.verticalScroll
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)
}
if x+width >= int(r.col) {
r.out.CursorForward(x + width - int(r.col))
}
r.out.CursorUp(windowHeight)
r.out.SetColor(DefaultColor, DefaultColor, false)
}
// ClearScreen clears the screen and moves the cursor to home.
func (r *Render) ClearScreen() {
r.out.EraseScreen()
r.out.CursorGoTo(0, 0)
}
// writeCmdWithPrefix writes cmd with prefix to the out.
func writeCmdWithPrefix(
out ConsoleWriter,
cmd string,
prefixLen int,
prefixColor Color,
bgColor Color,
defaultColor Color,
) {
out.SetColor(prefixColor, bgColor, false)
out.WriteStr(cmd[:prefixLen])
out.SetColor(DefaultColor, DefaultColor, false)
out.WriteStr(cmd[prefixLen:])
}
// preprocessCtx preprocesses the context before rendering.
func (r *Render) preprocessCtx(ctx renderCtx) renderCtx {
ctx.cmd = ctx.cmd.SplitWideLines(int(r.col))
return ctx
}
// renderCtx renders context to the out, returns
// (new location of cursor, new location of the end of the rendered command).
func (r *Render) renderCtx(ctx renderCtx) (newCursor location, newEndCursor location) {
// Preprocess the context.
ctx = r.preprocessCtx(ctx)
// Calculate current cursor position.
cursorRow, cursorCol := ctx.cmd.Document().GetCursorPosition()
// Calculate cursor position of the line end.
endRow, endCol := ctx.cmd.Document().GetCustomCursorPosition(
len([]rune(ctx.cmd.Text())),
)
ctxCursorPos := ctx.cursor.row*int(r.col) + ctx.cursor.col
ctxEndCursorPos := ctx.endCursor.row*int(r.col) + ctx.endCursor.col
if ctx.renderEvent == windowResizeRenderEvent {
r.clear(ctxCursorPos, true)
} else {
// Move to the end of rendered recently.
r.move(ctxCursorPos, ctxEndCursorPos)
// Erase rendered recently.
r.clear(ctxEndCursorPos, false)
}
// Render.
writeCmdWithPrefix(r.out, ctx.cmd.Text(), len(ctx.prefix),
ctx.prefixColor, r.prefixBGColor, DefaultColor)
r.lineWrap(endCol)
// Move cursor back to the position inside cmd.
r.move(endRow*int(r.col)+endCol, cursorRow*int(r.col)+cursorCol)
return location{cursorRow, cursorCol}, location{endRow, endCol}
}
// Render is main render function.
// It calls suitable sub-render function (as `renderBreakLine`) in dependence of render event.
// Returns new cursor position.
func (r *Render) Render(ctx renderCtx) (location, location) {
// 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 ctx.renderEvent == breakLineRenderEvent {
return r.renderBreakLine(ctx)
}
if r.col == 0 {
return location{}, location{}
}
defer func() { debug.AssertNoError(r.out.Flush()) }()
// Hide cursor to prevent blinking.
r.out.HideCursor()
defer func() {
r.out.ShowCursor()
}()
// Render current state.
curCursor, endCursor := r.renderCtx(ctx)
if ctx.renderCompletion {
r.renderCompletion(ctx)
if suggest, ok := ctx.completion.GetSelectedSuggestion(); ok {
curCursor.col = r.backward(curCursor.col, runewidth.StringWidth(
ctx.cmd.Document().GetWordBeforeCursorUntilSeparator(
ctx.completion.wordSeparator,
),
))
r.out.SetColor(r.previewSuggestionTextColor, r.previewSuggestionBGColor, false)
r.out.WriteStr(suggest.Text)
r.out.SetColor(DefaultColor, DefaultColor, false)
curCursor.col += runewidth.StringWidth(suggest.Text)
rest := ctx.cmd.Document().TextAfterCursor()
r.out.WriteStr(rest)
curCursor.col += runewidth.StringWidth(rest)
r.lineWrap(curCursor.col)
curCursor.col = r.backward(curCursor.col, runewidth.StringWidth(rest))
}
}
return curCursor, endCursor
}
// renderBreakline renders state with linebreak and calls breakline callback.
func (r *Render) renderBreakLine(ctx renderCtx) (location, location) {
defer func() { debug.AssertNoError(r.out.Flush()) }()
// Hide cursor to prevent blinking.
r.out.HideCursor()
defer func() {
r.out.ShowCursor()
}()
cmdBuf := NewBuffer()
cmdBuf.InsertText(ctx.cmd.Text()+"\n", false, true)
cmdDocument := ctx.cmd.Document()
ctx.cmd = cmdBuf
// Render state.
r.renderCtx(ctx)
if r.breakLineCallback != nil {
r.breakLineCallback(cmdDocument)
}
return location{}, location{}
}
// clear erases the screen from a beginning of input
//
// function supports two work modes.
// `hard=true` when `EraseDown` is used for clearing the rendered text.
//
// `hard=false`, when softly clear line by line is used.
//
// `EraseDown` may cause flickering in some terminals (e.g. `urxvt`).
func (r *Render) clear(cursor int, hard bool) {
r.out.EraseDown()
if !hard {
fromX, fromY := r.toPos(cursor)
r.out.CursorBackward(fromX)
for i := 0; i < fromY; i++ {
r.out.EraseEndOfLine()
r.out.CursorUp(1)
}
r.out.EraseEndOfLine()
} else {
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
}
}