github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/cmds/core/elvish/edit/styled.go (about)

     1  package edit
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/u-root/u-root/cmds/core/elvish/edit/ui"
     7  	"github.com/u-root/u-root/cmds/core/elvish/vector"
     8  )
     9  
    10  var errStyledStyles = errors.New("styles must either be a string or list of strings")
    11  
    12  // A constructor for *ui.Styled, for use in Elvish script.
    13  func styled(text string, styles interface{}) (*ui.Styled, error) {
    14  	switch styles := styles.(type) {
    15  	case string:
    16  		return &ui.Styled{text, ui.StylesFromString(styles)}, nil
    17  	case vector.Vector:
    18  		converted := make([]string, 0, styles.Len())
    19  		for it := styles.Iterator(); it.HasElem(); it.Next() {
    20  			elem, ok := it.Elem().(string)
    21  			if !ok {
    22  				return nil, errStyledStyles
    23  			}
    24  			converted = append(converted, elem)
    25  		}
    26  		return &ui.Styled{text, ui.Styles(converted)}, nil
    27  	default:
    28  		return nil, errStyledStyles
    29  	}
    30  }