-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattributedata.go
More file actions
281 lines (237 loc) · 8.6 KB
/
attributedata.go
File metadata and controls
281 lines (237 loc) · 8.6 KB
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
package xterm
// Ported from xterm.js src/common/buffer/AttributeData.ts.
// ExtendedAttrs holds extended cell attributes (underline style, underline color, URL ID).
type ExtendedAttrs struct {
ext uint32
urlID int
}
// NewExtendedAttrs creates an ExtendedAttrs with the given raw ext value and URL ID.
func NewExtendedAttrs(ext uint32, urlID int) *ExtendedAttrs {
return &ExtendedAttrs{ext: ext, urlID: urlID}
}
// Ext returns the raw extended attributes value. If a URL is set, the underline
// style bits are overridden to DASHED.
func (e *ExtendedAttrs) Ext() uint32 {
if e.urlID != 0 {
return (e.ext & ^ExtFlagUnderlineStyle) | (uint32(UnderlineStyleDashed) << 26)
}
return e.ext
}
// SetExt sets the raw extended attributes value.
func (e *ExtendedAttrs) SetExt(v uint32) {
e.ext = v
}
// UnderlineStyle returns the underline style. Returns DASHED if a URL is active.
func (e *ExtendedAttrs) UnderlineStyle() UnderlineStyle {
if e.urlID != 0 {
return UnderlineStyleDashed
}
return UnderlineStyle((e.ext & ExtFlagUnderlineStyle) >> 26)
}
// SetUnderlineStyle sets the underline style bits.
func (e *ExtendedAttrs) SetUnderlineStyle(v UnderlineStyle) {
e.ext &= ^ExtFlagUnderlineStyle
e.ext |= (uint32(v) << 26) & ExtFlagUnderlineStyle
}
// UnderlineColor returns the underline color (color mode + RGB/palette bits).
func (e *ExtendedAttrs) UnderlineColor() uint32 {
return e.ext & (AttrCMMask | AttrRGBMask)
}
// SetUnderlineColor sets the underline color bits.
func (e *ExtendedAttrs) SetUnderlineColor(v uint32) {
e.ext &= ^(AttrCMMask | AttrRGBMask)
e.ext |= v & (AttrCMMask | AttrRGBMask)
}
// URLID returns the OSC hyperlink ID.
func (e *ExtendedAttrs) URLID() int {
return e.urlID
}
// SetURLID sets the OSC hyperlink ID.
func (e *ExtendedAttrs) SetURLID(v int) {
e.urlID = v
}
// UnderlineVariantOffset returns the variant offset (3-bit unsigned from bits 29-31).
func (e *ExtendedAttrs) UnderlineVariantOffset() int {
val := int32(e.ext&ExtFlagVariantOffset) >> 29
if val < 0 {
return int(val ^ (^int32(0) << 3))
}
return int(val)
}
// SetUnderlineVariantOffset sets the variant offset bits.
func (e *ExtendedAttrs) SetUnderlineVariantOffset(v int) {
e.ext &= ^ExtFlagVariantOffset
e.ext |= (uint32(v) << 29) & ExtFlagVariantOffset
}
// Clone returns a deep copy.
func (e *ExtendedAttrs) Clone() *ExtendedAttrs {
return &ExtendedAttrs{ext: e.ext, urlID: e.urlID}
}
// IsEmpty returns true if the extended attrs carry no meaningful data.
func (e *ExtendedAttrs) IsEmpty() bool {
return e.UnderlineStyle() == UnderlineStyleNone && e.urlID == 0
}
// AttributeData holds the fg, bg, and extended attributes for a cell.
type AttributeData struct {
Fg uint32
Bg uint32
Extended *ExtendedAttrs
}
// DefaultAttrData returns an AttributeData with default (zero) values.
func DefaultAttrData() AttributeData {
return AttributeData{Extended: &ExtendedAttrs{}}
}
// Clone returns a deep copy.
func (a *AttributeData) Clone() AttributeData {
return AttributeData{
Fg: a.Fg,
Bg: a.Bg,
Extended: a.extended().Clone(),
}
}
func (a *AttributeData) extended() *ExtendedAttrs {
if a.Extended == nil {
a.Extended = &ExtendedAttrs{}
}
return a.Extended
}
// --- Static helpers ---
// ToColorRGB extracts an RGB triple from a packed color value.
func ToColorRGB(v uint32) ColorRGB {
return ColorRGB{
uint8((v >> AttrRedShift) & 0xFF),
uint8((v >> AttrGreenShift) & 0xFF),
uint8(v & 0xFF),
}
}
// FromColorRGB packs an RGB triple into a uint32.
func FromColorRGB(c ColorRGB) uint32 {
return (uint32(c[0])&0xFF)<<AttrRedShift |
(uint32(c[1])&0xFF)<<AttrGreenShift |
uint32(c[2])&0xFF
}
// --- Flag queries (return non-zero if set, matching xterm.js convention) ---
func (a *AttributeData) IsInverse() uint32 { return a.Fg & FgFlagInverse }
func (a *AttributeData) IsBold() uint32 { return a.Fg & FgFlagBold }
func (a *AttributeData) IsBlink() uint32 { return a.Fg & FgFlagBlink }
func (a *AttributeData) IsInvisible() uint32 { return a.Fg & FgFlagInvisible }
func (a *AttributeData) IsStrikethrough() uint32 { return a.Fg & FgFlagStrikethrough }
func (a *AttributeData) IsItalic() uint32 { return a.Bg & BgFlagItalic }
func (a *AttributeData) IsDim() uint32 { return a.Bg & BgFlagDim }
func (a *AttributeData) IsProtected() uint32 { return a.Bg & BgFlagProtected }
func (a *AttributeData) IsOverline() uint32 { return a.Bg & BgFlagOverline }
// IsUnderline checks both the fg underline flag and extended underline style.
func (a *AttributeData) IsUnderline() uint32 {
if a.HasExtendedAttrs() != 0 && a.extended().UnderlineStyle() != UnderlineStyleNone {
return 1
}
return a.Fg & FgFlagUnderline
}
// --- Color mode queries ---
func (a *AttributeData) GetFgColorMode() uint32 { return a.Fg & AttrCMMask }
func (a *AttributeData) GetBgColorMode() uint32 { return a.Bg & AttrCMMask }
func (a *AttributeData) IsFgRGB() bool { return (a.Fg & AttrCMMask) == AttrCMRGB }
func (a *AttributeData) IsBgRGB() bool { return (a.Bg & AttrCMMask) == AttrCMRGB }
func (a *AttributeData) IsFgDefault() bool { return (a.Fg & AttrCMMask) == 0 }
func (a *AttributeData) IsBgDefault() bool { return (a.Bg & AttrCMMask) == 0 }
func (a *AttributeData) IsFgPalette() bool {
cm := a.Fg & AttrCMMask
return cm == AttrCMP16 || cm == AttrCMP256
}
func (a *AttributeData) IsBgPalette() bool {
cm := a.Bg & AttrCMMask
return cm == AttrCMP16 || cm == AttrCMP256
}
func (a *AttributeData) IsAttributeDefault() bool {
return a.Fg == 0 && a.Bg == 0
}
// --- Color value queries ---
// GetFgColor returns the foreground color value. Returns -1 for default color mode.
func (a *AttributeData) GetFgColor() int {
switch a.Fg & AttrCMMask {
case AttrCMP16, AttrCMP256:
return int(a.Fg & AttrPColorMask)
case AttrCMRGB:
return int(a.Fg & AttrRGBMask)
default:
return -1
}
}
// GetBgColor returns the background color value. Returns -1 for default color mode.
func (a *AttributeData) GetBgColor() int {
switch a.Bg & AttrCMMask {
case AttrCMP16, AttrCMP256:
return int(a.Bg & AttrPColorMask)
case AttrCMRGB:
return int(a.Bg & AttrRGBMask)
default:
return -1
}
}
// --- Extended attributes ---
func (a *AttributeData) HasExtendedAttrs() uint32 { return a.Bg & BgFlagHasExtended }
// UpdateExtended sets or clears the HAS_EXTENDED flag based on whether extended attrs are empty.
func (a *AttributeData) UpdateExtended() {
if a.extended().IsEmpty() {
a.Bg &= ^BgFlagHasExtended
} else {
a.Bg |= BgFlagHasExtended
}
}
// GetUnderlineColor returns the underline color value, falling back to fg color.
func (a *AttributeData) GetUnderlineColor() int {
if (a.Bg&BgFlagHasExtended) != 0 && ^a.extended().UnderlineColor() != 0 {
switch a.extended().UnderlineColor() & AttrCMMask {
case AttrCMP16, AttrCMP256:
return int(a.extended().UnderlineColor() & AttrPColorMask)
case AttrCMRGB:
return int(a.extended().UnderlineColor() & AttrRGBMask)
default:
return a.GetFgColor()
}
}
return a.GetFgColor()
}
// GetUnderlineColorMode returns the color mode of the underline color, falling back to fg.
func (a *AttributeData) GetUnderlineColorMode() uint32 {
if (a.Bg&BgFlagHasExtended) != 0 && ^a.extended().UnderlineColor() != 0 {
return a.extended().UnderlineColor() & AttrCMMask
}
return a.GetFgColorMode()
}
// IsUnderlineColorRGB returns whether the underline color is RGB.
func (a *AttributeData) IsUnderlineColorRGB() bool {
if (a.Bg&BgFlagHasExtended) != 0 && ^a.extended().UnderlineColor() != 0 {
return (a.extended().UnderlineColor() & AttrCMMask) == AttrCMRGB
}
return a.IsFgRGB()
}
// IsUnderlineColorPalette returns whether the underline color is a palette color.
func (a *AttributeData) IsUnderlineColorPalette() bool {
if (a.Bg&BgFlagHasExtended) != 0 && ^a.extended().UnderlineColor() != 0 {
cm := a.extended().UnderlineColor() & AttrCMMask
return cm == AttrCMP16 || cm == AttrCMP256
}
return a.IsFgPalette()
}
// IsUnderlineColorDefault returns whether the underline color is the default.
func (a *AttributeData) IsUnderlineColorDefault() bool {
if (a.Bg&BgFlagHasExtended) != 0 && ^a.extended().UnderlineColor() != 0 {
return (a.extended().UnderlineColor() & AttrCMMask) == 0
}
return a.IsFgDefault()
}
// GetUnderlineStyle returns the effective underline style.
func (a *AttributeData) GetUnderlineStyle() UnderlineStyle {
if a.Fg&FgFlagUnderline == 0 {
return UnderlineStyleNone
}
if a.Bg&BgFlagHasExtended != 0 {
return a.extended().UnderlineStyle()
}
return UnderlineStyleSingle
}
// GetUnderlineVariantOffset returns the underline variant offset from extended attrs.
func (a *AttributeData) GetUnderlineVariantOffset() int {
return a.extended().UnderlineVariantOffset()
}