]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Utility/AnsiTerminal.h
Merge ^/head r286858 through r287489.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / include / lldb / Utility / AnsiTerminal.h
1 //===---------------------AnsiTerminal.h ------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10
11
12 #define ANSI_FG_COLOR_BLACK         30
13 #define ANSI_FG_COLOR_RED           31  
14 #define ANSI_FG_COLOR_GREEN         32  
15 #define ANSI_FG_COLOR_YELLOW        33  
16 #define ANSI_FG_COLOR_BLUE          34  
17 #define ANSI_FG_COLOR_PURPLE        35  
18 #define ANSI_FG_COLOR_CYAN          36  
19 #define ANSI_FG_COLOR_WHITE         37  
20
21 #define ANSI_BG_COLOR_BLACK         40
22 #define ANSI_BG_COLOR_RED           41  
23 #define ANSI_BG_COLOR_GREEN         42  
24 #define ANSI_BG_COLOR_YELLOW        43  
25 #define ANSI_BG_COLOR_BLUE          44  
26 #define ANSI_BG_COLOR_PURPLE        45  
27 #define ANSI_BG_COLOR_CYAN          46  
28 #define ANSI_BG_COLOR_WHITE         47  
29
30 #define ANSI_SPECIAL_FRAMED         51
31 #define ANSI_SPECIAL_ENCIRCLED      52
32
33 #define ANSI_CTRL_NORMAL            0
34 #define ANSI_CTRL_BOLD              1
35 #define ANSI_CTRL_FAINT             2
36 #define ANSI_CTRL_ITALIC            3
37 #define ANSI_CTRL_UNDERLINE         4
38 #define ANSI_CTRL_SLOW_BLINK        5
39 #define ANSI_CTRL_FAST_BLINK        6
40 #define ANSI_CTRL_IMAGE_NEGATIVE    7
41 #define ANSI_CTRL_CONCEAL           8
42 #define ANSI_CTRL_CROSSED_OUT       9
43
44 #define ANSI_ESC_START          "\033["
45 #define ANSI_ESC_END            "m"
46
47 #define ANSI_1_CTRL(ctrl1)          "\033["##ctrl1 ANSI_ESC_END
48 #define ANSI_2_CTRL(ctrl1,ctrl2)    "\033["##ctrl1";"##ctrl2 ANSI_ESC_END
49
50 namespace lldb_utility {
51
52     namespace ansi {
53
54         inline std::string
55         FormatAnsiTerminalCodes(const char *format, bool do_color = true)
56         {
57             // Convert "${ansi.XXX}" tokens to ansi values or clear them if do_color is false.
58             static const struct
59             {
60                 const char *name;
61                 const char *value;
62             } g_color_tokens[] =
63             {
64         #define _TO_STR2(_val) #_val
65         #define _TO_STR(_val) _TO_STR2(_val)
66                 { "fg.black}",        ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_BLACK)      ANSI_ESC_END },
67                 { "fg.red}",          ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_RED)        ANSI_ESC_END },
68                 { "fg.green}",        ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_GREEN)      ANSI_ESC_END },
69                 { "fg.yellow}",       ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_YELLOW)     ANSI_ESC_END },
70                 { "fg.blue}",         ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_BLUE)       ANSI_ESC_END },
71                 { "fg.purple}",       ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_PURPLE)     ANSI_ESC_END },
72                 { "fg.cyan}",         ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_CYAN)       ANSI_ESC_END },
73                 { "fg.white}",        ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_WHITE)      ANSI_ESC_END },
74                 { "bg.black}",        ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_BLACK)      ANSI_ESC_END },
75                 { "bg.red}",          ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_RED)        ANSI_ESC_END },
76                 { "bg.green}",        ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_GREEN)      ANSI_ESC_END },
77                 { "bg.yellow}",       ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_YELLOW)     ANSI_ESC_END },
78                 { "bg.blue}",         ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_BLUE)       ANSI_ESC_END },
79                 { "bg.purple}",       ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_PURPLE)     ANSI_ESC_END },
80                 { "bg.cyan}",         ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_CYAN)       ANSI_ESC_END },
81                 { "bg.white}",        ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_WHITE)      ANSI_ESC_END },
82                 { "normal}",          ANSI_ESC_START _TO_STR(ANSI_CTRL_NORMAL)         ANSI_ESC_END },
83                 { "bold}",            ANSI_ESC_START _TO_STR(ANSI_CTRL_BOLD)           ANSI_ESC_END },
84                 { "faint}",           ANSI_ESC_START _TO_STR(ANSI_CTRL_FAINT)          ANSI_ESC_END },
85                 { "italic}",          ANSI_ESC_START _TO_STR(ANSI_CTRL_ITALIC)         ANSI_ESC_END },
86                 { "underline}",       ANSI_ESC_START _TO_STR(ANSI_CTRL_UNDERLINE)      ANSI_ESC_END },
87                 { "slow-blink}",      ANSI_ESC_START _TO_STR(ANSI_CTRL_SLOW_BLINK)     ANSI_ESC_END },
88                 { "fast-blink}",      ANSI_ESC_START _TO_STR(ANSI_CTRL_FAST_BLINK)     ANSI_ESC_END },
89                 { "negative}",        ANSI_ESC_START _TO_STR(ANSI_CTRL_IMAGE_NEGATIVE) ANSI_ESC_END },
90                 { "conceal}",         ANSI_ESC_START _TO_STR(ANSI_CTRL_CONCEAL)        ANSI_ESC_END },
91                 { "crossed-out}",     ANSI_ESC_START _TO_STR(ANSI_CTRL_CROSSED_OUT)    ANSI_ESC_END },
92         #undef _TO_STR
93         #undef _TO_STR2
94             };
95             static const char tok_hdr[] = "${ansi.";
96
97             std::string fmt;
98             for (const char *p = format; *p; ++p)
99             {
100                 const char *tok_start = strstr (p, tok_hdr);
101                 if (!tok_start)
102                 {
103                     fmt.append (p, strlen(p));
104                     break;
105                 }
106
107                 fmt.append (p, tok_start - p);
108                 p = tok_start;
109
110                 const char *tok_str = tok_start + sizeof(tok_hdr) - 1;
111                 for (size_t i = 0; i < sizeof(g_color_tokens) / sizeof(g_color_tokens[0]); ++i)
112                 {
113                     if (!strncmp (tok_str, g_color_tokens[i].name, strlen(g_color_tokens[i].name)))
114                     {
115                         if (do_color)
116                             fmt.append (g_color_tokens[i].value);
117                         p = tok_str + strlen (g_color_tokens[i].name) - 1;
118                         break;
119                     }
120                 }
121             }
122             return fmt;
123         }
124     }
125 }