-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
styled.go
177 lines (153 loc) · 3.18 KB
/
styled.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package ui
import (
"fmt"
"strings"
"github.com/elves/elvish/eval/vals"
"github.com/elves/elvish/parse"
"github.com/xiaq/persistent/hash"
"github.com/xiaq/persistent/vector"
)
// Styled is a piece of text with style.
type Styled struct {
Text string
Styles Styles
}
var styleTranslationTable = map[string]string{
"bold": "1",
"dim": "2",
"italic": "3",
"underlined": "4",
"blink": "5",
"inverse": "7",
"black": "30",
"red": "31",
"green": "32",
"yellow": "33",
"blue": "34",
"magenta": "35",
"cyan": "36",
"lightgray": "37",
"gray": "90",
"lightred": "91",
"lightgreen": "92",
"lightyellow": "93",
"lightblue": "94",
"lightmagenta": "95",
"lightcyan": "96",
"white": "97",
"bg-default": "49",
"bg-black": "40",
"bg-red": "41",
"bg-green": "42",
"bg-yellow": "43",
"bg-blue": "44",
"bg-magenta": "45",
"bg-cyan": "46",
"bg-lightgray": "47",
"bg-gray": "100",
"bg-lightred": "101",
"bg-lightgreen": "102",
"bg-lightyellow": "103",
"bg-lightblue": "104",
"bg-lightmagenta": "105",
"bg-lightcyan": "106",
"bg-white": "107",
}
func Unstyled(s string) Styled {
return Styled{s, Styles{}}
}
func (s *Styled) Kind() string {
return "styled"
}
func (s *Styled) Equal(a interface{}) bool {
rhs, ok := a.(*Styled)
if !ok {
return false
}
return s.Text == rhs.Text && s.Styles.Eq(rhs.Styles)
}
func (s *Styled) Hash() uint32 {
h := hash.DJBInit
h = hash.DJBCombine(h, hash.String(s.Text))
h = hash.DJBCombine(h, s.Styles.Hash())
return h
}
func (s *Styled) String() string {
return "\033[" + s.Styles.String() + "m" + s.Text + "\033[m"
}
func (s *Styled) Repr(indent int) string {
quotedStyles := make([]string, len(s.Styles))
for i, st := range s.Styles {
quotedStyles[i] = parse.Quote(st)
}
return fmt.Sprintf("(edit:styled %s [%s]",
parse.Quote(s.Text), strings.Join(quotedStyles, " "))
}
func (s *Styled) Index(k interface{}) (interface{}, bool) {
switch k {
case "text":
return s.Text, true
case "styles":
li := vector.Empty
for _, st := range s.Styles {
li = li.Cons(st)
}
return li, true
default:
return nil, false
}
}
func (s *Styled) IterateKeys(f func(interface{}) bool) {
vals.Feed(f, "text", "styles")
}
type Styles []string
func (ss Styles) Eq(rhs Styles) bool {
if len(ss) != len(rhs) {
return false
}
for i, s := range ss {
if s != rhs[i] {
return false
}
}
return true
}
func (ss Styles) Hash() uint32 {
h := hash.DJBInit
for _, s := range ss {
h = hash.DJBCombine(h, hash.String(s))
}
return h
}
func JoinStyles(so Styles, st ...Styles) Styles {
for _, v := range st {
so = append(so, v...)
}
return so
}
func TranslateStyle(s string) string {
v, ok := styleTranslationTable[s]
if ok {
return v
}
return s
}
func StylesFromString(s string) Styles {
var st Styles
for _, v := range strings.Split(s, ";") {
st = append(st, v)
}
return st
}
func (s Styles) String() string {
var o string
for i, v := range s {
if len(v) > 0 {
if i > 0 {
o += ";"
}
o += TranslateStyle(v)
}
}
return o
}