style.go RawHistoryFolder
1package core
2
3type RGB struct {
4 R uint8
5 G uint8
6 B uint8
7}
8
9type StyleAttrs uint16
10
11const (
12 AttrDim StyleAttrs = 1 << iota
13 AttrUnderline
14 AttrBlink
15 AttrStrikethrough
16)
17
18type UnderlineStyle uint8
19
20const (
21 UnderlineNone UnderlineStyle = iota
22 UnderlineSingle
23 UnderlineDouble
24 UnderlineCurly
25 UnderlineDotted
26 UnderlineDashed
27)
28
29type Style struct {
30 Bold bool
31 Italic bool
32 Inverse bool
33 Attrs StyleAttrs
34 Foreground int // -1 means unset; ignored when HasForegroundRGB is true
35 Background int // -1 means unset; ignored when HasBackgroundRGB is true
36
37 HasForegroundRGB bool
38 ForegroundRGB RGB
39 HasBackgroundRGB bool
40 BackgroundRGB RGB
41
42 UnderlineStyle UnderlineStyle
43 HasUnderlineColor bool
44 UnderlineColor int // ignored when HasUnderlineColorRGB is true
45 HasUnderlineColorRGB bool
46 UnderlineColorRGB RGB
47}
48
49func DefaultStyle() Style { return Style{Foreground: -1, Background: -1} }
50
51// Canonical returns the unique representation of s under Equal. Fields that
52// are inactive for the selected color form are cleared. Canonical does not
53// treat the zero Style as the default style: indexed color zero remains
54// distinct from the unset colors returned by DefaultStyle.
55func (s Style) Canonical() Style {
56 if s.HasForegroundRGB {
57 s.Foreground = 0
58 } else {
59 s.ForegroundRGB = RGB{}
60 }
61 if s.HasBackgroundRGB {
62 s.Background = 0
63 } else {
64 s.BackgroundRGB = RGB{}
65 }
66 if s.HasUnderlineColorRGB {
67 s.HasUnderlineColor = false
68 s.UnderlineColor = 0
69 } else {
70 s.UnderlineColorRGB = RGB{}
71 if !s.HasUnderlineColor {
72 s.UnderlineColor = 0
73 }
74 }
75 return s
76}
77
78func (s Style) Equal(other Style) bool {
79 if s.Bold != other.Bold || s.Italic != other.Italic || s.Inverse != other.Inverse || s.Attrs != other.Attrs || s.UnderlineStyle != other.UnderlineStyle {
80 return false
81 }
82 if s.HasForegroundRGB != other.HasForegroundRGB || s.HasBackgroundRGB != other.HasBackgroundRGB || s.HasUnderlineColorRGB != other.HasUnderlineColorRGB {
83 return false
84 }
85 if s.HasForegroundRGB {
86 if s.ForegroundRGB != other.ForegroundRGB {
87 return false
88 }
89 } else if s.Foreground != other.Foreground {
90 return false
91 }
92 if s.HasBackgroundRGB {
93 if s.BackgroundRGB != other.BackgroundRGB {
94 return false
95 }
96 } else if s.Background != other.Background {
97 return false
98 }
99 if s.HasUnderlineColorRGB {
100 return s.UnderlineColorRGB == other.UnderlineColorRGB
101 }
102 if s.HasUnderlineColor != other.HasUnderlineColor {
103 return false
104 }
105 if s.HasUnderlineColor {
106 return s.UnderlineColor == other.UnderlineColor
107 }
108 return true
109}