go-prompt/render.go

201 lines
5.5 KiB
Go
Raw Normal View History

2017-07-14 01:51:19 +00:00
package prompt
2018-02-12 08:40:47 +00:00
import (
"math"
)
2017-08-21 15:47:15 +00:00
// Render to render prompt information from state of Buffer.
2017-07-14 01:51:19 +00:00
type Render struct {
2017-07-17 17:01:24 +00:00
out ConsoleWriter
prefix string
title string
row uint16
col uint16
2017-07-16 19:32:42 +00:00
// colors
2017-07-18 15:36:16 +00:00
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
2018-02-12 08:40:47 +00:00
scrollbarThumbColor Color
scrollbarBGColor Color
2017-07-15 11:22:56 +00:00
}
2017-08-21 15:47:15 +00:00
// Setup to initialize console output.
2017-07-15 11:22:56 +00:00
func (r *Render) Setup() {
2017-07-16 17:18:11 +00:00
if r.title != "" {
r.out.SetTitle(r.title)
2017-07-17 16:14:03 +00:00
r.out.Flush()
2017-07-15 11:22:56 +00:00
}
}
2017-07-15 16:18:40 +00:00
func (r *Render) renderPrefix() {
2017-07-18 16:16:51 +00:00
r.out.SetColor(r.prefixTextColor, r.prefixBGColor, false)
2017-07-16 17:11:52 +00:00
r.out.WriteStr(r.prefix)
2017-07-18 16:16:51 +00:00
r.out.SetColor(DefaultColor, DefaultColor, false)
2017-07-15 16:18:40 +00:00
}
2017-08-21 15:47:15 +00:00
// TearDown to clear title and erasing.
2017-07-15 11:22:56 +00:00
func (r *Render) TearDown() {
r.out.ClearTitle()
r.out.EraseDown()
r.out.Flush()
2017-07-15 08:37:54 +00:00
}
func (r *Render) prepareArea(lines int) {
2017-07-15 08:37:54 +00:00
for i := 0; i < lines; i++ {
r.out.ScrollDown()
}
for i := 0; i < lines; i++ {
r.out.ScrollUp()
}
return
2017-07-14 01:51:19 +00:00
}
2017-08-21 15:47:15 +00:00
// UpdateWinSize called when window size is changed.
2017-07-15 09:03:18 +00:00
func (r *Render) UpdateWinSize(ws *WinSize) {
2017-07-14 01:51:19 +00:00
r.row = ws.Row
r.col = ws.Col
return
}
2017-07-17 16:14:03 +00:00
func (r *Render) renderWindowTooSmall() {
r.out.CursorGoTo(0, 0)
r.out.EraseScreen()
2017-07-18 16:16:51 +00:00
r.out.SetColor(DarkRed, White, false)
2017-07-17 16:14:03 +00:00
r.out.WriteStr("Your console window is too small...")
r.out.Flush()
return
}
2017-08-09 12:33:47 +00:00
func (r *Render) renderCompletion(buf *Buffer, completions *CompletionManager) {
2018-02-12 08:40:47 +00:00
windowHeight := len(completions.tmp)
if windowHeight > int(completions.max) {
windowHeight = int(completions.max)
}
contentHeight := len(completions.tmp)
fractionVisible := float64(windowHeight) / float64(contentHeight)
fractionAbove := float64(completions.verticalScroll) / float64(contentHeight)
scrollbarHeight := int(math.Min(float64(windowHeight), math.Max(1, float64(windowHeight)*fractionVisible)))
scrollbarTop := int(float64(windowHeight) * fractionAbove)
isScrollThumb := func(row int) bool {
return scrollbarTop <= row && row <= scrollbarTop+scrollbarHeight
2017-07-17 12:54:39 +00:00
}
2017-08-09 12:33:47 +00:00
suggestions := completions.GetSuggestions()
if l := len(completions.GetSuggestions()); l == 0 {
2017-07-15 11:22:56 +00:00
return
}
2017-07-15 16:04:18 +00:00
2017-08-12 09:59:10 +00:00
formatted, width := formatSuggestions(
2017-08-09 12:33:47 +00:00
suggestions,
int(r.col)-len(r.prefix)-1, // -1 means a width of scrollbar
2017-07-17 14:17:08 +00:00
)
2018-02-12 08:40:47 +00:00
formatted = formatted[completions.verticalScroll : completions.verticalScroll+windowHeight]
2017-07-15 09:51:33 +00:00
l := len(formatted)
2018-02-12 08:40:47 +00:00
r.prepareArea(windowHeight)
2017-07-15 08:37:54 +00:00
// +1 means a width of scrollbar.
2018-02-12 09:50:42 +00:00
d := (len(r.prefix) + len(buf.Document().TextBeforeCursor()) + 1) % int(r.col)
if d == 0 { // the cursor is on right end.
r.out.CursorBackward(width)
2017-07-17 20:32:14 +00:00
} else if d+width > int(r.col) {
2017-07-17 15:35:10 +00:00
r.out.CursorBackward(d + width - int(r.col))
2017-07-15 16:04:18 +00:00
}
2018-02-12 08:40:47 +00:00
selected := completions.selected - completions.verticalScroll
2017-07-18 16:16:51 +00:00
r.out.SetColor(White, Cyan, false)
2017-07-15 09:51:33 +00:00
for i := 0; i < l; i++ {
r.out.CursorDown(1)
2018-02-12 08:40:47 +00:00
if i == selected {
2017-07-18 16:16:51 +00:00
r.out.SetColor(r.selectedSuggestionTextColor, r.selectedSuggestionBGColor, true)
2017-07-15 13:44:10 +00:00
} else {
2017-07-18 16:16:51 +00:00
r.out.SetColor(r.suggestionTextColor, r.suggestionBGColor, false)
2017-07-15 13:44:10 +00:00
}
2017-07-18 15:36:16 +00:00
r.out.WriteStr(formatted[i].Text)
2018-02-12 08:40:47 +00:00
if i == selected {
2017-07-18 16:16:51 +00:00
r.out.SetColor(r.selectedDescriptionTextColor, r.selectedDescriptionBGColor, false)
2017-07-18 15:36:16 +00:00
} else {
2017-07-18 16:16:51 +00:00
r.out.SetColor(r.descriptionTextColor, r.descriptionBGColor, false)
2017-07-18 15:36:16 +00:00
}
r.out.WriteStr(formatted[i].Description)
2018-02-12 08:40:47 +00:00
if isScrollThumb(i) {
r.out.SetColor(DefaultColor, r.scrollbarThumbColor, false)
} else {
r.out.SetColor(DefaultColor, r.scrollbarBGColor, false)
}
r.out.WriteStr(" ")
// +1 means a width of scrollbar.
2018-02-12 08:40:47 +00:00
r.out.CursorBackward(width + 1)
2017-07-15 09:51:33 +00:00
}
if d == 0 { // the cursor is on right end.
// DON'T CURSOR DOWN HERE. Because the line doesn't erase properly.
r.out.CursorForward(width + 1)
} else if d+width > int(r.col) {
2017-07-17 15:35:10 +00:00
r.out.CursorForward(d + width - int(r.col))
2017-07-15 16:04:18 +00:00
}
2017-07-15 08:37:54 +00:00
2017-07-15 09:51:33 +00:00
r.out.CursorUp(l)
2017-07-18 16:16:51 +00:00
r.out.SetColor(DefaultColor, DefaultColor, false)
2017-07-15 09:51:33 +00:00
return
}
2017-07-15 08:37:54 +00:00
2017-08-21 15:47:15 +00:00
// Render renders to the console.
2017-08-09 12:33:47 +00:00
func (r *Render) Render(buffer *Buffer, completion *CompletionManager) {
2017-07-17 16:14:03 +00:00
// Erasing
2017-07-17 14:37:40 +00:00
r.out.CursorBackward(int(r.col) + len(buffer.Text()) + len(r.prefix))
2017-07-15 11:22:56 +00:00
r.out.EraseDown()
2017-07-17 16:14:03 +00:00
// prepare area
line := buffer.Text()
2017-08-12 09:59:10 +00:00
h := ((len(r.prefix) + len(line)) / int(r.col)) + 1 + int(completion.max)
2017-07-18 15:36:16 +00:00
if h > int(r.row) || completionMargin > int(r.col) {
2017-07-17 16:14:03 +00:00
r.renderWindowTooSmall()
return
}
// Rendering
r.renderPrefix()
2017-07-18 16:16:51 +00:00
r.out.SetColor(r.inputTextColor, r.inputBGColor, false)
2017-07-15 11:22:56 +00:00
r.out.WriteStr(line)
2017-07-18 16:16:51 +00:00
r.out.SetColor(DefaultColor, DefaultColor, false)
r.out.CursorBackward(len([]rune(line)) - buffer.CursorPosition)
2017-08-09 12:33:47 +00:00
r.renderCompletion(buffer, completion)
if suggest, ok := completion.GetSelectedSuggestion(); ok {
2017-07-15 14:27:49 +00:00
r.out.CursorBackward(len([]rune(buffer.Document().GetWordBeforeCursor())))
2017-07-18 16:16:51 +00:00
r.out.SetColor(r.previewSuggestionTextColor, r.previewSuggestionBGColor, false)
2017-08-09 12:33:47 +00:00
r.out.WriteStr(suggest.Text)
2017-07-18 16:16:51 +00:00
r.out.SetColor(DefaultColor, DefaultColor, false)
2017-07-15 14:27:49 +00:00
}
2017-07-15 11:22:56 +00:00
r.out.Flush()
}
2017-08-21 15:47:15 +00:00
// BreakLine to break line.
2017-07-18 11:48:50 +00:00
func (r *Render) BreakLine(buffer *Buffer) {
// CR
2017-07-17 16:14:03 +00:00
r.out.CursorBackward(int(r.col) + len(buffer.Text()) + len(r.prefix))
2017-07-18 11:48:50 +00:00
// Erasing and Render
2017-07-17 16:14:03 +00:00
r.out.EraseDown()
r.renderPrefix()
2017-07-18 16:16:51 +00:00
r.out.SetColor(r.inputTextColor, r.inputBGColor, false)
2017-07-16 19:55:05 +00:00
r.out.WriteStr(buffer.Document().Text + "\n")
2017-07-18 16:16:51 +00:00
r.out.SetColor(DefaultColor, DefaultColor, false)
2017-07-18 13:06:33 +00:00
r.out.Flush()
2017-07-18 11:48:50 +00:00
}