2018-05-29 04:24:09 +08:00
|
|
|
package eval
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/elves/elvish/eval/vals"
|
2018-07-15 21:46:40 +08:00
|
|
|
"github.com/elves/elvish/parse"
|
2018-05-29 04:24:09 +08:00
|
|
|
"github.com/elves/elvish/styled"
|
|
|
|
)
|
|
|
|
|
|
|
|
var errStyledSegmentArgType = errors.New("argument to styled-segment must be a string or a styled segment")
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
addBuiltinFns(map[string]interface{}{
|
|
|
|
"styled-segment": styledSegment,
|
2018-05-29 05:14:06 +08:00
|
|
|
"styled": Styled,
|
2018-05-29 04:24:09 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Turns a string or styled Segment into a new styled Segment with the attributes
|
|
|
|
// from the supplied options applied to it. If the input is already a Segment its
|
|
|
|
// attributes are copied and modified.
|
|
|
|
func styledSegment(options RawOptions, input interface{}) (*styled.Segment, error) {
|
|
|
|
var text string
|
|
|
|
var style styled.Style
|
|
|
|
|
|
|
|
switch input := input.(type) {
|
|
|
|
case string:
|
|
|
|
text = input
|
|
|
|
case *styled.Segment:
|
|
|
|
text = input.Text
|
|
|
|
style = input.Style
|
|
|
|
default:
|
|
|
|
return nil, errStyledSegmentArgType
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := style.ImportFromOptions(options); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &styled.Segment{
|
|
|
|
Text: text,
|
|
|
|
Style: style,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-05-29 05:14:06 +08:00
|
|
|
// Styled turns a string, a styled Segment or a styled Text into a styled Text.
|
|
|
|
// This is done by applying a range of transformers to the input.
|
2018-07-15 21:46:40 +08:00
|
|
|
func Styled(fm *Frame, input interface{}, transformers ...interface{}) (styled.Text, error) {
|
2018-05-29 04:24:09 +08:00
|
|
|
var text styled.Text
|
|
|
|
|
|
|
|
switch input := input.(type) {
|
|
|
|
case string:
|
2018-07-15 21:46:40 +08:00
|
|
|
text = styled.Text{&styled.Segment{
|
2018-05-29 04:24:09 +08:00
|
|
|
Text: input,
|
|
|
|
Style: styled.Style{},
|
|
|
|
}}
|
|
|
|
case *styled.Segment:
|
2018-07-15 21:46:40 +08:00
|
|
|
text = styled.Text{input.Clone()}
|
|
|
|
case styled.Text:
|
|
|
|
text = input.Clone()
|
2018-05-29 04:24:09 +08:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("expected string, styled segment or styled text; got %s", vals.Kind(input))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, transformer := range transformers {
|
|
|
|
switch transformer := transformer.(type) {
|
|
|
|
case string:
|
|
|
|
transformerFn := styled.FindTransformer(transformer)
|
|
|
|
if transformerFn == nil {
|
2018-07-15 21:46:40 +08:00
|
|
|
return nil, fmt.Errorf("%s is not a valid style transformer", parse.Quote(transformer))
|
2018-05-29 04:24:09 +08:00
|
|
|
}
|
2018-07-15 21:46:40 +08:00
|
|
|
for _, seg := range text {
|
2019-11-24 18:40:01 +08:00
|
|
|
transformerFn(&seg.Style)
|
2018-05-29 04:24:09 +08:00
|
|
|
}
|
|
|
|
case Callable:
|
2018-07-15 21:46:40 +08:00
|
|
|
for i, seg := range text {
|
|
|
|
vs, err := fm.CaptureOutput(transformer, []interface{}{seg}, NoOpts)
|
2018-05-29 04:24:09 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if n := len(vs); n != 1 {
|
2018-07-15 21:46:40 +08:00
|
|
|
return nil, fmt.Errorf("style transformers must return a single styled segment; got %d values", n)
|
2018-05-29 04:24:09 +08:00
|
|
|
} else if transformedSegment, ok := vs[0].(*styled.Segment); !ok {
|
|
|
|
return nil, fmt.Errorf("style transformers must return a styled segment; got %s", vals.Kind(vs[0]))
|
|
|
|
} else {
|
2018-07-15 21:46:40 +08:00
|
|
|
text[i] = transformedSegment
|
2018-05-29 04:24:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("need string or callable; got %s", vals.Kind(transformer))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-15 21:46:40 +08:00
|
|
|
return text, nil
|
2018-05-29 04:24:09 +08:00
|
|
|
}
|