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