2018-05-29 04:24:09 +08:00
|
|
|
package eval
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2019-12-24 04:00:59 +08:00
|
|
|
"github.com/elves/elvish/pkg/eval/vals"
|
|
|
|
"github.com/elves/elvish/pkg/parse"
|
|
|
|
"github.com/elves/elvish/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-01-18 21:12:50 +08:00
|
|
|
//elvdoc:fn styled-segment
|
|
|
|
//
|
|
|
|
// ```elvish
|
|
|
|
// styled-segment $object &fg-color=default &bg-color=default &bold=$false &dim=$false &italic=$false &underlined=$false &blink=$false &inverse=$false
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// Constructs a styled segment and is a helper function for styled transformers.
|
|
|
|
// `$object` can be a plain string, a styled segment or a concatenation thereof.
|
|
|
|
// Probably the only reason to use it is to build custom style transformers:
|
|
|
|
//
|
|
|
|
// ```elvish
|
|
|
|
// fn my-awesome-style-transformer [seg]{ styled-segment $seg &bold=(not $seg[dim]) &dim=(not $seg[italic]) &italic=$seg[bold] }
|
|
|
|
// styled abc $my-awesome-style-transformer~
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// As just seen the properties of styled segments can be inspected by indexing into
|
|
|
|
// it. Valid indices are the same as the options to `styled-segment` plus `text`.
|
|
|
|
//
|
|
|
|
// ```elvish
|
|
|
|
// s = (styled-segment abc &bold)
|
|
|
|
// put $s[text]
|
|
|
|
// put $s[fg-color]
|
|
|
|
// put $s[bold]
|
|
|
|
// ```
|
|
|
|
|
|
|
|
//elvdoc:fn styled
|
|
|
|
//
|
|
|
|
// ```elvish
|
|
|
|
// styled $object $style-transformer...
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// Construct a styled text by applying the supplied transformers to the supplied
|
|
|
|
// object. `$object` can be either a string, a styled segment (see below), a styled
|
|
|
|
// text or an arbitrary concatenation of them. A `$style-transformer` is either:
|
|
|
|
//
|
|
|
|
// - The name of a builtin style transformer, which may be one of the following:
|
|
|
|
//
|
2020-04-11 10:39:47 +08:00
|
|
|
// - One of the attribute names `bold`, `dim`, `italic`, `underlined`,
|
|
|
|
// `blink` or `inverse` for setting the corresponding attribute.
|
2020-01-18 21:12:50 +08:00
|
|
|
//
|
2020-04-11 10:39:47 +08:00
|
|
|
// - An attribute name prefixed by `no-` for unsetting the attribute.
|
2020-01-18 21:12:50 +08:00
|
|
|
//
|
2020-04-11 10:39:47 +08:00
|
|
|
// - An attribute name prefixed by `toggle-` for toggling the attribute
|
|
|
|
// between set and unset.
|
2020-01-18 21:12:50 +08:00
|
|
|
//
|
|
|
|
// - A color name for setting the text color, which may be one of the
|
|
|
|
// following:
|
|
|
|
//
|
2020-04-11 01:17:37 +08:00
|
|
|
// - One of the 8 basic ANSI colors: `black`, `red`, `green`, `yellow`,
|
2020-04-11 10:39:47 +08:00
|
|
|
// `blue`, `magenta`, `cyan` and `white`.
|
2020-01-18 21:12:50 +08:00
|
|
|
//
|
2020-04-11 01:17:37 +08:00
|
|
|
// - The bright variant of the 8 basic ANSI colors, with a `bright-`
|
2020-04-11 10:39:47 +08:00
|
|
|
// prefix.
|
2020-01-18 21:12:50 +08:00
|
|
|
//
|
2020-04-11 01:17:37 +08:00
|
|
|
// - Any color from the xterm 256-color palette, as `colorX` (such as
|
2020-04-11 10:39:47 +08:00
|
|
|
// `color12`).
|
2020-01-18 21:12:50 +08:00
|
|
|
//
|
2020-05-15 11:57:02 +08:00
|
|
|
// - A 24-bit RGB color written as `#RRGGBB` such as `'#778899'`.
|
|
|
|
//
|
|
|
|
// **NOTE:** You need to quote such values since an unquoted `#` char
|
|
|
|
// introduces a comment. So use `'bg-#778899'` not `bg-#778899`. If
|
|
|
|
// you omit the quotes the text after the `#` char is ignored which
|
|
|
|
// will likely result in an error or unexpected behavior.
|
2020-01-18 21:12:50 +08:00
|
|
|
//
|
2020-04-11 10:39:47 +08:00
|
|
|
// - A color name prefixed by `bg-` to set the background color.
|
2020-01-18 21:12:50 +08:00
|
|
|
//
|
2020-05-15 11:57:02 +08:00
|
|
|
// - A color name prefixed by `fg-` to set the foreground color. This has
|
|
|
|
// the same effect as specifying the color name without the `fg-` prefix.
|
|
|
|
//
|
2020-01-18 21:12:50 +08:00
|
|
|
// - A lambda that receives a styled segment as the only argument and returns a
|
2020-04-11 10:39:47 +08:00
|
|
|
// single styled segment.
|
2020-01-18 21:12:50 +08:00
|
|
|
//
|
|
|
|
// - A function with the same properties as the lambda (provided via the
|
2020-04-11 10:39:47 +08:00
|
|
|
// `$transformer~` syntax).
|
2020-01-18 21:12:50 +08:00
|
|
|
//
|
|
|
|
// When a styled text is converted to a string the corresponding
|
|
|
|
// [ANSI SGR code](https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_.28Select_Graphic_Rendition.29_parameters)
|
|
|
|
// is built to render the style.
|
|
|
|
//
|
|
|
|
// A styled text is nothing more than a wrapper around a list of styled segments.
|
|
|
|
// They can be accessed by indexing into it.
|
|
|
|
//
|
|
|
|
// ```elvish
|
|
|
|
// s = (styled abc red)(styled def green)
|
|
|
|
// put $s[0] $s[1]
|
|
|
|
// ```
|
|
|
|
|
2018-05-29 04:24:09 +08:00
|
|
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-01 07:56:33 +08:00
|
|
|
// Turns a string or ui.Segment into a new ui.Segment with the attributes
|
2018-05-29 04:24:09 +08:00
|
|
|
// from the supplied options applied to it. If the input is already a Segment its
|
|
|
|
// attributes are copied and modified.
|
2019-12-01 07:56:33 +08:00
|
|
|
func styledSegment(options RawOptions, input interface{}) (*ui.Segment, error) {
|
2018-05-29 04:24:09 +08:00
|
|
|
var text string
|
2019-12-01 07:56:33 +08:00
|
|
|
var style ui.Style
|
2018-05-29 04:24:09 +08:00
|
|
|
|
|
|
|
switch input := input.(type) {
|
|
|
|
case string:
|
|
|
|
text = input
|
2019-12-01 07:56:33 +08:00
|
|
|
case *ui.Segment:
|
2018-05-29 04:24:09 +08:00
|
|
|
text = input.Text
|
|
|
|
style = input.Style
|
|
|
|
default:
|
|
|
|
return nil, errStyledSegmentArgType
|
|
|
|
}
|
|
|
|
|
2020-04-01 07:39:02 +08:00
|
|
|
if err := style.MergeFromOptions(options); err != nil {
|
2018-05-29 04:24:09 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-12-01 07:56:33 +08:00
|
|
|
return &ui.Segment{
|
2018-05-29 04:24:09 +08:00
|
|
|
Text: text,
|
|
|
|
Style: style,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-12-01 20:36:26 +08:00
|
|
|
// Styled turns a string, a ui.Segment or a ui.Text into a ui.Text by applying
|
|
|
|
// the given stylings.
|
|
|
|
func Styled(fm *Frame, input interface{}, stylings ...interface{}) (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:
|
2019-12-01 07:56:33 +08:00
|
|
|
text = ui.Text{&ui.Segment{
|
2018-05-29 04:24:09 +08:00
|
|
|
Text: input,
|
2019-12-01 07:56:33 +08:00
|
|
|
Style: ui.Style{},
|
2018-05-29 04:24:09 +08:00
|
|
|
}}
|
2019-12-01 07:56:33 +08:00
|
|
|
case *ui.Segment:
|
|
|
|
text = ui.Text{input.Clone()}
|
|
|
|
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 {
|
|
|
|
return styling.Call(fm, []interface{}{seg}, NoOpts)
|
|
|
|
})
|
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
|
|
|
}
|