vendor/github.com/charmbracelet/x/ansi/style.go
296 lines · 8968 bytes
1package ansi
2
3import (
4 "image/color"
5 "strconv"
6 "strings"
7)
8
9// ResetStyle is a SGR (Select Graphic Rendition) style sequence that resets
10// all attributes.
11// See: https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters
12const ResetStyle = "\x1b[m"
13
14// Attr is a SGR (Select Graphic Rendition) style attribute.
15type Attr = string
16
17// Style represents an ANSI SGR (Select Graphic Rendition) style.
18type Style []Attr
19
20// String returns the ANSI SGR (Select Graphic Rendition) style sequence for
21// the given style.
22func (s Style) String() string {
23 if len(s) == 0 {
24 return ResetStyle
25 }
26 return "\x1b[" + strings.Join(s, ";") + "m"
27}
28
29// Styled returns a styled string with the given style applied.
30func (s Style) Styled(str string) string {
31 if len(s) == 0 {
32 return str
33 }
34 return s.String() + str + ResetStyle
35}
36
37// Reset appends the reset style attribute to the style.
38func (s Style) Reset() Style {
39 return append(s, ResetAttr)
40}
41
42// Bold appends the bold style attribute to the style.
43func (s Style) Bold() Style {
44 return append(s, BoldAttr)
45}
46
47// Faint appends the faint style attribute to the style.
48func (s Style) Faint() Style {
49 return append(s, FaintAttr)
50}
51
52// Italic appends the italic style attribute to the style.
53func (s Style) Italic() Style {
54 return append(s, ItalicAttr)
55}
56
57// Underline appends the underline style attribute to the style.
58func (s Style) Underline() Style {
59 return append(s, UnderlineAttr)
60}
61
62// DoubleUnderline appends the double underline style attribute to the style.
63func (s Style) DoubleUnderline() Style {
64 return append(s, DoubleUnderlineAttr)
65}
66
67// CurlyUnderline appends the curly underline style attribute to the style.
68func (s Style) CurlyUnderline() Style {
69 return append(s, CurlyUnderlineAttr)
70}
71
72// DottedUnderline appends the dotted underline style attribute to the style.
73func (s Style) DottedUnderline() Style {
74 return append(s, DottedUnderlineAttr)
75}
76
77// DashedUnderline appends the dashed underline style attribute to the style.
78func (s Style) DashedUnderline() Style {
79 return append(s, DashedUnderlineAttr)
80}
81
82// SlowBlink appends the slow blink style attribute to the style.
83func (s Style) SlowBlink() Style {
84 return append(s, SlowBlinkAttr)
85}
86
87// RapidBlink appends the rapid blink style attribute to the style.
88func (s Style) RapidBlink() Style {
89 return append(s, RapidBlinkAttr)
90}
91
92// Reverse appends the reverse style attribute to the style.
93func (s Style) Reverse() Style {
94 return append(s, ReverseAttr)
95}
96
97// Conceal appends the conceal style attribute to the style.
98func (s Style) Conceal() Style {
99 return append(s, ConcealAttr)
100}
101
102// Strikethrough appends the strikethrough style attribute to the style.
103func (s Style) Strikethrough() Style {
104 return append(s, StrikethroughAttr)
105}
106
107// NoBold appends the no bold style attribute to the style.
108func (s Style) NoBold() Style {
109 return append(s, NoBoldAttr)
110}
111
112// NormalIntensity appends the normal intensity style attribute to the style.
113func (s Style) NormalIntensity() Style {
114 return append(s, NormalIntensityAttr)
115}
116
117// NoItalic appends the no italic style attribute to the style.
118func (s Style) NoItalic() Style {
119 return append(s, NoItalicAttr)
120}
121
122// NoUnderline appends the no underline style attribute to the style.
123func (s Style) NoUnderline() Style {
124 return append(s, NoUnderlineAttr)
125}
126
127// NoBlink appends the no blink style attribute to the style.
128func (s Style) NoBlink() Style {
129 return append(s, NoBlinkAttr)
130}
131
132// NoReverse appends the no reverse style attribute to the style.
133func (s Style) NoReverse() Style {
134 return append(s, NoReverseAttr)
135}
136
137// NoConceal appends the no conceal style attribute to the style.
138func (s Style) NoConceal() Style {
139 return append(s, NoConcealAttr)
140}
141
142// NoStrikethrough appends the no strikethrough style attribute to the style.
143func (s Style) NoStrikethrough() Style {
144 return append(s, NoStrikethroughAttr)
145}
146
147// DefaultForegroundColor appends the default foreground color style attribute to the style.
148func (s Style) DefaultForegroundColor() Style {
149 return append(s, DefaultForegroundColorAttr)
150}
151
152// DefaultBackgroundColor appends the default background color style attribute to the style.
153func (s Style) DefaultBackgroundColor() Style {
154 return append(s, DefaultBackgroundColorAttr)
155}
156
157// DefaultUnderlineColor appends the default underline color style attribute to the style.
158func (s Style) DefaultUnderlineColor() Style {
159 return append(s, DefaultUnderlineColorAttr)
160}
161
162// ForegroundColor appends the foreground color style attribute to the style.
163func (s Style) ForegroundColor(c Color) Style {
164 return append(s, ForegroundColorAttr(c))
165}
166
167// BackgroundColor appends the background color style attribute to the style.
168func (s Style) BackgroundColor(c Color) Style {
169 return append(s, BackgroundColorAttr(c))
170}
171
172// UnderlineColor appends the underline color style attribute to the style.
173func (s Style) UnderlineColor(c Color) Style {
174 return append(s, UnderlineColorAttr(c))
175}
176
177// SGR (Select Graphic Rendition) style attributes.
178// See: https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters
179const (
180 ResetAttr Attr = "0"
181 BoldAttr Attr = "1"
182 FaintAttr Attr = "2"
183 ItalicAttr Attr = "3"
184 UnderlineAttr Attr = "4"
185 DoubleUnderlineAttr Attr = "4:2"
186 CurlyUnderlineAttr Attr = "4:3"
187 DottedUnderlineAttr Attr = "4:4"
188 DashedUnderlineAttr Attr = "4:5"
189 SlowBlinkAttr Attr = "5"
190 RapidBlinkAttr Attr = "6"
191 ReverseAttr Attr = "7"
192 ConcealAttr Attr = "8"
193 StrikethroughAttr Attr = "9"
194 NoBoldAttr Attr = "21" // Some terminals treat this as double underline.
195 NormalIntensityAttr Attr = "22"
196 NoItalicAttr Attr = "23"
197 NoUnderlineAttr Attr = "24"
198 NoBlinkAttr Attr = "25"
199 NoReverseAttr Attr = "27"
200 NoConcealAttr Attr = "28"
201 NoStrikethroughAttr Attr = "29"
202 DefaultForegroundColorAttr Attr = "39"
203 DefaultBackgroundColorAttr Attr = "49"
204 DefaultUnderlineColorAttr Attr = "59"
205)
206
207// ForegroundColorAttr returns the style SGR attribute for the given foreground
208// color.
209// See: https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters
210func ForegroundColorAttr(c Color) Attr {
211 switch c := c.(type) {
212 case BasicColor:
213 // 3-bit or 4-bit ANSI foreground
214 // "3<n>" or "9<n>" where n is the color number from 0 to 7
215 if c < 8 {
216 return "3" + string('0'+c)
217 } else if c < 16 {
218 return "9" + string('0'+c-8)
219 }
220 case ExtendedColor:
221 // 256-color ANSI foreground
222 // "38;5;<n>"
223 return "38;5;" + strconv.FormatUint(uint64(c), 10)
224 case TrueColor, color.Color:
225 // 24-bit "true color" foreground
226 // "38;2;<r>;<g>;<b>"
227 r, g, b, _ := c.RGBA()
228 return "38;2;" +
229 strconv.FormatUint(uint64(shift(r)), 10) + ";" +
230 strconv.FormatUint(uint64(shift(g)), 10) + ";" +
231 strconv.FormatUint(uint64(shift(b)), 10)
232 }
233 return DefaultForegroundColorAttr
234}
235
236// BackgroundColorAttr returns the style SGR attribute for the given background
237// color.
238// See: https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters
239func BackgroundColorAttr(c Color) Attr {
240 switch c := c.(type) {
241 case BasicColor:
242 // 3-bit or 4-bit ANSI foreground
243 // "4<n>" or "10<n>" where n is the color number from 0 to 7
244 if c < 8 {
245 return "4" + string('0'+c)
246 } else {
247 return "10" + string('0'+c-8)
248 }
249 case ExtendedColor:
250 // 256-color ANSI foreground
251 // "48;5;<n>"
252 return "48;5;" + strconv.FormatUint(uint64(c), 10)
253 case TrueColor, color.Color:
254 // 24-bit "true color" foreground
255 // "38;2;<r>;<g>;<b>"
256 r, g, b, _ := c.RGBA()
257 return "48;2;" +
258 strconv.FormatUint(uint64(shift(r)), 10) + ";" +
259 strconv.FormatUint(uint64(shift(g)), 10) + ";" +
260 strconv.FormatUint(uint64(shift(b)), 10)
261 }
262 return DefaultBackgroundColorAttr
263}
264
265// UnderlineColorAttr returns the style SGR attribute for the given underline
266// color.
267// See: https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters
268func UnderlineColorAttr(c Color) Attr {
269 switch c := c.(type) {
270 // NOTE: we can't use 3-bit and 4-bit ANSI color codes with underline
271 // color, use 256-color instead.
272 //
273 // 256-color ANSI underline color
274 // "58;5;<n>"
275 case BasicColor:
276 return "58;5;" + strconv.FormatUint(uint64(c), 10)
277 case ExtendedColor:
278 return "58;5;" + strconv.FormatUint(uint64(c), 10)
279 case TrueColor, color.Color:
280 // 24-bit "true color" foreground
281 // "38;2;<r>;<g>;<b>"
282 r, g, b, _ := c.RGBA()
283 return "58;2;" +
284 strconv.FormatUint(uint64(shift(r)), 10) + ";" +
285 strconv.FormatUint(uint64(shift(g)), 10) + ";" +
286 strconv.FormatUint(uint64(shift(b)), 10)
287 }
288 return DefaultUnderlineColorAttr
289}
290
291func shift(v uint32) uint32 {
292 if v > 0xff {
293 return v >> 8
294 }
295 return v
296}