Add variadic completer arguments and clean

pull/53/head
rwxrob 2 years ago
parent 3468bffaf0
commit 961b197203
No known key found for this signature in database
GPG Key ID: 2B9111F33082AE77

@ -71,7 +71,7 @@ func ArgsFrom(line string) []string {
}
args = strings.Fields(line)
if line[len(line)-1] == ' ' {
args = append(args, " ")
args = append(args, "")
}
return args
}

@ -14,5 +14,5 @@ func ExampleArgsFrom() {
fmt.Printf("%q\n", bonzai.ArgsFrom(`greet hi french `))
// Output:
// ["greet" "hi" "french"]
// ["greet" "hi" "french" " "]
// ["greet" "hi" "french" ""]
}

@ -4,6 +4,7 @@
package bonzai
import (
"fmt"
"os"
"github.com/rwxrob/bonzai/comp"
@ -76,11 +77,11 @@ func (x *Cmd) Run() {
if line != "" {
cmd, args := x.Seek(ArgsFrom(line)[1:])
if cmd.Completer == nil {
list := comp.Standard(cmd, args)
list := comp.Standard(cmd, args...)
filter.Println(list)
Exit()
}
filter.Println(cmd.Completer(cmd, args))
filter.Println(cmd.Completer(cmd, args...))
Exit()
}

@ -88,7 +88,7 @@ func ExampleCmd_Seek() {
// Output:
// hi ["there"]
// salut []
// salut [" "]
// salut [""]
// french ["h"]
// help []
}

@ -7,7 +7,7 @@ package comp
// the provided arguments, if any. Completer functions must never be
// passed a nil Command or nil as the args slice. See comp.Standard.
type Completer func(leaf Command, args []string) []string
type Completer func(leaf Command, args ...string) []string
// Command interface is only here to break cyclical package imports.
// This enables Completers of any kind to be create and managed

@ -20,15 +20,15 @@ import (
// Hidden list and HasPrefix matching the first arg
//
// See comp.Completer.
func Standard(x Command, args []string) []string {
func Standard(x Command, args ...string) []string {
// if has completer, delegate
if c := x.GetCompleter(); c != nil {
return c(x, args)
return c(x, args...)
}
// check for unique first argument command
if len(args) == 0 {
// not sure we've completed the command name itself yet
if args == nil || len(args) == 0 {
return []string{x.GetName()}
}
@ -38,10 +38,5 @@ func Standard(x Command, args []string) []string {
list = append(list, x.GetParams()...)
list = filter.Minus(list, x.GetHidden())
// catch edge case for explicit word boundary
if args[0] == " " {
return list
}
return filter.HasPrefix(list, args[0])
}

@ -17,34 +17,34 @@ func ExampleStandard() {
foo.Add("bar")
foo.Add("blah")
// all commands and params since nothing specified
args := []string{}
fmt.Println(comp.Standard(foo, args))
// if no args, we have to assume the command isn't finished yet
fmt.Println(comp.Standard(foo))
// we know it's not a command, but no prefix just yet
// (usually this is when a space has been added after the command)
fmt.Println(comp.Standard(foo, ""))
// everything that begins with a (nothing)
args = []string{`a`}
fmt.Println(comp.Standard(foo, args))
fmt.Println(comp.Standard(foo, `a`))
// everything that begins with b (which is everything)
args = []string{`b`}
fmt.Println(comp.Standard(foo, args))
fmt.Println(comp.Standard(foo, `b`))
// everything that begins with bl (just blah)
args = []string{`bl`}
fmt.Println(comp.Standard(foo, args))
fmt.Println(comp.Standard(foo, `bl`))
// give own completer for days of the week
foo.Completer = func(cmd comp.Command, args []string) []string {
foo.Completer = func(cmd comp.Command, args ...string) []string {
list := []string{"mon", "tue", "wed", "thu", "fri", "sat", "sun"}
if len(args) == 0 {
return list
}
return filter.HasPrefix(list, args[0])
}
args = []string{`t`}
fmt.Println(comp.Standard(foo, args))
fmt.Println(comp.Standard(foo, `t`))
//Output:
// []
// [bar blah box]
// []
// [bar blah box]

Loading…
Cancel
Save