github.com/mem/u-root@v2.0.1-0.20181004165302-9b18b4636a33+incompatible/pkg/complete/multi.go (about)

     1  // Copyright 2012-2018 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package complete
     6  
     7  // MultiCompleter is a Completer consisting of one or more Completers
     8  // Why do this?
     9  // We need it for paths, anyway, but consider a shell which
    10  // has builtins and metacharacters such as >, &, etc.
    11  // You can build a MultiCompleter which has a string completer
    12  // and a set of file completers, so you don't need to special
    13  // case anything.
    14  type MultiCompleter struct {
    15  	Completers []Completer
    16  }
    17  
    18  // NewMultiCompleter returns a MultiCompleter created from
    19  // one or more Completers. It is perfectly legal to include a
    20  // MultiCompleter.
    21  func NewMultiCompleter(c Completer, cc ...Completer) Completer {
    22  	return &MultiCompleter{append([]Completer{c}, cc...)}
    23  }
    24  
    25  // Complete Returns a []string consisting of the results
    26  // of calling all the Completers.
    27  func (m *MultiCompleter) Complete(s string) ([]string, error) {
    28  	var files []string
    29  	for _, c := range m.Completers {
    30  		cc, err := c.Complete(s)
    31  		if err != nil {
    32  			Debug("MultiCompleter: %v: %v", c, err)
    33  		}
    34  		files = append(files, cc...)
    35  	}
    36  	return files, nil
    37  }