Rename Completion to Suggest
This commit is contained in:
parent
654143f525
commit
bdc32514f8
|
@ -11,8 +11,8 @@ func executor(ctx context.Context, t string) string {
|
|||
return fmt.Sprintln("Your input: " + t)
|
||||
}
|
||||
|
||||
func completer(t string) []prompt.Completion {
|
||||
return []prompt.Completion{
|
||||
func completer(t string) []prompt.Suggest {
|
||||
return []prompt.Suggest{
|
||||
{Text: "users", Description: "user table"},
|
||||
{Text: "sites", Description: "sites table"},
|
||||
{Text: "articles", Description: "articles table"},
|
||||
|
|
|
@ -25,8 +25,8 @@ func executor(ctx context.Context, t string) string {
|
|||
return ""
|
||||
}
|
||||
|
||||
func completer(t string) []prompt.Completion {
|
||||
return []prompt.Completion{
|
||||
func completer(t string) []prompt.Suggest {
|
||||
return []prompt.Suggest{
|
||||
{Text: "sleep 5s"},
|
||||
{Text: "sleep 20s"},
|
||||
}
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
package prompt
|
||||
|
||||
type Completion struct {
|
||||
type Suggest struct {
|
||||
Text string
|
||||
Description string
|
||||
}
|
||||
|
||||
type CompletionManager struct {
|
||||
selected int // -1 means nothing one is selected.
|
||||
tmp []*Completion
|
||||
tmp []*Suggest
|
||||
Max uint16
|
||||
}
|
||||
|
||||
|
@ -16,7 +16,7 @@ func (c *CompletionManager) Reset() {
|
|||
return
|
||||
}
|
||||
|
||||
func (c *CompletionManager) Update(new []*Completion) {
|
||||
func (c *CompletionManager) Update(new []*Suggest) {
|
||||
c.selected = -1
|
||||
c.tmp = new
|
||||
return
|
||||
|
@ -36,7 +36,7 @@ func (c *CompletionManager) Completing() bool {
|
|||
return c.selected != -1
|
||||
}
|
||||
|
||||
func (c *CompletionManager) update(completions []Completion) {
|
||||
func (c *CompletionManager) update(completions []Suggest) {
|
||||
max := int(c.Max)
|
||||
if len(completions) < max {
|
||||
max = len(completions)
|
||||
|
|
14
filter.go
14
filter.go
|
@ -2,9 +2,9 @@ package prompt
|
|||
|
||||
import "strings"
|
||||
|
||||
type CompletionFilter func([]Completion, string, bool) []Completion
|
||||
type CompletionFilter func([]Suggest, string, bool) []Suggest
|
||||
|
||||
func FilterHasPrefix(completions []Completion, sub string, ignoreCase bool) []Completion {
|
||||
func FilterHasPrefix(completions []Suggest, sub string, ignoreCase bool) []Suggest {
|
||||
if sub == "" {
|
||||
return completions
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ func FilterHasPrefix(completions []Completion, sub string, ignoreCase bool) []Co
|
|||
sub = strings.ToUpper(sub)
|
||||
}
|
||||
|
||||
ret := make([]Completion, 0, len(completions))
|
||||
ret := make([]Suggest, 0, len(completions))
|
||||
for i := range completions {
|
||||
c := completions[i].Text
|
||||
if ignoreCase {
|
||||
|
@ -25,7 +25,7 @@ func FilterHasPrefix(completions []Completion, sub string, ignoreCase bool) []Co
|
|||
return ret
|
||||
}
|
||||
|
||||
func FilterHasSuffix(completions []Completion, sub string, ignoreCase bool) []Completion {
|
||||
func FilterHasSuffix(completions []Suggest, sub string, ignoreCase bool) []Suggest {
|
||||
if sub == "" {
|
||||
return completions
|
||||
}
|
||||
|
@ -33,7 +33,7 @@ func FilterHasSuffix(completions []Completion, sub string, ignoreCase bool) []Co
|
|||
sub = strings.ToUpper(sub)
|
||||
}
|
||||
|
||||
ret := make([]Completion, 0, len(completions))
|
||||
ret := make([]Suggest, 0, len(completions))
|
||||
for i := range completions {
|
||||
c := completions[i].Text
|
||||
if ignoreCase {
|
||||
|
@ -46,7 +46,7 @@ func FilterHasSuffix(completions []Completion, sub string, ignoreCase bool) []Co
|
|||
return ret
|
||||
}
|
||||
|
||||
func FilterContains(completions []Completion, sub string, ignoreCase bool) []Completion {
|
||||
func FilterContains(completions []Suggest, sub string, ignoreCase bool) []Suggest {
|
||||
if sub == "" {
|
||||
return completions
|
||||
}
|
||||
|
@ -54,7 +54,7 @@ func FilterContains(completions []Completion, sub string, ignoreCase bool) []Com
|
|||
sub = strings.ToUpper(sub)
|
||||
}
|
||||
|
||||
ret := make([]Completion, 0, len(completions))
|
||||
ret := make([]Suggest, 0, len(completions))
|
||||
for i := range completions {
|
||||
c := completions[i].Text
|
||||
if ignoreCase {
|
||||
|
|
|
@ -9,36 +9,36 @@ func TestFilter(t *testing.T) {
|
|||
var scenarioTable = []struct {
|
||||
scenario string
|
||||
filter CompletionFilter
|
||||
list []Completion
|
||||
list []Suggest
|
||||
substr string
|
||||
ignoreCase bool
|
||||
expected []Completion
|
||||
expected []Suggest
|
||||
}{
|
||||
{
|
||||
scenario: "Contains don't ignore case",
|
||||
filter: FilterContains,
|
||||
list: []Completion{
|
||||
list: []Suggest{
|
||||
{Text: "abcde"},
|
||||
{Text: "fghij"},
|
||||
{Text: "ABCDE"},
|
||||
},
|
||||
substr: "cd",
|
||||
ignoreCase: false,
|
||||
expected: []Completion{
|
||||
expected: []Suggest{
|
||||
{Text: "abcde"},
|
||||
},
|
||||
},
|
||||
{
|
||||
scenario: "Contains ignore case",
|
||||
filter: FilterContains,
|
||||
list: []Completion{
|
||||
list: []Suggest{
|
||||
{Text: "abcde"},
|
||||
{Text: "fghij"},
|
||||
{Text: "ABCDE"},
|
||||
},
|
||||
substr: "cd",
|
||||
ignoreCase: true,
|
||||
expected: []Completion{
|
||||
expected: []Suggest{
|
||||
{Text: "abcde"},
|
||||
{Text: "ABCDE"},
|
||||
},
|
||||
|
@ -46,28 +46,28 @@ func TestFilter(t *testing.T) {
|
|||
{
|
||||
scenario: "HasPrefix don't ignore case",
|
||||
filter: FilterHasPrefix,
|
||||
list: []Completion{
|
||||
list: []Suggest{
|
||||
{Text: "abcde"},
|
||||
{Text: "fghij"},
|
||||
{Text: "ABCDE"},
|
||||
},
|
||||
substr: "abc",
|
||||
ignoreCase: false,
|
||||
expected: []Completion{
|
||||
expected: []Suggest{
|
||||
{Text: "abcde"},
|
||||
},
|
||||
},
|
||||
{
|
||||
scenario: "HasPrefix ignore case",
|
||||
filter: FilterHasPrefix,
|
||||
list: []Completion{
|
||||
list: []Suggest{
|
||||
{Text: "abcde"},
|
||||
{Text: "fabcj"},
|
||||
{Text: "ABCDE"},
|
||||
},
|
||||
substr: "abc",
|
||||
ignoreCase: true,
|
||||
expected: []Completion{
|
||||
expected: []Suggest{
|
||||
{Text: "abcde"},
|
||||
{Text: "ABCDE"},
|
||||
},
|
||||
|
@ -75,28 +75,28 @@ func TestFilter(t *testing.T) {
|
|||
{
|
||||
scenario: "HasSuffix don't ignore case",
|
||||
filter: FilterHasSuffix,
|
||||
list: []Completion{
|
||||
list: []Suggest{
|
||||
{Text: "abcde"},
|
||||
{Text: "fcdej"},
|
||||
{Text: "ABCDE"},
|
||||
},
|
||||
substr: "cde",
|
||||
ignoreCase: false,
|
||||
expected: []Completion{
|
||||
expected: []Suggest{
|
||||
{Text: "abcde"},
|
||||
},
|
||||
},
|
||||
{
|
||||
scenario: "HasSuffix ignore case",
|
||||
filter: FilterHasSuffix,
|
||||
list: []Completion{
|
||||
list: []Suggest{
|
||||
{Text: "abcde"},
|
||||
{Text: "fcdej"},
|
||||
{Text: "ABCDE"},
|
||||
},
|
||||
substr: "cde",
|
||||
ignoreCase: true,
|
||||
expected: []Completion{
|
||||
expected: []Suggest{
|
||||
{Text: "abcde"},
|
||||
{Text: "ABCDE"},
|
||||
},
|
||||
|
|
|
@ -16,7 +16,7 @@ const (
|
|||
)
|
||||
|
||||
type Executor func(context.Context, string) string
|
||||
type Completer func(string) []Completion
|
||||
type Completer func(string) []Suggest
|
||||
|
||||
type Prompt struct {
|
||||
in ConsoleParser
|
||||
|
|
10
render.go
10
render.go
|
@ -81,7 +81,7 @@ func (r *Render) renderWindowTooSmall() {
|
|||
return
|
||||
}
|
||||
|
||||
func (r *Render) renderCompletion(buf *Buffer, completions []Completion, max uint16, selected int) {
|
||||
func (r *Render) renderCompletion(buf *Buffer, completions []Suggest, max uint16, selected int) {
|
||||
if max > r.row {
|
||||
max = r.row
|
||||
}
|
||||
|
@ -136,7 +136,7 @@ func (r *Render) renderCompletion(buf *Buffer, completions []Completion, max uin
|
|||
return
|
||||
}
|
||||
|
||||
func (r *Render) Render(buffer *Buffer, completions []Completion, maxCompletions uint16, selected int) {
|
||||
func (r *Render) Render(buffer *Buffer, completions []Suggest, maxCompletions uint16, selected int) {
|
||||
// Erasing
|
||||
r.out.CursorBackward(int(r.col) + len(buffer.Text()) + len(r.prefix))
|
||||
r.out.EraseDown()
|
||||
|
@ -187,9 +187,9 @@ func (r *Render) RenderResult(result string) {
|
|||
r.out.SetColor(DefaultColor, DefaultColor, false)
|
||||
}
|
||||
|
||||
func formatCompletions(completions []Completion, max int) (new []Completion, width int) {
|
||||
func formatCompletions(completions []Suggest, max int) (new []Suggest, width int) {
|
||||
num := len(completions)
|
||||
new = make([]Completion, num)
|
||||
new = make([]Suggest, num)
|
||||
leftWidth := 0
|
||||
rightWidth := 0
|
||||
|
||||
|
@ -238,7 +238,7 @@ func formatCompletions(completions []Completion, max int) (new []Completion, wid
|
|||
} else {
|
||||
newDescription = rightPrefix + completions[i].Description + rightSuffix
|
||||
}
|
||||
new[i] = Completion{Text: newText, Description: newDescription}
|
||||
new[i] = Suggest{Text: newText, Description: newDescription}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
|
@ -8,16 +8,16 @@ import (
|
|||
func TestFormatCompletion(t *testing.T) {
|
||||
scenarioTable := []struct {
|
||||
scenario string
|
||||
completions []Completion
|
||||
completions []Suggest
|
||||
prefix string
|
||||
suffix string
|
||||
expected []Completion
|
||||
expected []Suggest
|
||||
maxWidth int
|
||||
expectedWidth int
|
||||
}{
|
||||
{
|
||||
scenario: "",
|
||||
completions: []Completion{
|
||||
completions: []Suggest{
|
||||
{Text: "select"},
|
||||
{Text: "from"},
|
||||
{Text: "insert"},
|
||||
|
@ -25,7 +25,7 @@ func TestFormatCompletion(t *testing.T) {
|
|||
},
|
||||
prefix: " ",
|
||||
suffix: " ",
|
||||
expected: []Completion{
|
||||
expected: []Suggest{
|
||||
{Text: " select "},
|
||||
{Text: " from "},
|
||||
{Text: " insert "},
|
||||
|
@ -36,7 +36,7 @@ func TestFormatCompletion(t *testing.T) {
|
|||
},
|
||||
{
|
||||
scenario: "",
|
||||
completions: []Completion{
|
||||
completions: []Suggest{
|
||||
{Text: "select", Description: "select description"},
|
||||
{Text: "from", Description: "from description"},
|
||||
{Text: "insert", Description: "insert description"},
|
||||
|
@ -44,7 +44,7 @@ func TestFormatCompletion(t *testing.T) {
|
|||
},
|
||||
prefix: " ",
|
||||
suffix: " ",
|
||||
expected: []Completion{
|
||||
expected: []Suggest{
|
||||
{Text: " select ", Description: " select description "},
|
||||
{Text: " from ", Description: " from description "},
|
||||
{Text: " insert ", Description: " insert description "},
|
||||
|
|
Loading…
Reference in New Issue