package ui
import (
)
type Segment struct {
Style
Text string
}
func (*Segment) () string { return "ui:text-segment" }
func ( *Segment) (int) string {
:= new(bytes.Buffer)
:= func( string, , interface{}) {
if != {
var string
if , := .(Color); {
= .String()
} else {
= vals.Repr(, 0)
}
fmt.Fprintf(, "&%s=%s ", , )
}
}
("fg-color", .Foreground, nil)
("bg-color", .Background, nil)
("bold", .Bold, false)
("dim", .Dim, false)
("italic", .Italic, false)
("underlined", .Underlined, false)
("blink", .Blink, false)
("inverse", .Inverse, false)
if .Len() == 0 {
return .Text
}
return fmt.Sprintf("(ui:text-segment %s %s)", .Text, strings.TrimSpace(.String()))
}
func (*Segment) ( func( interface{}) bool) {
vals.Feed(, "text", "fg-color", "bg-color", "bold", "dim", "italic", "underlined", "blink", "inverse")
}
func ( *Segment) ( interface{}) ( interface{}, bool) {
switch {
case "text":
= .Text
case "fg-color":
if .Foreground == nil {
return "default", true
}
return .Foreground.String(), true
case "bg-color":
if .Background == nil {
return "default", true
}
return .Background.String(), true
case "bold":
= .Bold
case "dim":
= .Dim
case "italic":
= .Italic
case "underlined":
= .Underlined
case "blink":
= .Blink
case "inverse":
= .Inverse
}
return , != nil
}
func ( *Segment) ( interface{}) (interface{}, error) {
switch rhs := .(type) {
case string:
return Text{
,
&Segment{Text: },
}, nil
case float64:
return Text{
,
&Segment{Text: vals.ToString()},
}, nil
case *Segment:
return Text{, }, nil
case Text:
return Text(append([]*Segment{}, ...)), nil
}
return nil, vals.ErrConcatNotImplemented
}
func ( *Segment) ( interface{}) (interface{}, error) {
switch lhs := .(type) {
case string:
return Text{
&Segment{Text: },
,
}, nil
case float64:
return Text{
&Segment{Text: vals.ToString()},
,
}, nil
}
return nil, vals.ErrConcatNotImplemented
}
func ( *Segment) () *Segment {
:= *
return &
}
func ( *Segment) ( rune) int {
return strings.Count(.Text, string())
}
func ( *Segment) ( rune) []*Segment {
:= strings.Split(.Text, string())
:= make([]*Segment, len())
for , := range {
[] = &Segment{.Style, }
}
return
}
func ( *Segment) () string {
return .VTString()
}
func ( *Segment) () string {
:= .SGR()
if == "" {
return .Text
}
return fmt.Sprintf("\033[%sm%s\033[m", , .Text)
}