AnsiColor.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef ANSI_COLOR_H_
34 #define ANSI_COLOR_H_
35 
36 #define _ANSI_TERM_GET_OVERRIDE(_1, _2, _3, _4, _5, NAME, ...) NAME
37 
38 #define _ANSI_TERM_CONCAT_COLOR5(a, b, c, d, e) \
39  "\x1b[" _ANSI_TERM_ ## a ";" _ANSI_TERM_ ## b ";" _ANSI_TERM_ ## c \
40  ";" _ANSI_TERM_ ## d ";" _ANSI_TERM_ ## e "m"
41 #define _ANSI_TERM_CONCAT_COLOR4(a, b, c, d) \
42  "\x1b[" _ANSI_TERM_ ## a ";" _ANSI_TERM_ ## b ";" _ANSI_TERM_ ## c \
43  ";" _ANSI_TERM_ ## d "m"
44 #define _ANSI_TERM_CONCAT_COLOR3(a, b, c) \
45  "\x1b[" _ANSI_TERM_ ## a ";" _ANSI_TERM_ ## b ";" _ANSI_TERM_ ## c "m"
46 #define _ANSI_TERM_CONCAT_COLOR2(a, b) \
47  "\x1b[" _ANSI_TERM_ ## a ";" _ANSI_TERM_ ## b "m"
48 #define _ANSI_TERM_CONCAT_COLOR1(a) \
49  "\x1b[" _ANSI_TERM_ ## a "m"
50 
51 #define _ANSI_TERM_FG_BLACK "30"
52 #define _ANSI_TERM_FG_RED "31"
53 #define _ANSI_TERM_FG_GREEN "32"
54 #define _ANSI_TERM_FG_YELLOW "33"
55 #define _ANSI_TERM_FG_BLUE "34"
56 #define _ANSI_TERM_FG_MAGENTA "35"
57 #define _ANSI_TERM_FG_CYAN "36"
58 #define _ANSI_TERM_FG_WHITE "37"
59 
60 #define _ANSI_TERM_BG_BLACK "40"
61 #define _ANSI_TERM_BG_RED "41"
62 #define _ANSI_TERM_BG_GREEN "42"
63 #define _ANSI_TERM_BG_YELLOW "43"
64 #define _ANSI_TERM_BG_BLUE "44"
65 #define _ANSI_TERM_BG_MAGENTA "45"
66 #define _ANSI_TERM_BG_CYAN "46"
67 #define _ANSI_TERM_BG_WHITE "47"
68 
69 #define _ANSI_TERM_ATTR_RESET "0"
70 #define _ANSI_TERM_ATTR_BOLD "1"
71 #define _ANSI_TERM_ATTR_UNDERLINE "4"
72 #define _ANSI_TERM_ATTR_BLINK "5"
73 
74 /* @brief Returns an ANSI escape sequence for the chosen colors and style
75  *
76  * The parameters are variadic, and up to five can be specified in any order:
77  *
78  * FG_BLACK, FG_RED, FG_GREEN, FG_YELLOW, FG_BLUE, FG_MAGENTA, FG_CYAN, FG_WHITE,
79  * BG_BLACK, BG_RED, BG_GREEN, BG_YELLOW, BG_BLUE, BG_MAGENTA, BG_CYAN, BG_WHITE,
80  * ATTR_RESET, ATTR_BOLD, ATTR_UNDERLINE, ATTR_BLINK
81  *
82  * Example use:
83  * char str[] = ANSI_COLOR(FG_WHITE, ATTR_BOLD) "Hello" ANSI_COLOR(ATTR_RESET)
84  * --> "Esc[37;1mHelloEsc[0m" where Esc is the Escape control character 0x1B.
85  *
86  * @param 1 - color or style
87  * @param 2 - color or style
88  * @param 3 - color or style
89  * @param 4 - color or style
90  * @param 5 - color or style
91  *
92  */
93 #define ANSI_COLOR(...) \
94  _ANSI_TERM_GET_OVERRIDE(__VA_ARGS__, \
95  _ANSI_TERM_CONCAT_COLOR5, _ANSI_TERM_CONCAT_COLOR4, _ANSI_TERM_CONCAT_COLOR3, \
96  _ANSI_TERM_CONCAT_COLOR2, _ANSI_TERM_CONCAT_COLOR1) (__VA_ARGS__)
97 
98 
99 #endif /* ANSI_COLOR_H_ */
© Copyright 1995-2019, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale