github.com/arneball/complete@v1.1.2/predict.go (about)

     1  package complete
     2  
     3  // Predictor implements a predict method, in which given
     4  // command line arguments returns a list of options it predicts.
     5  type Predictor interface {
     6  	Predict(Args) []string
     7  }
     8  
     9  // PredictOr unions two predicate functions, so that the result predicate
    10  // returns the union of their predication
    11  func PredictOr(predictors ...Predictor) Predictor {
    12  	return PredictFunc(func(a Args) (prediction []string) {
    13  		for _, p := range predictors {
    14  			if p == nil {
    15  				continue
    16  			}
    17  			prediction = append(prediction, p.Predict(a)...)
    18  		}
    19  		return
    20  	})
    21  }
    22  
    23  // PredictFunc determines what terms can follow a command or a flag
    24  // It is used for auto completion, given last - the last word in the already
    25  // in the command line, what words can complete it.
    26  type PredictFunc func(Args) []string
    27  
    28  // Predict invokes the predict function and implements the Predictor interface
    29  func (p PredictFunc) Predict(a Args) []string {
    30  	if p == nil {
    31  		return nil
    32  	}
    33  	return p(a)
    34  }
    35  
    36  // PredictNothing does not expect anything after.
    37  var PredictNothing Predictor
    38  
    39  // PredictAnything expects something, but nothing particular, such as a number
    40  // or arbitrary name.
    41  var PredictAnything = PredictFunc(func(Args) []string { return nil })