complete/predict.go

42 lines
1.2 KiB
Go
Raw Normal View History

2017-05-05 08:57:22 -05:00
package complete
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 })