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