complete/predict.go

175 lines
4.3 KiB
Go
Raw Normal View History

2017-05-05 08:57:22 -05:00
package complete
2017-05-05 13:57:21 -05:00
import (
"io/ioutil"
2017-05-05 13:57:21 -05:00
"os"
"path/filepath"
"strings"
2017-05-07 11:53:55 -05:00
"github.com/posener/complete/match"
2017-05-05 13:57:21 -05:00
)
2017-05-11 12:28:31 -05:00
// Predictor implements a predict method, in which given
// command line arguments returns a list of options it predicts.
type Predictor interface {
Predict(Args) []string
}
2017-05-05 13:57:21 -05:00
2017-05-11 12:28:31 -05:00
// PredictOr unions two predicate functions, so that the result predicate
// returns the union of their predication
2017-05-11 12:28:31 -05:00
func PredictOr(predictors ...Predictor) Predictor {
return PredictFunc(func(a Args) (prediction []string) {
for _, p := range predictors {
if p == nil {
continue
}
prediction = append(prediction, p.Predict(a)...)
}
return
})
}
2017-05-11 12:28:31 -05:00
// PredictFunc determines what terms can follow a command or a flag
// It is used for auto completion, given last - the last word in the already
// in the command line, what words can complete it.
type PredictFunc func(Args) []string
// Predict invokes the predict function and implements the Predictor interface
func (p PredictFunc) Predict(a Args) []string {
if p == nil {
2017-05-05 13:57:21 -05:00
return nil
}
2017-05-11 12:28:31 -05:00
return p(a)
2017-05-05 08:57:22 -05:00
}
2017-05-06 14:06:49 -05:00
// PredictNothing does not expect anything after.
2017-05-11 12:28:31 -05:00
var PredictNothing Predictor
2017-05-05 08:57:22 -05:00
2017-05-06 14:25:44 -05:00
// PredictAnything expects something, but nothing particular, such as a number
2017-05-06 14:06:49 -05:00
// or arbitrary name.
2017-05-11 12:28:31 -05:00
var PredictAnything = PredictFunc(func(Args) []string { return nil })
2017-05-05 13:57:21 -05:00
2017-05-06 14:06:49 -05:00
// PredictSet expects specific set of terms, given in the options argument.
2017-05-11 12:28:31 -05:00
func PredictSet(options ...string) Predictor {
2017-05-11 12:48:40 -05:00
return predictSet(options)
2017-05-11 12:28:31 -05:00
}
2017-05-11 12:48:40 -05:00
type predictSet []string
2017-05-11 12:28:31 -05:00
func (p predictSet) Predict(a Args) (prediction []string) {
for _, m := range p {
2017-05-11 12:48:40 -05:00
if match.Prefix(m, a.Last) {
prediction = append(prediction, m)
}
}
2017-05-11 12:28:31 -05:00
return
2017-05-05 16:25:27 -05:00
}
2017-05-06 14:06:49 -05:00
// PredictDirs will search for directories in the given started to be typed
// path, if no path was started to be typed, it will complete to directories
// in the current working directory.
2017-05-11 12:28:31 -05:00
func PredictDirs(pattern string) Predictor {
return files(pattern, false)
}
2017-05-05 16:25:27 -05:00
2017-05-06 14:06:49 -05:00
// PredictFiles will search for files matching the given pattern in the started to
// be typed path, if no path was started to be typed, it will complete to files that
// match the pattern in the current working directory.
// To match any file, use "*" as pattern. To match go files use "*.go", and so on.
2017-05-11 12:28:31 -05:00
func PredictFiles(pattern string) Predictor {
return files(pattern, true)
}
func files(pattern string, allowFiles bool) PredictFunc {
2017-05-11 12:28:31 -05:00
return func(a Args) (prediction []string) {
if strings.HasSuffix(a.Last, "/..") {
return
}
2017-05-11 12:28:31 -05:00
dir := dirFromLast(a.Last)
rel := !filepath.IsAbs(pattern)
2017-05-11 12:28:31 -05:00
Log("looking for files in %s (last=%s)", dir, a.Last)
files := listFiles(dir, pattern)
// get wording directory for relative name
workDir, err := os.Getwd()
2017-05-05 13:57:21 -05:00
if err != nil {
workDir = ""
2017-05-05 13:57:21 -05:00
}
// add dir if match
files = append(files, dir)
2017-05-11 12:28:31 -05:00
// add all matching files to prediction
for _, f := range files {
if stat, err := os.Stat(f); err != nil || (!stat.IsDir() && !allowFiles) {
continue
}
// change file name to relative if necessary
if rel && workDir != "" {
f = toRel(workDir, f)
}
// test matching of file to the argument
2017-05-11 12:48:40 -05:00
if match.File(f, a.Last) {
prediction = append(prediction, f)
2017-05-11 12:28:31 -05:00
}
}
return
2017-05-06 14:06:49 -05:00
}
}
func listFiles(dir, pattern string) []string {
m := map[string]bool{}
if files, err := filepath.Glob(filepath.Join(dir, pattern)); err == nil {
for _, f := range files {
m[f] = true
}
}
if dirs, err := ioutil.ReadDir(dir); err == nil {
for _, d := range dirs {
if d.IsDir() {
m[d.Name()] = true
}
}
}
list := make([]string, 0, len(m))
for k := range m {
list = append(list, k)
}
return list
}
// toRel changes a file name to a relative name
func toRel(wd, file string) string {
abs, err := filepath.Abs(file)
2017-05-05 13:57:21 -05:00
if err != nil {
return file
2017-05-05 13:57:21 -05:00
}
rel, err := filepath.Rel(wd, abs)
if err != nil {
return file
2017-05-05 13:57:21 -05:00
}
if rel != "." {
rel = "./" + rel
}
if info, err := os.Stat(rel); err == nil && info.IsDir() {
rel += "/"
}
return rel
2017-05-05 13:57:21 -05:00
}
// 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 {
if info, err := os.Stat(last); err == nil && info.IsDir() {
return last
}
dir := filepath.Dir(last)
_, err := os.Stat(dir)
if err != nil {
return "./"
}
return dir
}