Add Suggestion struct with Description
This commit is contained in:
parent
3cad620d16
commit
c7333f7c05
@ -11,12 +11,12 @@ func executor(t string) string {
|
|||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
func completer(t string) []string {
|
func completer(t string) []*prompt.Suggestion {
|
||||||
return []string{
|
return []*prompt.Suggestion{
|
||||||
"users",
|
{Text: "users"},
|
||||||
"sites",
|
{Text: "sites"},
|
||||||
"articles",
|
{Text: "articles"},
|
||||||
"comments",
|
{Text: "comments"},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
12
prompt.go
12
prompt.go
@ -8,7 +8,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Executor func(string) string
|
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 {
|
type Prompt struct {
|
||||||
in ConsoleParser
|
in ConsoleParser
|
||||||
@ -43,7 +47,7 @@ func (p *Prompt) Run() {
|
|||||||
if w != "" {
|
if w != "" {
|
||||||
p.buf.DeleteBeforeCursor(len([]rune(w)))
|
p.buf.DeleteBeforeCursor(len([]rune(w)))
|
||||||
}
|
}
|
||||||
p.buf.InsertText(c, false, true)
|
p.buf.InsertText(c.Text, false, true)
|
||||||
}
|
}
|
||||||
p.selected = -1
|
p.selected = -1
|
||||||
p.buf.InsertText(string(b), false, true)
|
p.buf.InsertText(string(b), false, true)
|
||||||
@ -54,7 +58,7 @@ func (p *Prompt) Run() {
|
|||||||
if w != "" {
|
if w != "" {
|
||||||
p.buf.DeleteBeforeCursor(len([]rune(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)
|
p.renderer.BreakLine(p.buf)
|
||||||
res := p.executor(p.buf.Text())
|
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)
|
max := int(p.maxCompletions)
|
||||||
if len(completions) < max {
|
if len(completions) < max {
|
||||||
max = len(completions)
|
max = len(completions)
|
||||||
|
30
render.go
30
render.go
@ -67,19 +67,19 @@ func (r *Render) renderWindowTooSmall() {
|
|||||||
return
|
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 {
|
if max > r.row {
|
||||||
max = r.row
|
max = r.row
|
||||||
}
|
}
|
||||||
|
|
||||||
if l := len(words); l == 0 {
|
if l := len(suggestions); l == 0 {
|
||||||
return
|
return
|
||||||
} else if l > int(max) {
|
} else if l > int(max) {
|
||||||
words = words[:max]
|
suggestions = suggestions[:max]
|
||||||
}
|
}
|
||||||
|
|
||||||
formatted, width := formatCompletions(
|
formatted, width := formatCompletions(
|
||||||
words,
|
suggestions,
|
||||||
int(r.col)-len(r.prefix),
|
int(r.col)-len(r.prefix),
|
||||||
" ",
|
" ",
|
||||||
" ",
|
" ",
|
||||||
@ -117,7 +117,7 @@ func (r *Render) renderCompletion(buf *Buffer, words []string, max uint16, selec
|
|||||||
return
|
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
|
// Erasing
|
||||||
r.out.CursorBackward(int(r.col) + len(buffer.Text()) + len(r.prefix))
|
r.out.CursorBackward(int(r.col) + len(buffer.Text()) + len(r.prefix))
|
||||||
r.out.EraseDown()
|
r.out.EraseDown()
|
||||||
@ -142,7 +142,7 @@ func (r *Render) Render(buffer *Buffer, completions []string, maxCompletions uin
|
|||||||
c := completions[selected]
|
c := completions[selected]
|
||||||
r.out.CursorBackward(len([]rune(buffer.Document().GetWordBeforeCursor())))
|
r.out.CursorBackward(len([]rune(buffer.Document().GetWordBeforeCursor())))
|
||||||
r.out.SetColor(r.previewSuggestionTextColor, r.previewSuggestionBGColor)
|
r.out.SetColor(r.previewSuggestionTextColor, r.previewSuggestionBGColor)
|
||||||
r.out.WriteStr(c)
|
r.out.WriteStr(c.Text)
|
||||||
r.out.SetColor(DefaultColor, DefaultColor)
|
r.out.SetColor(DefaultColor, DefaultColor)
|
||||||
}
|
}
|
||||||
r.out.Flush()
|
r.out.Flush()
|
||||||
@ -169,14 +169,14 @@ func (r *Render) RenderResult(result string) {
|
|||||||
r.out.SetColor(DefaultColor, DefaultColor)
|
r.out.SetColor(DefaultColor, DefaultColor)
|
||||||
}
|
}
|
||||||
|
|
||||||
func formatCompletions(words []string, max int, prefix string, suffix string) (new []string, width int) {
|
func formatCompletions(suggestions []*Suggestion, max int, prefix string, suffix string) (new []string, width int) {
|
||||||
num := len(words)
|
num := len(suggestions)
|
||||||
new = make([]string, num)
|
new = make([]string, num)
|
||||||
width = 0
|
width = 0
|
||||||
|
|
||||||
for i := 0; i < num; i++ {
|
for i := 0; i < num; i++ {
|
||||||
if width < len([]rune(words[i])) {
|
if width < len([]rune(suggestions[i].Text)) {
|
||||||
width = len([]rune(words[i]))
|
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++ {
|
for i := 0; i < num; i++ {
|
||||||
if l := len(words[i]); l > width {
|
if l := len(suggestions[i].Text); l > width {
|
||||||
new[i] = prefix + words[i][:width-len("...")] + "..." + suffix
|
new[i] = prefix + suggestions[i].Text[:width-len("...")] + "..." + suffix
|
||||||
} else if l < width {
|
} else if l < width {
|
||||||
spaces := strings.Repeat(" ", width-len([]rune(words[i])))
|
spaces := strings.Repeat(" ", width-len([]rune(suggestions[i].Text)))
|
||||||
new[i] = prefix + words[i] + spaces + suffix
|
new[i] = prefix + suggestions[i].Text + spaces + suffix
|
||||||
} else {
|
} else {
|
||||||
new[i] = prefix + words[i] + suffix
|
new[i] = prefix + suggestions[i].Text + suffix
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
width += len(prefix) + len(suffix)
|
width += len(prefix) + len(suffix)
|
||||||
|
@ -8,7 +8,7 @@ import (
|
|||||||
func TestFormatCompletion(t *testing.T) {
|
func TestFormatCompletion(t *testing.T) {
|
||||||
scenarioTable := []struct {
|
scenarioTable := []struct {
|
||||||
scenario string
|
scenario string
|
||||||
completions []string
|
completions []*Suggestion
|
||||||
prefix string
|
prefix string
|
||||||
suffix string
|
suffix string
|
||||||
expected []string
|
expected []string
|
||||||
@ -17,11 +17,11 @@ func TestFormatCompletion(t *testing.T) {
|
|||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
scenario: "",
|
scenario: "",
|
||||||
completions: []string{
|
completions: []*Suggestion{
|
||||||
"select",
|
{Text: "select"},
|
||||||
"from",
|
{Text: "from"},
|
||||||
"insert",
|
{Text: "insert"},
|
||||||
"where",
|
{Text: "where"},
|
||||||
},
|
},
|
||||||
prefix: " ",
|
prefix: " ",
|
||||||
suffix: " ",
|
suffix: " ",
|
||||||
|
Loading…
Reference in New Issue
Block a user