Remove Complete struct
This commit is contained in:
parent
5dbf53eec0
commit
d33bac720b
|
@ -186,5 +186,5 @@ func main() {
|
|||
},
|
||||
}
|
||||
|
||||
complete.New(gogo).Complete()
|
||||
complete.Run(gogo)
|
||||
}
|
||||
|
|
|
@ -11,33 +11,28 @@ const (
|
|||
envDebug = "COMP_DEBUG"
|
||||
)
|
||||
|
||||
type Completer struct {
|
||||
Command
|
||||
}
|
||||
|
||||
func New(c Command) *Completer {
|
||||
return &Completer{Command: c}
|
||||
}
|
||||
|
||||
func (c *Completer) Complete() {
|
||||
// Run get a command, get the typed arguments from environment
|
||||
// variable, and print out the complete options
|
||||
func Run(c Command) {
|
||||
args := getLine()
|
||||
Log("Completing args: %s", args)
|
||||
|
||||
options := c.complete(args)
|
||||
options := complete(c, args)
|
||||
|
||||
Log("Completion: %s", options)
|
||||
output(options)
|
||||
}
|
||||
|
||||
func (c *Completer) complete(args []string) []string {
|
||||
all, _ := c.options(args[:len(args)-1])
|
||||
return c.chooseRelevant(last(args), all)
|
||||
}
|
||||
// complete get a command an command line arguments and returns
|
||||
// matching completion options
|
||||
func complete(c Command, args []string) (matching []string) {
|
||||
options, _ := c.options(args[:len(args)-1])
|
||||
|
||||
func (c *Completer) chooseRelevant(last string, options []Option) (relevant []string) {
|
||||
// choose only matching options
|
||||
l := last(args)
|
||||
for _, option := range options {
|
||||
if option.Matches(last) {
|
||||
relevant = append(relevant, option.String())
|
||||
if option.Matches(l) {
|
||||
matching = append(matching, option.String())
|
||||
}
|
||||
}
|
||||
return
|
|
@ -13,29 +13,27 @@ func TestCompleter_Complete(t *testing.T) {
|
|||
os.Setenv(envDebug, "1")
|
||||
}
|
||||
|
||||
c := Completer{
|
||||
Command: Command{
|
||||
Sub: map[string]Command{
|
||||
"sub1": {
|
||||
Flags: map[string]Predicate{
|
||||
"-flag1": PredictAnything,
|
||||
"-flag2": PredictNothing,
|
||||
},
|
||||
},
|
||||
"sub2": {
|
||||
Flags: map[string]Predicate{
|
||||
"-flag2": PredictNothing,
|
||||
"-flag3": PredictSet("opt1", "opt2", "opt12"),
|
||||
},
|
||||
Args: PredictDirs("./tests/").Or(PredictFiles("./tests/*.md")),
|
||||
c := Command{
|
||||
Sub: map[string]Command{
|
||||
"sub1": {
|
||||
Flags: map[string]Predicate{
|
||||
"-flag1": PredictAnything,
|
||||
"-flag2": PredictNothing,
|
||||
},
|
||||
},
|
||||
Flags: map[string]Predicate{
|
||||
"-h": PredictNothing,
|
||||
"-global1": PredictAnything,
|
||||
"-o": PredictFiles("./tests/*.txt"),
|
||||
"sub2": {
|
||||
Flags: map[string]Predicate{
|
||||
"-flag2": PredictNothing,
|
||||
"-flag3": PredictSet("opt1", "opt2", "opt12"),
|
||||
},
|
||||
Args: PredictDirs("./tests/").Or(PredictFiles("./tests/*.md")),
|
||||
},
|
||||
},
|
||||
Flags: map[string]Predicate{
|
||||
"-h": PredictNothing,
|
||||
"-global1": PredictAnything,
|
||||
"-o": PredictFiles("./tests/*.txt"),
|
||||
},
|
||||
}
|
||||
|
||||
allGlobals := []string{}
|
||||
|
@ -181,7 +179,7 @@ func TestCompleter_Complete(t *testing.T) {
|
|||
os.Setenv(envComplete, tt.args)
|
||||
args := getLine()
|
||||
|
||||
got := c.complete(args)
|
||||
got := complete(c, args)
|
||||
|
||||
sort.Strings(tt.want)
|
||||
sort.Strings(got)
|
Loading…
Reference in New Issue