-
Notifications
You must be signed in to change notification settings - Fork 0
/
style.go
291 lines (254 loc) · 7.67 KB
/
style.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright (c) 2022 Stephan Lukits. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package lines
import (
"sort"
"github.com/slukits/lines/internal/api"
)
// StyleAttributeMask defines the looks of a rune on the screen; e.g.
// bold, blinking, dimmed etc.
type StyleAttributeMask = api.StyleAttributeMask
// The DefaultStyle has no style attributes and "default" colors. The
// semantics of the later is determined by the backend implementation.
// Use the With* methods to create new styles from the default style:
//
// myStyle := lines.DefaultStyle.WithFG(lines.Yellow).WithBG(lines.Blue)
var DefaultStyle = api.DefaultStyle
const (
Bold StyleAttributeMask = api.Bold
Blink StyleAttributeMask = api.Blink
Reverse StyleAttributeMask = api.Reverse
Underline StyleAttributeMask = api.Underline
Dim StyleAttributeMask = api.Dim
Italic StyleAttributeMask = api.Italic
StrikeThrough StyleAttributeMask = api.StrikeThrough
Invalid StyleAttributeMask = api.Invalid
ZeroStyle StyleAttributeMask = api.ZeroStyle
)
var StyleAttributeNames = map[StyleAttributeMask]string{
Bold: "bold",
Blink: "blink",
Reverse: "reverse",
Underline: "underline",
Dim: "dim",
Italic: "italic",
StrikeThrough: "strike through",
Invalid: "invalid",
}
// Style represents what a print to the screen should look like. A new
// Style instance has fore- and background color set to black. Use its
// With* methods to create a style with desired properties:
//
// myStyle := (lines.Style{}).WithFG(lines.White)
type Style = api.Style
// NewStyle creates a new style with given style attributes and given fore-
// and background color:
//
// myStyle := lines.NewStyle(lines.Bold|lines.Dim, lines.White, lines.Black)
var NewStyle = api.NewStyle
// Range is a two component array of which the first component
// represents the (inclusive) start of a range while the seconde is the
// (exclusive) end of the range. Range is typically used to define
// style ranges for rune-sequences in a [Line] (see [Line.AddStyleRange]).
type Range [2]int
// Start returns the inclusive start-point of given range r.
func (r Range) Start() int { return r[0] }
// End returns the exclusive end-point of given range r.
func (r Range) End() int { return r[1] }
// copy returns a copy of given range r.
func (r Range) copy() Range {
return Range{r[0], r[1]}
}
// shift increases start and and index by given s.
func (r Range) shift(s int) Range {
return Range{r[0] + s, r[1] + s}
}
// expand returns a new with the same starting point as given range r
// having its end-point increased by.
func (r Range) expand(by int) Range {
return Range{r[0], r[1] + by}
}
// contains returns true if given i is in the style range r
// [r.Start,r.End[.
func (r Range) contains(i int) bool {
return r.Start() <= i && i < r.End()
}
// SR represents a ranged style which is used internally to style a
// sequence of runes in a [Component]'s [Line] or may be passed to
// [Line.AddStyleRange] calls.
type SR struct {
Range
Style
}
// Increment the start position of embedded range.
func (sr *SR) IncrementStart() { sr.Range[0] = sr.Range[0] + 1 }
// SetEnd sets the endpoint of embedded range.
func (sr *SR) SetEnd(x int) { sr.Range[1] = x }
// zeroRange is used in StyleRanges instance for its default style since
// it contains no position.
var zeroRange = Range{0, 0}
// styleRanges maps a set of Range instances to their styles and is used
// to determine the style at a particular rune position. If asked for
// the style at certain position and no style range is found containing
// that position the default style is returned.
type styleRanges map[Range]Style
// newStyleRanges creates a new style ranges instance having given style
// set as default style.
func newStyleRanges(dflt Style) styleRanges {
return styleRanges{zeroRange: dflt}
}
// defaultStyle returns given style ranges s' default style.
func (s styleRanges) defaultStyle() Style {
if _, ok := s[zeroRange]; !ok {
return DefaultStyle
}
return s[zeroRange]
}
func (s styleRanges) ensureDefaultStyle() Style {
if _, ok := s[zeroRange]; !ok {
s[zeroRange] = DefaultStyle
}
return s[zeroRange]
}
// withAA ensures that given style ranges s have a default style and that
// it is set to given style attributes.
func (s styleRanges) withAA(aa StyleAttributeMask) {
s[zeroRange] = s.ensureDefaultStyle().WithAA(aa)
}
// withFG ensures that given style ranges s have a default style and
// that its foreground color is set to given color.
func (s styleRanges) withFG(c Color) {
s[zeroRange] = s[zeroRange].WithFG(c)
}
// withBG ensures that given style ranges s have a default style and
// that its background color is set to given color.
func (s styleRanges) withBG(c Color) {
s[zeroRange] = s[zeroRange].WithBG(c)
}
// copy returns a shallow copy of given style ranges s.
func (s styleRanges) copy() styleRanges {
if s == nil {
return nil
}
cp := styleRanges{}
for r, s := range s {
cp[r.copy()] = s
}
return cp
}
// add adds to given style ranges sr given range r and style s iff r
// doesn't overlap any ranges in sr.
func (sr styleRanges) add(r Range, s Style) {
if sr.isOverlapping(r) {
return
}
sr[r] = s
}
// isOverlapping returns true if given range r's start- or end-point is
// contained in a range of given style ranges sr; false otherwise.
func (sr styleRanges) isOverlapping(r Range) bool {
for r := range sr {
if r.contains(r.Start()) || r.contains(r.End()) {
return true
}
}
return false
}
// copyWithDefault returns a shallow copy of given style ranges s
// setting given style dflt as its default style iff s doesn't have a
// default.
func (s styleRanges) copyWithDefault(dflt Style) styleRanges {
cp := s.copy()
if cp == nil {
return styleRanges{zeroRange: dflt}
}
if _, ok := cp[zeroRange]; !ok {
cp[zeroRange] = dflt
}
return cp
}
func (ss styleRanges) unstyled(start, end int) []Range {
if start >= end || start < 0 {
return nil
}
rr := []Range{{start, end}}
for _, r := range ss.orderedRanges() {
last := rr[len(rr)-1]
if r.Start() <= last.Start() && r.End() >= last.End() {
return rr[:len(rr)-1]
}
if r.Start() > last.Start() && r.End() < last.End() {
rr[len(rr)-1] = Range{last.Start(), r.Start()}
rr = append(rr, Range{r.End(), last.End()})
continue
}
if r.Start() <= last.Start() {
rr[len(rr)-1][0] = r.End()
continue
}
rr[len(rr)-1][1] = r.Start()
break
}
return rr
}
func (ss styleRanges) orderedRanges() []Range {
rr := []Range{}
for r := range ss {
if r == zeroRange {
continue
}
rr = append(rr, r)
}
sort.Slice(rr, func(i, j int) bool {
return rr[i].Start() < rr[j].Start()
})
return rr
}
// expand finds in given style ranges s the style range containing point
// at and increases its end point by.
func (s styleRanges) expand(at, by int) {
if s == nil {
return
}
update := map[Range]Range{}
for r := range s {
if r.End() <= at {
continue
}
switch {
case r.contains(at):
update[r] = r.expand(by)
default:
update[r] = r.shift(by)
}
}
for k, u := range update {
if u.Start() < u.End() {
s[u] = s[k]
}
delete(s, k)
}
}
// contract finds in given style ranges s the style range containing point
// at and decreases its end point by.
func (s styleRanges) contract(at, by int) {
s.expand(at, -by)
}
// of finds in given style ranges s the range containing given cell and
// returns mapping style.
func (s styleRanges) of(cell int) Style {
if len(s) == 0 {
return api.DefaultStyle
}
for r := range s {
if !r.contains(cell) {
continue
}
return s[r]
}
if s, ok := s[zeroRange]; ok {
return s
}
return api.DefaultStyle
}