Fix completion and Method -> Call

pull/2/head
rwxrob 2 years ago
parent e37af25024
commit d339a87795
No known key found for this signature in database
GPG Key ID: 2B9111F33082AE77

@ -17,10 +17,10 @@ limitations under the License.
package bonzai
import (
"log"
"os"
"github.com/rwxrob/bonzai/comp"
"github.com/rwxrob/bonzai/filter"
)
// Cmd is a struct the easier to use and read when creating

@ -16,8 +16,17 @@ limitations under the License.
package cmd
var Help = &Cmd{
import (
"log"
"github.com/rwxrob/bonzai"
)
var Help = &bonzai.Cmd{
Name: `help`,
Aliases: []string{"h"},
// TODO
Call: func(none ...string) error {
log.Println("would print help")
return nil
},
}

@ -20,6 +20,7 @@ import (
"fmt"
"github.com/rwxrob/bonzai"
"github.com/rwxrob/bonzai/cmd"
)
func ExampleCmd_Seek() {
@ -27,7 +28,7 @@ func ExampleCmd_Seek() {
hello := &bonzai.Cmd{
Name: `hello`,
Params: []string{"there"},
Method: func(args ...string) error {
Call: func(args ...string) error {
if len(args) > 0 {
fmt.Printf("hello %v\n", args[0])
return nil
@ -40,7 +41,7 @@ func ExampleCmd_Seek() {
hi := &bonzai.Cmd{
Name: `hi`,
Params: []string{"there", "ya"},
Method: func(args ...string) error {
Call: func(args ...string) error {
if len(args) > 0 {
fmt.Printf("hi %v\n", args[0])
return nil
@ -52,7 +53,7 @@ func ExampleCmd_Seek() {
yo := &bonzai.Cmd{
Name: `yo`,
Method: func(args ...string) error {
Call: func(args ...string) error {
fmt.Println("yo")
return nil
},
@ -61,7 +62,7 @@ func ExampleCmd_Seek() {
salut := &bonzai.Cmd{
Name: `salut`,
Params: []string{"la"},
Method: func(args ...string) error {
Call: func(args ...string) error {
if len(args) > 0 {
fmt.Printf("salut %v\n", args[0])
return nil
@ -74,12 +75,12 @@ func ExampleCmd_Seek() {
french := &bonzai.Cmd{
Name: `french`,
Aliases: []string{"fr"},
Commands: []*bonzai.Cmd{salut},
Commands: []*bonzai.Cmd{cmd.Help, salut},
}
greet := &bonzai.Cmd{
Name: `greet`,
Commands: []*bonzai.Cmd{yo, hi, hello, french},
Commands: []*bonzai.Cmd{cmd.Help, yo, hi, hello, french},
}
cmd, args := greet.Seek(bonzai.ArgsFrom(`hi there`))
@ -91,10 +92,18 @@ func ExampleCmd_Seek() {
cmd, args = greet.Seek(bonzai.ArgsFrom(`french salut `))
fmt.Printf("%v %q\n", cmd.Name, args)
cmd, args = greet.Seek(bonzai.ArgsFrom(`french h`))
fmt.Printf("%v %q\n", cmd.Name, args)
cmd, args = greet.Seek(bonzai.ArgsFrom(`french help`))
fmt.Printf("%v %q\n", cmd.Name, args)
// Output:
// hi ["there"]
// salut []
// salut [" "]
// french ["h"]
// help []
}
func ExampleCmd_CmdNames() {
@ -109,25 +118,18 @@ func ExampleCmd_CmdNames() {
func ExampleCmd_GetCommands() {
foo := new(bonzai.Cmd)
foo.Params = []string{"box"}
foo.Add("bar")
foo.Add("blah")
foo.Add("other")
fmt.Println(foo.GetCommands())
fmt.Println(foo.GetCommands("b"))
// Output:
// [bar blah other]
// [bar blah]
}
func ExampleCmd_GetParams() {
foo := new(bonzai.Cmd)
foo.Params = []string{"box", "bing", "and"}
foo.Add("bar")
foo.Add("blah")
fmt.Println(foo.GetParams())
fmt.Println(foo.GetParams("b"))
// Output:
// [box bing and]
// [box bing]
}

@ -16,7 +16,9 @@ limitations under the License.
package comp
import "github.com/rwxrob/bonzai/filter"
import (
"github.com/rwxrob/bonzai/filter"
)
// Standard completion is resolved as follows:
//
@ -24,24 +26,40 @@ import "github.com/rwxrob/bonzai/filter"
//
// 2. If leaf has no arguments, return all Commands and Params
//
// 3. Otherwise, return every Command or Param that is not in the
// 3. If first argument is the name of a Command or one of
// Aliases return only the Command name
//
// 4. Otherwise, return every Command or Param that is not in the
// Hidden list and HasPrefix matching the first arg
//
// See comp.Completer.
func Standard(x Command, args []string) []string {
// if has completer, delegate
if c := x.GetCompleter(); c != nil {
return c(x, args)
}
// fetch to avoid redundant calls
commands := x.GetCommands()
params := x.GetParams()
hidden := x.GetHidden()
// check for unique first argument command
if len(args) == 0 {
return []string{x.GetName()}
}
// build list of visible commands and params
list := []string{}
list = append(list, x.GetCommands()...)
list = append(list, x.GetParams()...)
list = filter.Minus(list, x.GetHidden())
list = append(list, commands...)
list = append(list, params...)
list = filter.Minus(list, hidden)
if len(args) > 0 {
list = filter.HasPrefix(list, args[0])
// catch edge case for first command
if args[0] == " " {
return list
}
return list
return filter.HasPrefix(list, args[0])
}

Loading…
Cancel
Save