From c7333f7c05407e028ffd692af886c4348eeaba62 Mon Sep 17 00:00:00 2001 From: c-bata Date: Tue, 18 Jul 2017 09:53:59 +0900 Subject: [PATCH] Add Suggestion struct with Description --- _example/echo_prompt.go | 12 ++++++------ prompt.go | 12 ++++++++---- render.go | 30 +++++++++++++++--------------- render_test.go | 12 ++++++------ 4 files changed, 35 insertions(+), 31 deletions(-) diff --git a/_example/echo_prompt.go b/_example/echo_prompt.go index 1547230..4d88d99 100644 --- a/_example/echo_prompt.go +++ b/_example/echo_prompt.go @@ -11,12 +11,12 @@ func executor(t string) string { return r } -func completer(t string) []string { - return []string{ - "users", - "sites", - "articles", - "comments", +func completer(t string) []*prompt.Suggestion { + return []*prompt.Suggestion{ + {Text: "users"}, + {Text: "sites"}, + {Text: "articles"}, + {Text: "comments"}, } } diff --git a/prompt.go b/prompt.go index ceaf144..108f92c 100644 --- a/prompt.go +++ b/prompt.go @@ -8,7 +8,11 @@ import ( ) type Executor func(string) string -type Completer func(string) []string +type Completer func(string) []*Suggestion +type Suggestion struct { + Text string + Description string +} type Prompt struct { in ConsoleParser @@ -43,7 +47,7 @@ func (p *Prompt) Run() { if w != "" { p.buf.DeleteBeforeCursor(len([]rune(w))) } - p.buf.InsertText(c, false, true) + p.buf.InsertText(c.Text, false, true) } p.selected = -1 p.buf.InsertText(string(b), false, true) @@ -54,7 +58,7 @@ func (p *Prompt) Run() { if w != "" { p.buf.DeleteBeforeCursor(len([]rune(w))) } - p.buf.InsertText(c, false, true) + p.buf.InsertText(c.Text, false, true) } p.renderer.BreakLine(p.buf) res := p.executor(p.buf.Text()) @@ -93,7 +97,7 @@ func (p *Prompt) Run() { } } -func (p *Prompt) updateSelectedCompletion(completions []string) { +func (p *Prompt) updateSelectedCompletion(completions []*Suggestion) { max := int(p.maxCompletions) if len(completions) < max { max = len(completions) diff --git a/render.go b/render.go index 9c3c8a7..1f744aa 100644 --- a/render.go +++ b/render.go @@ -67,19 +67,19 @@ func (r *Render) renderWindowTooSmall() { return } -func (r *Render) renderCompletion(buf *Buffer, words []string, max uint16, selected int) { +func (r *Render) renderCompletion(buf *Buffer, suggestions []*Suggestion, max uint16, selected int) { if max > r.row { max = r.row } - if l := len(words); l == 0 { + if l := len(suggestions); l == 0 { return } else if l > int(max) { - words = words[:max] + suggestions = suggestions[:max] } formatted, width := formatCompletions( - words, + suggestions, int(r.col)-len(r.prefix), " ", " ", @@ -117,7 +117,7 @@ func (r *Render) renderCompletion(buf *Buffer, words []string, max uint16, selec return } -func (r *Render) Render(buffer *Buffer, completions []string, maxCompletions uint16, selected int) { +func (r *Render) Render(buffer *Buffer, completions []*Suggestion, maxCompletions uint16, selected int) { // Erasing r.out.CursorBackward(int(r.col) + len(buffer.Text()) + len(r.prefix)) r.out.EraseDown() @@ -142,7 +142,7 @@ func (r *Render) Render(buffer *Buffer, completions []string, maxCompletions uin c := completions[selected] r.out.CursorBackward(len([]rune(buffer.Document().GetWordBeforeCursor()))) r.out.SetColor(r.previewSuggestionTextColor, r.previewSuggestionBGColor) - r.out.WriteStr(c) + r.out.WriteStr(c.Text) r.out.SetColor(DefaultColor, DefaultColor) } r.out.Flush() @@ -169,14 +169,14 @@ func (r *Render) RenderResult(result string) { r.out.SetColor(DefaultColor, DefaultColor) } -func formatCompletions(words []string, max int, prefix string, suffix string) (new []string, width int) { - num := len(words) +func formatCompletions(suggestions []*Suggestion, max int, prefix string, suffix string) (new []string, width int) { + num := len(suggestions) new = make([]string, num) width = 0 for i := 0; i < num; i++ { - if width < len([]rune(words[i])) { - width = len([]rune(words[i])) + if width < len([]rune(suggestions[i].Text)) { + width = len([]rune(suggestions[i].Text)) } } @@ -185,13 +185,13 @@ func formatCompletions(words []string, max int, prefix string, suffix string) (n } for i := 0; i < num; i++ { - if l := len(words[i]); l > width { - new[i] = prefix + words[i][:width-len("...")] + "..." + suffix + if l := len(suggestions[i].Text); l > width { + new[i] = prefix + suggestions[i].Text[:width-len("...")] + "..." + suffix } else if l < width { - spaces := strings.Repeat(" ", width-len([]rune(words[i]))) - new[i] = prefix + words[i] + spaces + suffix + spaces := strings.Repeat(" ", width-len([]rune(suggestions[i].Text))) + new[i] = prefix + suggestions[i].Text + spaces + suffix } else { - new[i] = prefix + words[i] + suffix + new[i] = prefix + suggestions[i].Text + suffix } } width += len(prefix) + len(suffix) diff --git a/render_test.go b/render_test.go index 94615ba..8b0d125 100644 --- a/render_test.go +++ b/render_test.go @@ -8,7 +8,7 @@ import ( func TestFormatCompletion(t *testing.T) { scenarioTable := []struct { scenario string - completions []string + completions []*Suggestion prefix string suffix string expected []string @@ -17,11 +17,11 @@ func TestFormatCompletion(t *testing.T) { }{ { scenario: "", - completions: []string{ - "select", - "from", - "insert", - "where", + completions: []*Suggestion{ + {Text: "select"}, + {Text: "from"}, + {Text: "insert"}, + {Text: "where"}, }, prefix: " ", suffix: " ",