| 1 |
package color |
| 2 |
|
| 3 |
// Attribute represents a single SGR (Select Graphic Rendition) code used for |
| 4 |
// styling terminal text output. It is an integer type that corresponds to |
| 5 |
// specific ANSI escape sequence parameter values for controlling text |
| 6 |
// appearance, including colors, formatting, and visual effects. |
| 7 |
// |
| 8 |
// Attribute values are designed to be combined when constructing ANSI escape |
| 9 |
// sequences. The zero value (Reset) clears all formatting and returns text |
| 10 |
// to the default terminal appearance. |
| 11 |
// |
| 12 |
// Attributes are used in conjunction with the Color type to create reusable |
| 13 |
// styling configurations that can be applied to strings for colored terminal |
| 14 |
// output. |
| 15 |
type Attribute int |
| 16 |
|
| 17 |
// Internal constants used for constructing ANSI escape sequences and |
| 18 |
// managing buffer allocations. |
| 19 |
const ( |
| 20 |
// escape is the ANSI escape character (ESC, 0x1b) used to begin |
| 21 |
// escape sequences for terminal control and styling. |
| 22 |
escape = "\x1b" |
| 23 |
|
| 24 |
// builderGrowthFactor specifies the multiplier used for pre-allocating |
| 25 |
// strings.Builder capacity when constructing escape sequences. This |
| 26 |
// value provides a reasonable buffer for typical SGR sequences |
| 27 |
// containing multiple attributes. |
| 28 |
builderGrowthFactor = 4 |
| 29 |
|
| 30 |
// rgbColorFormatSpecifier is the SGR parameter value (2) that indicates |
| 31 |
// the following parameters specify a 24-bit RGB color using the format |
| 32 |
// "38;2;R;G;B" for foreground or "48;2;R;G;B" for background colors. |
| 33 |
rgbColorFormatSpecifier Attribute = 2 |
| 34 |
) |
| 35 |
|
| 36 |
// Base text formatting attributes. |
| 37 |
// These attributes control fundamental text styling such as boldness, |
| 38 |
// italics, underlining, and visibility. Multiple base attributes can be |
| 39 |
// combined to create complex text formatting. |
| 40 |
// |
| 41 |
// Base attributes start at SGR code 0 (Reset) and increment through 9. |
| 42 |
const ( |
| 43 |
// Reset clears all text formatting and returns to the default terminal |
| 44 |
// appearance. This is typically used at the end of styled text to |
| 45 |
// prevent formatting from bleeding into subsequent output. |
| 46 |
Reset Attribute = iota |
| 47 |
|
| 48 |
// Bold applies increased intensity to the text, making it appear |
| 49 |
// thicker and more prominent. Not all terminal emulators support this. |
| 50 |
Bold |
| 51 |
|
| 52 |
// Faint applies decreased intensity to the text, making it appear |
| 53 |
// dimmer or lighter than normal text. Support varies across terminals. |
| 54 |
Faint |
| 55 |
|
| 56 |
// Italic applies italic styling to the text. This is not widely |
| 57 |
// supported across all terminal emulators. |
| 58 |
Italic |
| 59 |
|
| 60 |
// Underline adds a line beneath the text. This is commonly supported |
| 61 |
// across most terminal emulators. |
| 62 |
Underline |
| 63 |
|
| 64 |
// BlinkSlow makes the text blink slowly (less than 150 times per |
| 65 |
// minute). This is rarely supported in modern terminal emulators. |
| 66 |
BlinkSlow |
| 67 |
|
| 68 |
// BlinkRapid makes the text blink rapidly (150 times per minute or |
| 69 |
// more). This is rarely supported in modern terminal emulators. |
| 70 |
BlinkRapid |
| 71 |
|
| 72 |
// ReverseVideo swaps the foreground and background colors. This is |
| 73 |
// useful for highlighting text or creating inverted color schemes. |
| 74 |
ReverseVideo |
| 75 |
|
| 76 |
// Concealed hides the text, making it invisible. This is sometimes |
| 77 |
// used for passwords or hidden content. Not widely supported. |
| 78 |
Concealed |
| 79 |
|
| 80 |
// CrossedOut applies a line through the text (strikethrough). |
| 81 |
// This is useful for indicating deleted or deprecated content. |
| 82 |
CrossedOut |
| 83 |
) |
| 84 |
|
| 85 |
// Reset attributes for clearing specific text formatting. |
| 86 |
// These attributes undo the effects of their corresponding base attributes, |
| 87 |
// allowing selective removal of formatting without a full reset. |
| 88 |
// |
| 89 |
// Reset attributes start at SGR code 22 and increment through 29, |
| 90 |
// corresponding to the base attributes in the 0-9 range. |
| 91 |
const ( |
| 92 |
// ResetBold removes bold or faint formatting, returning text to |
| 93 |
// normal intensity. |
| 94 |
ResetBold Attribute = iota + 22 |
| 95 |
|
| 96 |
// ResetItalic removes italic formatting from the text. |
| 97 |
ResetItalic |
| 98 |
|
| 99 |
// ResetUnderline removes underline formatting from the text. |
| 100 |
ResetUnderline |
| 101 |
|
| 102 |
// ResetBlinking disables any blinking effects applied to the text. |
| 103 |
ResetBlinking |
| 104 |
|
| 105 |
// _ is a placeholder for SGR code 25, which is reserved. |
| 106 |
_ |
| 107 |
|
| 108 |
// ResetReversed restores normal color ordering, undoing the effect |
| 109 |
// of ReverseVideo. |
| 110 |
ResetReversed |
| 111 |
|
| 112 |
// ResetConcealed makes concealed text visible again. |
| 113 |
ResetConcealed |
| 114 |
|
| 115 |
// ResetCrossedOut removes the strikethrough formatting from the text. |
| 116 |
ResetCrossedOut |
| 117 |
) |
| 118 |
|
| 119 |
// Foreground colors for text styling. |
| 120 |
// These attributes set the text color using the standard 8-color palette |
| 121 |
// defined in the ANSI standard. Each color corresponds to a specific SGR |
| 122 |
// code in the 30-37 range. |
| 123 |
// |
| 124 |
// Foreground colors can be combined with base attributes and background |
| 125 |
// colors to create rich text styling. |
| 126 |
const ( |
| 127 |
// FgBlack sets the foreground color to black. |
| 128 |
FgBlack Attribute = iota + 30 |
| 129 |
|
| 130 |
// FgRed sets the foreground color to red. |
| 131 |
FgRed |
| 132 |
|
| 133 |
// FgGreen sets the foreground color to green. |
| 134 |
FgGreen |
| 135 |
|
| 136 |
// FgYellow sets the foreground color to yellow. |
| 137 |
FgYellow |
| 138 |
|
| 139 |
// FgBlue sets the foreground color to blue. |
| 140 |
FgBlue |
| 141 |
|
| 142 |
// FgMagenta sets the foreground color to magenta (purple). |
| 143 |
FgMagenta |
| 144 |
|
| 145 |
// FgCyan sets the foreground color to cyan. |
| 146 |
FgCyan |
| 147 |
|
| 148 |
// FgWhite sets the foreground color to white. |
| 149 |
FgWhite |
| 150 |
|
| 151 |
// foreground is an internal marker used to distinguish foreground |
| 152 |
// color attributes from other attribute types. It is used internally |
| 153 |
// for implementing 256-color and 24-bit RGB color support. |
| 154 |
foreground |
| 155 |
) |
| 156 |
|
| 157 |
// High-intensity foreground colors. |
| 158 |
// These attributes provide bright or bold variants of the standard |
| 159 |
// foreground colors, corresponding to SGR codes in the 90-97 range. |
| 160 |
// High-intensity colors offer better visibility and contrast in |
| 161 |
// terminal output. |
| 162 |
const ( |
| 163 |
// FgHiBlack sets the foreground color to bright black (gray). |
| 164 |
FgHiBlack Attribute = iota + 90 |
| 165 |
|
| 166 |
// FgHiRed sets the foreground color to bright red. |
| 167 |
FgHiRed |
| 168 |
|
| 169 |
// FgHiGreen sets the foreground color to bright green. |
| 170 |
FgHiGreen |
| 171 |
|
| 172 |
// FgHiYellow sets the foreground color to bright yellow. |
| 173 |
FgHiYellow |
| 174 |
|
| 175 |
// FgHiBlue sets the foreground color to bright blue. |
| 176 |
FgHiBlue |
| 177 |
|
| 178 |
// FgHiMagenta sets the foreground color to bright magenta. |
| 179 |
FgHiMagenta |
| 180 |
|
| 181 |
// FgHiCyan sets the foreground color to bright cyan. |
| 182 |
FgHiCyan |
| 183 |
|
| 184 |
// FgHiWhite sets the foreground color to bright white. |
| 185 |
FgHiWhite |
| 186 |
) |
| 187 |
|
| 188 |
// Background colors for text styling. |
| 189 |
// These attributes set the background color using the standard 8-color |
| 190 |
// palette defined in the ANSI standard. Each color corresponds to a |
| 191 |
// specific SGR code in the 40-47 range. |
| 192 |
// |
| 193 |
// Background colors can be combined with foreground colors and text |
| 194 |
// attributes to create visually distinct output. |
| 195 |
const ( |
| 196 |
// BgBlack sets the background color to black. |
| 197 |
BgBlack Attribute = iota + 40 |
| 198 |
|
| 199 |
// BgRed sets the background color to red. |
| 200 |
BgRed |
| 201 |
|
| 202 |
// BgGreen sets the background color to green. |
| 203 |
BgGreen |
| 204 |
|
| 205 |
// BgYellow sets the background color to yellow. |
| 206 |
BgYellow |
| 207 |
|
| 208 |
// BgBlue sets the background color to blue. |
| 209 |
BgBlue |
| 210 |
|
| 211 |
// BgMagenta sets the background color to magenta (purple). |
| 212 |
BgMagenta |
| 213 |
|
| 214 |
// BgCyan sets the background color to cyan. |
| 215 |
BgCyan |
| 216 |
|
| 217 |
// BgWhite sets the background color to white. |
| 218 |
BgWhite |
| 219 |
|
| 220 |
// background is an internal marker used to distinguish background |
| 221 |
// color attributes from other attribute types. It is used internally |
| 222 |
// for implementing 256-color and 24-bit RGB color support. |
| 223 |
background |
| 224 |
) |
| 225 |
|
| 226 |
// High-intensity background colors. |
| 227 |
// These attributes provide bright variants of the standard background |
| 228 |
// colors, corresponding to SGR codes in the 100-107 range. |
| 229 |
// High-intensity background colors can be used for emphasis or |
| 230 |
// highlighting specific text regions. |
| 231 |
const ( |
| 232 |
// BgHiBlack sets the background color to bright black (gray). |
| 233 |
BgHiBlack Attribute = iota + 100 |
| 234 |
|
| 235 |
// BgHiRed sets the background color to bright red. |
| 236 |
BgHiRed |
| 237 |
|
| 238 |
// BgHiGreen sets the background color to bright green. |
| 239 |
BgHiGreen |
| 240 |
|
| 241 |
// BgHiYellow sets the background color to bright yellow. |
| 242 |
BgHiYellow |
| 243 |
|
| 244 |
// BgHiBlue sets the background color to bright blue. |
| 245 |
BgHiBlue |
| 246 |
|
| 247 |
// BgHiMagenta sets the background color to bright magenta. |
| 248 |
BgHiMagenta |
| 249 |
|
| 250 |
// BgHiCyan sets the background color to bright cyan. |
| 251 |
BgHiCyan |
| 252 |
|
| 253 |
// BgHiWhite sets the background color to bright white. |
| 254 |
BgHiWhite |
| 255 |
) |