summaryrefslogtreecommitdiff
path: root/predicate.go
diff options
context:
space:
mode:
authorEyal Posener <[email protected]>2017-05-06 21:04:11 +0300
committerGitHub <[email protected]>2017-05-06 21:04:11 +0300
commit2b6aed2b1e974a733c0dc614a9617c33a54c208c (patch)
treed9f9de0148c079071095e96c1033c39f0c898de1 /predicate.go
parent07b98cb91243293e26564058a78f28b83aa81cd4 (diff)
parent9963a854946be0603f9e79ccba0a8b2688b20053 (diff)
Merge pull request #3 from posener/predicate-as-function-type
Predicate as function type
Diffstat (limited to 'predicate.go')
-rw-r--r--predicate.go93
1 files changed, 49 insertions, 44 deletions
diff --git a/predicate.go b/predicate.go
index 0740cfb..f975e27 100644
--- a/predicate.go
+++ b/predicate.go
@@ -6,74 +6,63 @@ 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() []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() []Option { return append(p.predict(), other.predict()...) },
- }
+ return func(last string) []Option { return append(p.predict(last), other.predict(last)...) }
}
-func (p *Predicate) predict() []Option {
- if p == nil || p.Predictor == nil {
+func (p Predicate) predict(last string) []Option {
+ if p == nil {
return nil
}
- return p.Predictor()
+ return p(last)
}
var (
- PredictNothing *Predicate = nil
- PredictAnything = &Predicate{}
+ PredictNothing Predicate = nil
)
-func PredictSet(options ...string) *Predicate {
- return &Predicate{
- Predictor: func() []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 PredictDirs(path string) *Predicate {
- return &Predicate{Predictor: dirs(path)}
+func PredictDirs(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 PredictFiles(pattern string) Predicate {
+ 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 +72,7 @@ func glob(pattern string) func() []Option {
return filesToOptions(files)
}
}
+
func filesToRel(files []string) {
wd, err := os.Getwd()
if err != nil {
@@ -97,6 +87,9 @@ func filesToRel(files []string) {
if err != nil {
continue
}
+ if rel == "." {
+ rel = ""
+ }
files[i] = "./" + rel
}
return
@@ -109,3 +102,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
+}