forked from elves/elvish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
segment.go
108 lines (92 loc) · 2.38 KB
/
segment.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package styled
import (
"bytes"
"fmt"
"strings"
"github.com/elves/elvish/eval/vals"
)
// Segment is a string that has some style applied to it.
type Segment struct {
Style
Text string
}
func (Segment) Kind() string { return "styled-segment" }
// Repr returns the representation of this Segment. The string can be used to
// construct an identical Segment. Unset or default attributes are skipped. If
// the Segment represents an unstyled string only this string is returned.
func (s Segment) Repr(indent int) string {
buf := new(bytes.Buffer)
addIfNotEqual := func(key string, val, cmp interface{}) {
if val != cmp {
fmt.Fprintf(buf, "&%s=%s ", key, vals.Repr(val, 0))
}
}
addIfNotEqual("fg-color", s.Foreground, "")
addIfNotEqual("bg-color", s.Background, "")
addIfNotEqual("bold", s.Bold, false)
addIfNotEqual("dim", s.Dim, false)
addIfNotEqual("italic", s.Italic, false)
addIfNotEqual("underlined", s.Underlined, false)
addIfNotEqual("blink", s.Blink, false)
addIfNotEqual("inverse", s.Inverse, false)
if buf.Len() == 0 {
return s.Text
}
return fmt.Sprintf("(styled-segment %s %s)", s.Text, strings.TrimSpace(buf.String()))
}
func (s Segment) IterateKeys(fn func(v interface{}) bool) {
vals.Feed(fn, "text", "fg-color", "bg-color", "bold", "dim", "italic", "underlined", "blink", "inverse")
}
// Index provides access to the attributes of the Segment.
func (s Segment) Index(k interface{}) (v interface{}, ok bool) {
switch k {
case "text":
v = s.Text
case "fg-color":
v = s.Foreground
case "bg-color":
v = s.Background
case "bold":
v = s.Bold
case "dim":
v = s.Dim
case "italic":
v = s.Italic
case "underlined":
v = s.Underlined
case "blink":
v = s.Blink
case "inverse":
v = s.Inverse
}
if v == "" {
v = "default"
}
return v, v != nil
}
// Concat implements Segment+string, Segment+Segment and Segment+Text.
func (s Segment) Concat(v interface{}) (interface{}, error) {
switch rhs := v.(type) {
case string:
return Text{
s,
Segment{Text: rhs},
}, nil
case *Segment:
return Text{s, *rhs}, nil
case *Text:
return Text(append([]Segment{s}, *rhs...)), nil
}
return nil, vals.ErrConcatNotImplemented
}
// RConcat implements string+Segment.
func (s Segment) RConcat(v interface{}) (interface{}, error) {
switch lhs := v.(type) {
case string:
return Text{
Segment{Text: lhs},
s,
}, nil
}
return nil, vals.ErrConcatNotImplemented
}