ANSI-Project 1.0
Make quick ANSI formats to beautify terminal output
ansi.h
Go to the documentation of this file.
1/*
2 * ANSI-Project: C
3 *
4 * Make quick ANSI formats to beautify terminal output
5 *
6 * @see https://github.com/bruneo32/ANSI-Project
7 * @see https://en.wikipedia.org/wiki/ANSI_escape_code
8 *
9 * @author Bruno Castro
10 * @version 1.0
11 * @since January 2022
12 */
13
14
15#ifndef _ANSI_H
16#define _ANSI_H
17
18#ifndef _STDIO_H
19 #include <stdio.h>
20#endif
21
22#ifndef ANSI_ESC
23 #define ANSI_ESC "\x1B"
24#endif
25#ifndef ANSI_CSI
26 #define ANSI_CSI "\x9B"
27#endif
28#ifndef ANSI_DSC
29 #define ANSI_DSC "\x90"
30#endif
31#ifndef ANSI_OSC
32 #define ANSI_OSC "\x9D"
33#endif
34
35
36/*
37 * Colors [quick]
38 */
39
40// BASIC
41#define ANSI_RESET ANSI_ESC "[0m"
42
43#define ANSI_BOLD ANSI_ESC "[1m"
44#define ANSI_FAINT ANSI_ESC "[2m"
45#define ANSI_ITALIC ANSI_ESC "[3m"
46#define ANSI_UNDER ANSI_ESC "[4m"
47#define ANSI_SBLINK ANSI_ESC "[5m"
48#define ANSI_RBLINK ANSI_ESC "[6m"
49#define ANSI_REVERSE ANSI_ESC "[7m"
50#define ANSI_HIDE ANSI_ESC "[8m"
51#define ANSI_STRIKE ANSI_ESC "[9m"
52
56#define ANSI_DEF_FONT ANSI_ESC "[10m"
57
61#define ANSI_Franktur ANSI_ESC "[20m"
62
63#define ANSI_BOLD_OFF ANSI_ESC "[21m"
64#define ANSI_HI_OFF ANSI_ESC "[22m"
65#define ANSI_ITALIC_OFF ANSI_ESC "[23m"
66#define ANSI_UNDER_OFF ANSI_ESC "[24m"
67#define ANSI_BLINK_OFF ANSI_ESC "[25m"
68#define ANSI_REVERSE_OFF ANSI_ESC "[27m"
69#define ANSI_REVEAL ANSI_ESC "[28m"
70#define ANSI_STRIKE_OFF ANSI_ESC "[29m"
71
72// FOREGROUND
73#define ANSI_BLACK ANSI_ESC "[30m"
74#define ANSI_RED ANSI_ESC "[31m"
75#define ANSI_GREEN ANSI_ESC "[32m"
76#define ANSI_YELLOW ANSI_ESC "[33m"
77#define ANSI_BLUE ANSI_ESC "[34m"
78#define ANSI_PURPLE ANSI_ESC "[35m"
79#define ANSI_CYAN ANSI_ESC "[36m"
80#define ANSI_WHITE ANSI_ESC "[37m"
81
82#define ANSI_DEFAULT ANSI_ESC "[39m"
83
84// BACKGROUND
85#define ANSI_BK_BLACK ANSI_ESC "[40m"
86#define ANSI_BK_RED ANSI_ESC "[41m"
87#define ANSI_BK_GREEN ANSI_ESC "[42m"
88#define ANSI_BK_YELLOW ANSI_ESC "[43m"
89#define ANSI_BK_BLUE ANSI_ESC "[44m"
90#define ANSI_BK_PURPLE ANSI_ESC "[45m"
91#define ANSI_BK_CYAN ANSI_ESC "[46m"
92#define ANSI_BK_WHITE ANSI_ESC "[47m"
93
94#define ANSI_BK_DEFAULT ANSI_ESC "[49m"
95
96// HIGH INTENSITY
97#define ANSI_BLACK_HI ANSI_ESC "[90m"
98#define ANSI_RED_HI ANSI_ESC "[91m"
99#define ANSI_GREEN_HI ANSI_ESC "[92m"
100#define ANSI_YELLOW_HI ANSI_ESC "[93m"
101#define ANSI_BLUE_HI ANSI_ESC "[94m"
102#define ANSI_PURPLE_HI ANSI_ESC "[95m"
103#define ANSI_CYAN_HI ANSI_ESC "[96m"
104#define ANSI_WHITE_HI ANSI_ESC "[97m"
105
106
107// HI BACKGROUND
108#define ANSI_BK_BLACK_HI ANSI_ESC "[100m"
109#define ANSI_BK_RED_HI ANSI_ESC "[101m"
110#define ANSI_BK_GREEN_HI ANSI_ESC "[102m"
111#define ANSI_BK_YELLOW_HI ANSI_ESC "[103m"
112#define ANSI_BK_BLUE_HI ANSI_ESC "[104m"
113#define ANSI_BK_PURPLE_HI ANSI_ESC "[105m"
114#define ANSI_BK_CYAN_HI ANSI_ESC "[106m"
115#define ANSI_BK_WHITE_HI ANSI_ESC "[107m"
116
117
118/*
119 * Colors [bake]
120 */
121
128#define ANSI_SGR(str) ANSI_ESC "[" #str "m"
129
138#define ANSI_RGB(r, g, b) ANSI_ESC "[38;2;" #r ";" #g ";" #b "m"
139
148#define ANSI_BK_RGB(r, g, b) ANSI_ESC "[48;2;" #r ";" #g ";" #b "m"
149
150
151
152/*
153 * CONTROLS [quick]
154 */
155
162#define ANSI_C0 ANSI_ESC "[1;1H"
163
169#define ANSI_CUU1 ANSI_ESC "[1A"
170
176#define ANSI_CUD1 ANSI_ESC "[1B"
177
183#define ANSI_CUF1 ANSI_ESC "[1C"
184
190#define ANSI_CUB1 ANSI_ESC "[1D"
191
197#define ANSI_CNL1 ANSI_ESC "[1E"
198
204#define ANSI_CPL1 ANSI_ESC "[1F"
205
211#define ANSI_SU1 ANSI_ESC "[1S"
212
218#define ANSI_SD1 ANSI_ESC "[1T"
219
225#define ANSI_ED0 ANSI_ESC "[0J"
226
232#define ANSI_ED1 ANSI_ESC "[1J"
233
239#define ANSI_ED2 ANSI_ESC "[2J"
240
246#define ANSI_ED3 ANSI_ESC "[3J"
247
248
254#define ANSI_EL0 ANSI_ESC "[0K"
255
261#define ANSI_EL1 ANSI_ESC "[1K"
262
268#define ANSI_EL2 ANSI_ESC "[2K"
269
270
276#define ANSI_AUX_PORT_ON ANSI_ESC "[5i"
277
283#define ANSI_AUX_PORT_OFF ANSI_ESC "[4i"
284
290#define ANSI_DSR ANSI_ESC "[6n"
291
298#define ANSI_SCP ANSI_ESC "[s"
299
305#define ANSI_RCP ANSI_ESC "[u"
306
307
308/*
309 * CONTROLS [bake]
310 */
311
318#define ANSI_CUU(n) ANSI_ESC "[" #n "A"
319
326#define ANSI_CUD(n) ANSI_ESC "[" #n "B"
327
334#define ANSI_CUF(n) ANSI_ESC "[" #n "C"
335
342#define ANSI_CUB(n) ANSI_ESC "[" #n "D"
343
344
351#define ANSI_CNL(n) ANSI_ESC "[" #n "E"
352
359#define ANSI_CPL(n) ANSI_ESC "[" #n "F"
360
367#define ANSI_CHA(n) ANSI_ESC "[" #n "G"
368
376#define ANSI_CUP(row, col) ANSI_ESC "[" #row ";" #col "H"
377
378
389#define ANSI_ED(n) ANSI_ESC "[" #n "J"
390
400#define ANSI_EL(n) ANSI_ESC "[" #n "K"
401
408#define ANSI_SU(n) ANSI_ESC "[" #n "S"
409
416#define ANSI_SD(n) ANSI_ESC "[" #n "T"
417
426#define ANSI_HVP(row, col) ANSI_ESC "[" #row ";" #col "f"
427
428
429/*
430 * CUSTOM
431 */
432
439#define ANSI_custom(str) ANSI_ESC "[" #str
440
441
442
443#ifdef __cplusplus
444 /*
445 * Code for C++ only
446 */
447
451 namespace ansi {
452
453 std::string ESC = std::string(ANSI_ESC);
454 std::string CSI = std::string(ANSI_CSI);
455 std::string DSC = std::string(ANSI_DSC);
456 std::string OSC = std::string(ANSI_OSC);
457
458 /*
459 * Colors [bake]
460 */
461
469 std::string SGR(std::string str) {
470 return ansi::ESC+"["+str+"m";
471 }
472
481 std::string RGB(int r, int g, int b) {
482 return ansi::ESC+"[38;2;"+
483 std::to_string(r)+";"+
484 std::to_string(g)+";"+
485 std::to_string(b)+"m";
486 }
487
496 std::string BK_RGB(int r, int g, int b) {
497 return ansi::ESC+"[48;2;"+
498 std::to_string(r)+";"+
499 std::to_string(g)+";"+
500 std::to_string(b)+"m";
501 }
502
503
504 /*
505 * CONTROLS [bake]
506 */
507
514 std::string CUU(int n) {
515 return ansi::ESC+"["+ std::to_string(n) +"A";
516 }
517
524 std::string CUD(int n) {
525 return ansi::ESC+"["+ std::to_string(n) +"B";
526 }
527
534 std::string CUF(int n) {
535 return ansi::ESC+"["+ std::to_string(n) +"C";
536 }
537
544 std::string CUB(int n) {
545 return ansi::ESC+"["+ std::to_string(n) +"D";
546 }
547
548
555 std::string CNL(int n) {
556 return ansi::ESC+"["+ std::to_string(n) +"E";
557 }
558
565 std::string CPL(int n) {
566 return ansi::ESC+"["+ std::to_string(n) +"F";
567 }
568
575 std::string CHA(int n) {
576 return ansi::ESC+"["+ std::to_string(n) +"G";
577 }
578
586 std::string CUP(int row, int col) {
587 return ansi::ESC+"["+ std::to_string(row)+";"+std::to_string(col) +"H";
588 }
589
590
601 std::string ED(int n) {
602 return ansi::ESC+"["+ std::to_string(n) +"J";
603 }
604
614 std::string EL(int n) {
615 return ansi::ESC+"["+ std::to_string(n) +"K";
616 }
617
624 std::string SU(int n) {
625 return ansi::ESC+"["+ std::to_string(n) +"S";
626 }
627
634 std::string SD(int n) {
635 return ansi::ESC+"["+ std::to_string(n) +"T";
636 }
637
646 std::string HVP(int row, int col) {
647 return ansi::ESC+"["+ std::to_string(row)+";"+std::to_string(col) +"f";
648 }
649
650
651 /*
652 * CUSTOM
653 */
654
661 std::string custom(std::string str) {
662 return ansi::ESC+"["+str;
663 }
664
665 }
666
667#endif // __cplusplus
668
669#endif // _ANSI_H
#define ANSI_OSC
Definition: ansi.h:32
#define ANSI_DSC
Definition: ansi.h:29
#define ANSI_ESC
Definition: ansi.h:23
#define ANSI_CSI
Definition: ansi.h:26