libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
ansistream.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <cstring>
12#include <promeki/core/string.h>
13#include <promeki/core/util.h>
14#include <promeki/core/color.h>
15
17
18class IODevice;
19
28 public:
32 enum TextStyle {
33 Bold = 1,
34 Dim = 2,
35 Italic = 3,
37 Blink = 5,
39 Hidden = 8
40 };
41
56 enum AnsiColor : uint8_t {
57 // -- System colors (0-15) --
58 Black = 0,
59 Maroon = 1,
60 DarkRed = 1,
61 Green = 2,
63 Olive = 3,
65 Navy = 4,
67 Purple = 5,
69 Teal = 6,
71 Silver = 7,
73 Grey = 8,
75 Red = 9,
76 Lime = 10,
77 Yellow = 11,
78 Blue = 12,
79 Fuchsia = 13,
80 Magenta = 13,
81 Aqua = 14,
82 Cyan = 14,
83 White = 15,
84
85 // -- Color cube (16-231) --
86 Grey0 = 16,
87 NavyBlue = 17,
88 DarkBlue_18 = 18,
89 Blue3_19 = 19,
90 Blue3_20 = 20,
91 Blue1 = 21,
92 DarkGreen_22 = 22,
93 DeepSkyBlue4_23 = 23,
94 DeepSkyBlue4_24 = 24,
95 DeepSkyBlue4_25 = 25,
96 DodgerBlue3 = 26,
97 DodgerBlue2 = 27,
98 Green4 = 28,
99 SpringGreen4 = 29,
100 Turquoise4 = 30,
101 DeepSkyBlue3_31 = 31,
102 DeepSkyBlue3_32 = 32,
103 DodgerBlue1 = 33,
104 Green3_34 = 34,
105 SpringGreen3_35 = 35,
106 DarkCyan_36 = 36,
107 LightSeaGreen = 37,
108 DeepSkyBlue2 = 38,
109 DeepSkyBlue1 = 39,
110 Green3_40 = 40,
111 SpringGreen3_41 = 41,
112 SpringGreen2_42 = 42,
113 Cyan3 = 43,
114 DarkTurquoise = 44,
115 Turquoise2 = 45,
116 Green1 = 46,
117 SpringGreen2_47 = 47,
118 SpringGreen1 = 48,
119 MediumSpringGreen = 49,
120 Cyan2 = 50,
121 Cyan1 = 51,
122 DarkRed_52 = 52,
123 DeepPink4_53 = 53,
124 Purple4_54 = 54,
125 Purple4_55 = 55,
126 Purple3 = 56,
127 BlueViolet = 57,
128 Orange4_58 = 58,
129 Grey37 = 59,
130 MediumPurple4 = 60,
131 SlateBlue3_61 = 61,
132 SlateBlue3_62 = 62,
133 RoyalBlue1 = 63,
134 Chartreuse4 = 64,
135 DarkSeaGreen4_65 = 65,
136 PaleTurquoise4 = 66,
137 SteelBlue = 67,
138 SteelBlue3 = 68,
139 CornflowerBlue = 69,
140 Chartreuse3_70 = 70,
141 DarkSeaGreen4_71 = 71,
142 CadetBlue_72 = 72,
143 CadetBlue_73 = 73,
144 SkyBlue3 = 74,
145 SteelBlue1_75 = 75,
146 Chartreuse3_76 = 76,
147 PaleGreen3_77 = 77,
148 SeaGreen3 = 78,
149 Aquamarine3 = 79,
150 MediumTurquoise = 80,
151 SteelBlue1_81 = 81,
152 Chartreuse2_82 = 82,
153 SeaGreen2 = 83,
154 SeaGreen1_84 = 84,
155 SeaGreen1_85 = 85,
156 Aquamarine1_86 = 86,
157 DarkSlateGray2 = 87,
158 DarkRed_88 = 88,
159 DeepPink4_89 = 89,
160 DarkMagenta_90 = 90,
161 DarkMagenta_91 = 91,
162 DarkViolet_92 = 92,
163 Purple_93 = 93,
164 Orange4_94 = 94,
165 LightPink4 = 95,
166 Plum4 = 96,
167 MediumPurple3_97 = 97,
168 MediumPurple3_98 = 98,
169 SlateBlue1 = 99,
170 Yellow4_100 = 100,
171 Wheat4 = 101,
172 Grey53 = 102,
173 LightSlateGrey = 103,
174 MediumPurple = 104,
175 LightSlateBlue = 105,
176 Yellow4_106 = 106,
177 DarkOliveGreen3_107 = 107,
178 DarkSeaGreen = 108,
179 LightSkyBlue3_109 = 109,
180 LightSkyBlue3_110 = 110,
181 SkyBlue2 = 111,
182 Chartreuse2_112 = 112,
183 DarkOliveGreen3_113 = 113,
184 PaleGreen3_114 = 114,
185 DarkSeaGreen3_115 = 115,
186 DarkSlateGray3 = 116,
187 SkyBlue1 = 117,
188 Chartreuse1 = 118,
189 LightGreen_119 = 119,
190 LightGreen_120 = 120,
191 PaleGreen1_121 = 121,
192 Aquamarine1_122 = 122,
193 DarkSlateGray1 = 123,
194 Red3_124 = 124,
195 DeepPink4_125 = 125,
196 MediumVioletRed = 126,
197 Magenta3_127 = 127,
198 DarkViolet_128 = 128,
199 Purple_129 = 129,
200 DarkOrange3_130 = 130,
201 IndianRed_131 = 131,
202 HotPink3_132 = 132,
203 MediumOrchid3 = 133,
204 MediumOrchid = 134,
205 MediumPurple2_135 = 135,
206 DarkGoldenrod = 136,
207 LightSalmon3_137 = 137,
208 RosyBrown = 138,
209 Grey63 = 139,
210 MediumPurple2_140 = 140,
211 MediumPurple1 = 141,
212 Gold3_142 = 142,
213 DarkKhaki = 143,
214 NavajoWhite3 = 144,
215 Grey69 = 145,
216 LightSteelBlue3 = 146,
217 LightSteelBlue = 147,
218 Yellow3_148 = 148,
219 DarkOliveGreen3_149 = 149,
220 DarkSeaGreen3_150 = 150,
221 DarkSeaGreen2_151 = 151,
222 LightCyan3 = 152,
223 LightSkyBlue1 = 153,
224 GreenYellow = 154,
225 DarkOliveGreen2 = 155,
226 PaleGreen1_156 = 156,
227 DarkSeaGreen2_157 = 157,
228 DarkSeaGreen1_158 = 158,
229 PaleTurquoise1 = 159,
230 Red3_160 = 160,
231 DeepPink3_161 = 161,
232 DeepPink3_162 = 162,
233 Magenta3_163 = 163,
234 Magenta3_164 = 164,
235 Magenta2_165 = 165,
236 DarkOrange3_166 = 166,
237 IndianRed_167 = 167,
238 HotPink3_168 = 168,
239 HotPink2 = 169,
240 Orchid = 170,
241 MediumOrchid1_171 = 171,
242 Orange3 = 172,
243 LightSalmon3_173 = 173,
244 LightPink3 = 174,
245 Pink3 = 175,
246 Plum3 = 176,
247 Violet = 177,
248 Gold3_178 = 178,
249 LightGoldenrod3 = 179,
250 Tan = 180,
251 MistyRose3 = 181,
252 Thistle3 = 182,
253 Plum2 = 183,
254 Yellow3_184 = 184,
255 Khaki3 = 185,
256 LightGoldenrod2_186 = 186,
257 LightYellow3 = 187,
258 Grey84 = 188,
259 LightSteelBlue1 = 189,
260 Yellow2 = 190,
261 DarkOliveGreen1_191 = 191,
262 DarkOliveGreen1_192 = 192,
263 DarkSeaGreen1_193 = 193,
264 Honeydew2 = 194,
265 LightCyan1 = 195,
266 Red1 = 196,
267 DeepPink2 = 197,
268 DeepPink1_198 = 198,
269 DeepPink1_199 = 199,
270 Magenta2_200 = 200,
271 Magenta1 = 201,
272 OrangeRed1 = 202,
273 IndianRed1_203 = 203,
274 IndianRed1_204 = 204,
275 HotPink_205 = 205,
276 HotPink_206 = 206,
277 MediumOrchid1_207 = 207,
278 DarkOrange = 208,
279 Salmon1 = 209,
280 LightCoral = 210,
281 PaleVioletRed1 = 211,
282 Orchid2 = 212,
283 Orchid1 = 213,
284 Orange1 = 214,
285 SandyBrown = 215,
286 LightSalmon1 = 216,
287 LightPink1 = 217,
288 Pink1 = 218,
289 Plum1 = 219,
290 Gold1 = 220,
291 LightGoldenrod2_221 = 221,
292 LightGoldenrod2_222 = 222,
293 NavajoWhite1 = 223,
294 MistyRose1 = 224,
295 Thistle1 = 225,
296 Yellow1 = 226,
297 LightGoldenrod1 = 227,
298 Khaki1 = 228,
299 Wheat1 = 229,
300 Cornsilk1 = 230,
301 Grey100 = 231,
302
303 // -- Grayscale ramp (232-255) --
304 Grey3 = 232,
305 Grey7 = 233,
306 Grey11 = 234,
307 Grey15 = 235,
308 Grey19 = 236,
309 Grey23 = 237,
310 Grey27 = 238,
311 Grey30 = 239,
312 Grey35 = 240,
313 Grey39 = 241,
314 Grey42 = 242,
315 Grey46 = 243,
316 Grey50 = 244,
317 Grey54 = 245,
318 Grey58 = 246,
319 Grey62 = 247,
320 Grey66 = 248,
321 Grey70 = 249,
322 Grey74 = 250,
323 Grey78 = 251,
324 Grey82 = 252,
325 Grey85 = 253,
326 Grey89 = 254,
327 Grey93 = 255
328 };
329
335 static Color ansiColor(int index);
336
342 static Color ansiColor(AnsiColor color) {
343 return ansiColor(static_cast<int>(color));
344 }
345
358 static AnsiColor findClosestAnsiColor(const Color &color, int maxIndex = 255);
359
366 static bool stdoutWindowSize(int &rows, int &cols);
367
372 static bool stdoutSupportsANSI();
373
378 AnsiStream(IODevice *device) : _device(device), _enabled(true) { }
379
386 void setAnsiEnabled(bool val) {
387 _enabled = val;
388 return;
389 }
390
395 IODevice *device() const { return _device; }
396
402 AnsiStream &write(const String &text);
403
409 AnsiStream &write(const char *text);
410
416 AnsiStream &write(char ch);
417
423 AnsiStream &write(int val);
424
428 void flush();
429
431 AnsiStream &operator<<(const String &text) { return write(text); }
433 AnsiStream &operator<<(const char *text) { return write(text); }
435 AnsiStream &operator<<(char ch) { return write(ch); }
437 AnsiStream &operator<<(int val) { return write(val); }
438
450
462
469 AnsiStream &setForeground(const Color &color, int maxIndex = 255);
470
477 AnsiStream &setBackground(const Color &color, int maxIndex = 255);
478
485 if(!_enabled) return *this;
486 *this << "\033[" << n << "A";
487 return *this;
488 }
489
496 if(!_enabled) return *this;
497 *this << "\033[" << n << "B";
498 return *this;
499 }
500
507 if(!_enabled) return *this;
508 *this << "\033[" << n << "C";
509 return *this;
510 }
511
518 if(!_enabled) return *this;
519 *this << "\033[" << n << "D";
520 return *this;
521 }
522
530 if(!_enabled) return *this;
531 *this << "\033[" << r << ";" << c << "H";
532 return *this;
533 }
534
540 if(!_enabled) return *this;
541 *this << "\033[2J";
542 return *this;
543 }
544
550 if(!_enabled) return *this;
551 *this << "\033[0G";
552 return *this;
553 }
554
560 if(!_enabled) return *this;
561 *this << "\033[999G";
562 return *this;
563 }
564
570 if(!_enabled) return *this;
571 *this << "\033[2K";
572 return *this;
573 }
574
580 if(!_enabled) return *this;
581 *this << "\033[1K";
582 return *this;
583 }
584
590 if(!_enabled) return *this;
591 *this << "\033[0K";
592 return *this;
593 }
594
600 if(!_enabled) return *this;
601 *this << "\033[0m";
602 return *this;
603 }
604
610 if(!_enabled) return *this;
611 *this << "\033[39m";
612 return *this;
613 }
614
620 if(!_enabled) return *this;
621 *this << "\033[49m";
622 return *this;
623 }
624
630 if(!_enabled) return *this;
631 *this << "\033[?25h";
632 return *this;
633 }
634
640 if(!_enabled) return *this;
641 *this << "\033[?25l";
642 return *this;
643 }
644
650 if(!_enabled) return *this;
651 *this << "\033[s";
652 return *this;
653 }
654
661 if(!_enabled) return *this;
662 *this << "\033[u";
663 return *this;
664 }
665
672 AnsiStream &enableScrollingRegion(int startRow, int endRow) {
673 if(!_enabled) return *this;
674 *this << "\033[" << startRow << ";" << endRow << "r";
675 return *this;
676 }
677
684 if(!_enabled) return *this;
685 *this << "\033[" << n << "S";
686 return *this;
687 }
688
695 if(!_enabled) return *this;
696 *this << "\033[" << n << "T";
697 return *this;
698 }
699
706 if(!_enabled) return *this;
707 *this << "\033[" << n << "X";
708 return *this;
709 }
710
721 AnsiStream &setForeground256(uint8_t index) {
722 if(!_enabled) return *this;
723 *this << "\033[38;5;" << static_cast<int>(index) << "m";
724 return *this;
725 }
726
737 AnsiStream &setBackground256(uint8_t index) {
738 if(!_enabled) return *this;
739 *this << "\033[48;5;" << static_cast<int>(index) << "m";
740 return *this;
741 }
742
750 AnsiStream &setForegroundRGB(uint8_t r, uint8_t g, uint8_t b) {
751 if(!_enabled) return *this;
752 *this << "\033[38;2;" << static_cast<int>(r) << ";"
753 << static_cast<int>(g) << ";" << static_cast<int>(b) << "m";
754 return *this;
755 }
756
764 AnsiStream &setBackgroundRGB(uint8_t r, uint8_t g, uint8_t b) {
765 if(!_enabled) return *this;
766 *this << "\033[48;2;" << static_cast<int>(r) << ";"
767 << static_cast<int>(g) << ";" << static_cast<int>(b) << "m";
768 return *this;
769 }
770
777 if(!_enabled) return *this;
778 *this << "\033[" << (enable ? "9" : "29") << "m";
779 return *this;
780 }
781
787 if(!_enabled) return *this;
788 *this << "\033[?1049h";
789 return *this;
790 }
791
797 if(!_enabled) return *this;
798 *this << "\033[?1049l";
799 return *this;
800 }
801
809 bool getCursorPosition(IODevice *input, int &row, int &col);
810
811 private:
812 IODevice *_device;
813 bool _enabled;
814};
815
ANSI escape code writer backed by an IODevice. Writes ANSI escape sequences and raw text to an IODevi...
Definition ansistream.h:27
static AnsiColor findClosestAnsiColor(const Color &color, int maxIndex=255)
Finds the closest ANSI palette entry for an RGB color.
AnsiStream & operator<<(const String &text)
Writes a String via operator<<.
Definition ansistream.h:431
static Color ansiColor(AnsiColor color)
Returns the RGB color for an AnsiColor palette entry.
Definition ansistream.h:342
AnsiStream & cursorLeft(int n)
Moves the cursor left N columns.
Definition ansistream.h:517
static bool stdoutWindowSize(int &rows, int &cols)
Returns the window size of the current STDOUT device.
AnsiStream & resetForeground()
Resets the foreground to the terminal default.
Definition ansistream.h:609
AnsiStream & cursorUp(int n)
Moves the cursor up N rows.
Definition ansistream.h:484
AnsiStream & write(const char *text)
Writes a C string to the underlying device.
AnsiStream & write(char ch)
Writes a single character to the underlying device.
AnsiStream & setBackground256(uint8_t index)
Sets the background to a 256-color palette index.
Definition ansistream.h:737
AnsiStream & setBackground(AnsiColor color)
Sets the background to an ANSI palette color.
AnsiStream & setForeground(const Color &color, int maxIndex=255)
Sets the foreground to the closest ANSI palette match.
AnsiStream & setBackground(const Color &color, int maxIndex=255)
Sets the background to the closest ANSI palette match.
AnsiStream & setForegroundRGB(uint8_t r, uint8_t g, uint8_t b)
Sets the foreground to a 24-bit RGB color.
Definition ansistream.h:750
AnsiColor
ANSI 256-color palette indices.
Definition ansistream.h:56
@ DarkCyan
Alias for Teal.
Definition ansistream.h:70
@ DarkBlue
Alias for Navy.
Definition ansistream.h:66
@ LightGray
Alias for Silver.
Definition ansistream.h:72
@ DarkGray
Alias for Grey.
Definition ansistream.h:74
@ DarkRed
Alias for Maroon.
Definition ansistream.h:60
@ DarkYellow
Alias for Olive.
Definition ansistream.h:64
@ Cyan
Alias for Aqua.
Definition ansistream.h:82
@ DarkGreen
Alias for Green (system).
Definition ansistream.h:62
@ Magenta
Alias for Fuchsia.
Definition ansistream.h:80
@ DarkMagenta
Alias for Purple (system).
Definition ansistream.h:68
AnsiStream & write(const String &text)
Writes raw text to the underlying device.
AnsiStream & scrollUp(int n)
Causes the scrolling region to scroll up N rows.
Definition ansistream.h:683
AnsiStream & clearScreen()
Clears the screen.
Definition ansistream.h:539
AnsiStream & setCursorPosition(int r, int c)
Sets the absolute cursor position.
Definition ansistream.h:529
AnsiStream & moveToEndOfLine()
Moves the cursor to the end of the current line.
Definition ansistream.h:559
AnsiStream & restoreCursorPosition()
Recalls a saved cursor position.
Definition ansistream.h:660
bool getCursorPosition(IODevice *input, int &row, int &col)
Requests the current cursor position from the terminal.
void setAnsiEnabled(bool val)
Sets the ANSI output enabled. If not enabled, no ANSI codes will be output but non-ANSI content will ...
Definition ansistream.h:386
AnsiStream & moveToStartOfLine()
Moves the cursor to the start of the current line.
Definition ansistream.h:549
AnsiStream & enableScrollingRegion(int startRow, int endRow)
Enables a region of the screen to scroll.
Definition ansistream.h:672
TextStyle
ANSI text styles.
Definition ansistream.h:32
@ Inverted
Swapped foreground and background colors.
Definition ansistream.h:38
@ Dim
Faint or decreased intensity.
Definition ansistream.h:34
@ Italic
Italic text.
Definition ansistream.h:35
@ Bold
Bold or increased intensity.
Definition ansistream.h:33
@ Underlined
Underlined text.
Definition ansistream.h:36
@ Hidden
Hidden (invisible) text.
Definition ansistream.h:39
@ Blink
Blinking text.
Definition ansistream.h:37
IODevice * device() const
Returns the underlying IODevice.
Definition ansistream.h:395
AnsiStream & eraseCharacters(int n)
Erases N characters at cursor.
Definition ansistream.h:705
AnsiStream & clearLine()
Clears the current line.
Definition ansistream.h:569
AnsiStream & cursorDown(int n)
Moves the cursor down N rows.
Definition ansistream.h:495
AnsiStream & clearLineAfterCursor()
Clears between the cursor and the end of the current line.
Definition ansistream.h:589
AnsiStream & useMainScreenBuffer()
Terminal should switch to main screen buffer.
Definition ansistream.h:796
AnsiStream & saveCursorPosition()
Saves the cursor position for later recall.
Definition ansistream.h:649
AnsiStream & cursorRight(int n)
Moves the cursor right N columns.
Definition ansistream.h:506
AnsiStream & scrollDown(int n)
Causes the scrolling region to scroll down N rows.
Definition ansistream.h:694
AnsiStream & setForeground(AnsiColor color)
Sets the foreground to an ANSI palette color.
AnsiStream & setStrikethrough(bool enable)
Enables strike-through mode if supported.
Definition ansistream.h:776
void flush()
Flushes the underlying device.
AnsiStream & useAlternateScreenBuffer()
Terminal should switch to an alternate buffer.
Definition ansistream.h:786
AnsiStream & hideCursor()
Makes the cursor invisible.
Definition ansistream.h:639
AnsiStream & operator<<(int val)
Writes an integer via operator<<.
Definition ansistream.h:437
AnsiStream & write(int val)
Writes an integer to the underlying device.
AnsiStream & reset()
Resets the terminal to default configuration.
Definition ansistream.h:599
AnsiStream & resetBackground()
Resets the background to the terminal default.
Definition ansistream.h:619
AnsiStream & setBackgroundRGB(uint8_t r, uint8_t g, uint8_t b)
Sets the background to a 24-bit RGB color.
Definition ansistream.h:764
static bool stdoutSupportsANSI()
Returns true if the current STDOUT can support ANSI signaling.
AnsiStream & operator<<(const char *text)
Writes a C string via operator<<.
Definition ansistream.h:433
static Color ansiColor(int index)
Returns the RGB color for a 256-color palette entry.
AnsiStream(IODevice *device)
Constructs an AnsiStream writing to the given device.
Definition ansistream.h:378
AnsiStream & setForeground256(uint8_t index)
Sets the foreground to a 256-color palette index.
Definition ansistream.h:721
AnsiStream & operator<<(char ch)
Writes a single character via operator<<.
Definition ansistream.h:435
AnsiStream & clearLineBeforeCursor()
Clears between the cursor and the start of the current line.
Definition ansistream.h:579
AnsiStream & showCursor()
Makes the cursor visible.
Definition ansistream.h:629
General-purpose RGBA color.
Definition color.h:24
Abstract base class for all I/O devices.
Definition iodevice.h:29
Encoding-aware string class with copy-on-write semantics.
Definition string.h:35
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19