From dc4c327ae8cd5602ae10eeabde9bdf6fa5624286 Mon Sep 17 00:00:00 2001 From: Eyal Posener Date: Sat, 6 May 2017 19:47:27 +0300 Subject: Use local completion files and directories according to typed command --- predicate.go | 61 ++++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 22 deletions(-) (limited to 'predicate.go') diff --git a/predicate.go b/predicate.go index 0740cfb..a5b760e 100644 --- a/predicate.go +++ b/predicate.go @@ -9,7 +9,7 @@ import ( type Predicate struct { // Predictor is function that returns list of arguments that can // come after the flag/command - Predictor func() []Option + Predictor func(last string) []Option } // Or unions two predicate struct, so that the result predicate @@ -19,25 +19,26 @@ func (p *Predicate) Or(other *Predicate) *Predicate { return nil } return &Predicate{ - Predictor: func() []Option { return append(p.predict(), other.predict()...) }, + Predictor: func(last string) []Option { return append(p.predict(last), other.predict(last)...) }, } } -func (p *Predicate) predict() []Option { +func (p *Predicate) predict(last string) []Option { if p == nil || p.Predictor == nil { return nil } - return p.Predictor() + return p.Predictor(last) } var ( PredictNothing *Predicate = nil PredictAnything = &Predicate{} + PredictDirs = &Predicate{Predictor: dirs} ) func PredictSet(options ...string) *Predicate { return &Predicate{ - Predictor: func() []Option { + Predictor: func(last string) []Option { ret := make([]Option, len(options)) for i := range options { ret[i] = Arg(options[i]) @@ -51,29 +52,29 @@ func PredictFiles(pattern string) *Predicate { return &Predicate{Predictor: glob(pattern)} } -func PredictDirs(path string) *Predicate { - return &Predicate{Predictor: dirs(path)} +func dirs(last string) (options []Option) { + dir := dirFromLast(last) + return dirsAt(dir) } -func dirs(path string) func() []Option { - return func() (options []Option) { - dirs := []string{} - filepath.Walk(path, func(path string, info os.FileInfo, err error) error { - if info.IsDir() { - dirs = append(dirs, path) - } - return nil - }) - if !filepath.IsAbs(path) { - filesToRel(dirs) +func dirsAt(path string) []Option { + dirs := []string{} + filepath.Walk(path, func(path string, info os.FileInfo, err error) error { + if info.IsDir() { + dirs = append(dirs, path) } - return filesToOptions(dirs) + return nil + }) + if !filepath.IsAbs(path) { + filesToRel(dirs) } + return filesToOptions(dirs) } -func glob(pattern string) func() []Option { - return func() []Option { - files, err := filepath.Glob(pattern) +func glob(pattern string) func(last string) []Option { + return func(last string) []Option { + dir := dirFromLast(last) + files, err := filepath.Glob(filepath.Join(dir, pattern)) if err != nil { Log("failed glob operation with pattern '%s': %s", pattern, err) } @@ -83,6 +84,7 @@ func glob(pattern string) func() []Option { return filesToOptions(files) } } + func filesToRel(files []string) { wd, err := os.Getwd() if err != nil { @@ -97,6 +99,9 @@ func filesToRel(files []string) { if err != nil { continue } + if rel == "." { + rel = "" + } files[i] = "./" + rel } return @@ -109,3 +114,15 @@ func filesToOptions(files []string) []Option { } return options } + +// dirFromLast gives the directory of the current written +// last argument if it represents a file name being written. +// in case that it is not, we fall back to the current directory. +func dirFromLast(last string) string { + dir := filepath.Dir(last) + _, err := os.Stat(dir) + if err != nil { + return "./" + } + return dir +} -- cgit v1.2.3 From f46c5f8a2808c5ade2f0b805a473765960250fe4 Mon Sep 17 00:00:00 2001 From: Eyal Posener Date: Sat, 6 May 2017 20:07:50 +0300 Subject: Change Predicate to be of function type --- command.go | 4 ++-- gocomplete/tests.go | 18 ++++++++---------- predicate.go | 48 ++++++++++++++++++------------------------------ run_test.go | 11 +++++++---- 4 files changed, 35 insertions(+), 46 deletions(-) (limited to 'predicate.go') diff --git a/command.go b/command.go index fc0e28b..eac9dde 100644 --- a/command.go +++ b/command.go @@ -2,13 +2,13 @@ package complete type Commands map[string]Command -type Flags map[string]*Predicate +type Flags map[string]Predicate type Command struct { Name string Sub Commands Flags Flags - Args *Predicate + Args Predicate } // options returns all available complete options for the given command diff --git a/gocomplete/tests.go b/gocomplete/tests.go index 865058f..60218a5 100644 --- a/gocomplete/tests.go +++ b/gocomplete/tests.go @@ -11,16 +11,14 @@ import ( "github.com/posener/complete" ) -func predictTest(testType string) *complete.Predicate { - return &complete.Predicate{ - Predictor: func(last string) []complete.Option { - tests := testNames(testType) - options := make([]complete.Option, len(tests)) - for i := range tests { - options[i] = complete.Arg(tests[i]) - } - return options - }, +func predictTest(testType string) complete.Predicate { + return func(last string) []complete.Option { + tests := testNames(testType) + options := make([]complete.Option, len(tests)) + for i := range tests { + options[i] = complete.Arg(tests[i]) + } + return options } } diff --git a/predicate.go b/predicate.go index a5b760e..f975e27 100644 --- a/predicate.go +++ b/predicate.go @@ -6,53 +6,41 @@ import ( ) // Predicate determines what terms can follow a command or a flag -type Predicate struct { - // Predictor is function that returns list of arguments that can - // come after the flag/command - Predictor func(last string) []Option -} +type Predicate func(last string) []Option // Or unions two predicate struct, so that the result predicate // returns the union of their predication -func (p *Predicate) Or(other *Predicate) *Predicate { +func (p Predicate) Or(other Predicate) Predicate { if p == nil || other == nil { return nil } - return &Predicate{ - Predictor: func(last string) []Option { return append(p.predict(last), other.predict(last)...) }, - } + return func(last string) []Option { return append(p.predict(last), other.predict(last)...) } } -func (p *Predicate) predict(last string) []Option { - if p == nil || p.Predictor == nil { +func (p Predicate) predict(last string) []Option { + if p == nil { return nil } - return p.Predictor(last) + return p(last) } var ( - PredictNothing *Predicate = nil - PredictAnything = &Predicate{} - PredictDirs = &Predicate{Predictor: dirs} + PredictNothing Predicate = nil ) -func PredictSet(options ...string) *Predicate { - return &Predicate{ - Predictor: func(last string) []Option { - ret := make([]Option, len(options)) - for i := range options { - ret[i] = Arg(options[i]) - } - return ret - }, - } -} +func PredictAnything(last string) []Option { return nil } -func PredictFiles(pattern string) *Predicate { - return &Predicate{Predictor: glob(pattern)} +func PredictSet(options ...string) Predicate { + return func(last string) []Option { + ret := make([]Option, len(options)) + for i := range options { + ret[i] = Arg(options[i]) + } + return ret + } } -func dirs(last string) (options []Option) { +func PredictDirs(last string) (options []Option) { dir := dirFromLast(last) return dirsAt(dir) } @@ -71,7 +59,7 @@ func dirsAt(path string) []Option { return filesToOptions(dirs) } -func glob(pattern string) func(last string) []Option { +func PredictFiles(pattern string) Predicate { return func(last string) []Option { dir := dirFromLast(last) files, err := filepath.Glob(filepath.Join(dir, pattern)) diff --git a/run_test.go b/run_test.go index 0fe52d6..4cbf36d 100644 --- a/run_test.go +++ b/run_test.go @@ -9,9 +9,12 @@ import ( func TestCompleter_Complete(t *testing.T) { t.Parallel() + // Set debug environment variable so logs will be printed if testing.Verbose() { os.Setenv(envDebug, "1") } + + // Change to tests directory for testing completion of files and directories err := os.Chdir("./tests") if err != nil { t.Fatal(err) @@ -20,20 +23,20 @@ func TestCompleter_Complete(t *testing.T) { c := Command{ Sub: map[string]Command{ "sub1": { - Flags: map[string]*Predicate{ + Flags: map[string]Predicate{ "-flag1": PredictAnything, "-flag2": PredictNothing, }, }, "sub2": { - Flags: map[string]*Predicate{ + Flags: map[string]Predicate{ "-flag2": PredictNothing, "-flag3": PredictSet("opt1", "opt2", "opt12"), }, - Args: PredictDirs.Or(PredictFiles("*.md")), + Args: Predicate(PredictDirs).Or(PredictFiles("*.md")), }, }, - Flags: map[string]*Predicate{ + Flags: map[string]Predicate{ "-h": PredictNothing, "-global1": PredictAnything, "-o": PredictFiles("*.txt"), -- cgit v1.2.3