github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+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, []string, error) {
    28  	var files []string
    29  	var exact string
    30  	for _, c := range m.Completers {
    31  		x, cc, err := c.Complete(s)
    32  		if err != nil {
    33  			Debug("MultiCompleter: %v: %v", c, err)
    34  		}
    35  		files = append(files, cc...)
    36  		if exact == "" {
    37  			exact = x
    38  		}
    39  
    40  	}
    41  	return exact, files, nil
    42  }