2018-05-29 04:24:09 +08:00
|
|
|
package eval
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2021-01-27 09:28:38 +08:00
|
|
|
"src.elv.sh/pkg/eval/vals"
|
|
|
|
"src.elv.sh/pkg/parse"
|
|
|
|
"src.elv.sh/pkg/ui"
|
2018-05-29 04:24:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var errStyledSegmentArgType = errors.New("argument to styled-segment must be a string or a styled segment")
|
|
|
|
|
2020-08-17 01:46:29 +08:00
|
|
|
func init() {
|
2022-03-20 23:50:25 +08:00
|
|
|
addBuiltinFns(map[string]any{
|
2020-08-17 01:46:29 +08:00
|
|
|
"styled-segment": styledSegment,
|
2020-09-05 05:15:29 +08:00
|
|
|
"styled": styled,
|
2020-08-17 01:46:29 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Turns a string or ui.Segment into a new ui.Segment with the attributes
|
|
|
|
// from the supplied options applied to it. If the input is already a Segment its
|
|
|
|
// attributes are copied and modified.
|
2022-03-20 23:50:25 +08:00
|
|
|
func styledSegment(options RawOptions, input any) (*ui.Segment, error) {
|
2020-08-17 01:46:29 +08:00
|
|
|
var text string
|
|
|
|
var style ui.Style
|
|
|
|
|
|
|
|
switch input := input.(type) {
|
|
|
|
case string:
|
|
|
|
text = input
|
|
|
|
case *ui.Segment:
|
|
|
|
text = input.Text
|
|
|
|
style = input.Style
|
|
|
|
default:
|
|
|
|
return nil, errStyledSegmentArgType
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := style.MergeFromOptions(options); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ui.Segment{
|
|
|
|
Text: text,
|
|
|
|
Style: style,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-03-20 23:50:25 +08:00
|
|
|
func styled(fm *Frame, input any, stylings ...any) (ui.Text, error) {
|
2019-12-01 07:56:33 +08:00
|
|
|
var text ui.Text
|
2018-05-29 04:24:09 +08:00
|
|
|
|
|
|
|
switch input := input.(type) {
|
|
|
|
case string:
|
2023-03-15 05:47:38 +08:00
|
|
|
text = ui.T(input)
|
2019-12-01 07:56:33 +08:00
|
|
|
case *ui.Segment:
|
2023-03-15 05:47:38 +08:00
|
|
|
text = ui.TextFromSegment(input)
|
2019-12-01 07:56:33 +08:00
|
|
|
case ui.Text:
|
2018-07-15 21:46:40 +08:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2019-12-01 20:36:26 +08:00
|
|
|
for _, styling := range stylings {
|
|
|
|
switch styling := styling.(type) {
|
2018-05-29 04:24:09 +08:00
|
|
|
case string:
|
2019-12-01 20:36:26 +08:00
|
|
|
parsedStyling := ui.ParseStyling(styling)
|
|
|
|
if parsedStyling == nil {
|
|
|
|
return nil, fmt.Errorf("%s is not a valid style transformer", parse.Quote(styling))
|
2018-05-29 04:24:09 +08:00
|
|
|
}
|
2019-12-01 20:36:26 +08:00
|
|
|
text = ui.StyleText(text, parsedStyling)
|
2018-05-29 04:24:09 +08:00
|
|
|
case Callable:
|
2018-07-15 21:46:40 +08:00
|
|
|
for i, seg := range text {
|
2020-04-10 08:12:25 +08:00
|
|
|
vs, err := fm.CaptureOutput(func(fm *Frame) error {
|
2022-03-20 23:50:25 +08:00
|
|
|
return styling.Call(fm, []any{seg}, NoOpts)
|
2020-04-10 08:12:25 +08:00
|
|
|
})
|
2018-05-29 04:24:09 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if n := len(vs); n != 1 {
|
2019-12-01 20:36:26 +08:00
|
|
|
return nil, fmt.Errorf("styling function must return a single segment; got %d values", n)
|
|
|
|
} else if styledSegment, ok := vs[0].(*ui.Segment); !ok {
|
|
|
|
return nil, fmt.Errorf("styling function must return a segment; got %s", vals.Kind(vs[0]))
|
2018-05-29 04:24:09 +08:00
|
|
|
} else {
|
2019-12-01 20:36:26 +08:00
|
|
|
text[i] = styledSegment
|
2018-05-29 04:24:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2019-12-01 20:36:26 +08:00
|
|
|
return nil, fmt.Errorf("need string or callable; got %s", vals.Kind(styling))
|
2018-05-29 04:24:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-15 21:46:40 +08:00
|
|
|
return text, nil
|
2018-05-29 04:24:09 +08:00
|
|
|
}
|