]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Utility/AnsiTerminal.h
Merge clang 7.0.1 and several follow-up changes
[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 #define ANSI_FG_COLOR_BLACK 30
11 #define ANSI_FG_COLOR_RED 31
12 #define ANSI_FG_COLOR_GREEN 32
13 #define ANSI_FG_COLOR_YELLOW 33
14 #define ANSI_FG_COLOR_BLUE 34
15 #define ANSI_FG_COLOR_PURPLE 35
16 #define ANSI_FG_COLOR_CYAN 36
17 #define ANSI_FG_COLOR_WHITE 37
18
19 #define ANSI_BG_COLOR_BLACK 40
20 #define ANSI_BG_COLOR_RED 41
21 #define ANSI_BG_COLOR_GREEN 42
22 #define ANSI_BG_COLOR_YELLOW 43
23 #define ANSI_BG_COLOR_BLUE 44
24 #define ANSI_BG_COLOR_PURPLE 45
25 #define ANSI_BG_COLOR_CYAN 46
26 #define ANSI_BG_COLOR_WHITE 47
27
28 #define ANSI_SPECIAL_FRAMED 51
29 #define ANSI_SPECIAL_ENCIRCLED 52
30
31 #define ANSI_CTRL_NORMAL 0
32 #define ANSI_CTRL_BOLD 1
33 #define ANSI_CTRL_FAINT 2
34 #define ANSI_CTRL_ITALIC 3
35 #define ANSI_CTRL_UNDERLINE 4
36 #define ANSI_CTRL_SLOW_BLINK 5
37 #define ANSI_CTRL_FAST_BLINK 6
38 #define ANSI_CTRL_IMAGE_NEGATIVE 7
39 #define ANSI_CTRL_CONCEAL 8
40 #define ANSI_CTRL_CROSSED_OUT 9
41
42 #define ANSI_ESC_START "\033["
43 #define ANSI_ESC_END "m"
44
45 #define ANSI_STR(s) #s
46 #define ANSI_DEF_STR(s) ANSI_STR(s)
47
48 #define ANSI_ESCAPE1(s) ANSI_ESC_START ANSI_DEF_STR(s) ANSI_ESC_END
49
50 #define ANSI_1_CTRL(ctrl1) "\033["##ctrl1 ANSI_ESC_END
51 #define ANSI_2_CTRL(ctrl1, ctrl2) "\033["##ctrl1 ";"##ctrl2 ANSI_ESC_END
52
53 #include "llvm/ADT/ArrayRef.h"
54 #include "llvm/ADT/STLExtras.h"
55 #include "llvm/ADT/StringRef.h"
56
57 #include <string>
58
59 namespace lldb_utility {
60
61 namespace ansi {
62
63 inline std::string FormatAnsiTerminalCodes(llvm::StringRef format,
64                                            bool do_color = true) {
65   // Convert "${ansi.XXX}" tokens to ansi values or clear them if do_color is
66   // false.
67   static const struct {
68     const char *name;
69     const char *value;
70   } g_color_tokens[] = {
71 #define _TO_STR2(_val) #_val
72 #define _TO_STR(_val) _TO_STR2(_val)
73       {"fg.black}", ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_BLACK) ANSI_ESC_END},
74       {"fg.red}", ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_RED) ANSI_ESC_END},
75       {"fg.green}", ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_GREEN) ANSI_ESC_END},
76       {"fg.yellow}", ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_YELLOW) ANSI_ESC_END},
77       {"fg.blue}", ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_BLUE) ANSI_ESC_END},
78       {"fg.purple}", ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_PURPLE) ANSI_ESC_END},
79       {"fg.cyan}", ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_CYAN) ANSI_ESC_END},
80       {"fg.white}", ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_WHITE) ANSI_ESC_END},
81       {"bg.black}", ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_BLACK) ANSI_ESC_END},
82       {"bg.red}", ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_RED) ANSI_ESC_END},
83       {"bg.green}", ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_GREEN) ANSI_ESC_END},
84       {"bg.yellow}", ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_YELLOW) ANSI_ESC_END},
85       {"bg.blue}", ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_BLUE) ANSI_ESC_END},
86       {"bg.purple}", ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_PURPLE) ANSI_ESC_END},
87       {"bg.cyan}", ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_CYAN) ANSI_ESC_END},
88       {"bg.white}", ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_WHITE) ANSI_ESC_END},
89       {"normal}", ANSI_ESC_START _TO_STR(ANSI_CTRL_NORMAL) ANSI_ESC_END},
90       {"bold}", ANSI_ESC_START _TO_STR(ANSI_CTRL_BOLD) ANSI_ESC_END},
91       {"faint}", ANSI_ESC_START _TO_STR(ANSI_CTRL_FAINT) ANSI_ESC_END},
92       {"italic}", ANSI_ESC_START _TO_STR(ANSI_CTRL_ITALIC) ANSI_ESC_END},
93       {"underline}", ANSI_ESC_START _TO_STR(ANSI_CTRL_UNDERLINE) ANSI_ESC_END},
94       {"slow-blink}",
95        ANSI_ESC_START _TO_STR(ANSI_CTRL_SLOW_BLINK) ANSI_ESC_END},
96       {"fast-blink}",
97        ANSI_ESC_START _TO_STR(ANSI_CTRL_FAST_BLINK) ANSI_ESC_END},
98       {"negative}",
99        ANSI_ESC_START _TO_STR(ANSI_CTRL_IMAGE_NEGATIVE) ANSI_ESC_END},
100       {"conceal}", ANSI_ESC_START _TO_STR(ANSI_CTRL_CONCEAL) ANSI_ESC_END},
101       {"crossed-out}",
102        ANSI_ESC_START _TO_STR(ANSI_CTRL_CROSSED_OUT) ANSI_ESC_END},
103 #undef _TO_STR
104 #undef _TO_STR2
105   };
106   auto codes = llvm::makeArrayRef(g_color_tokens);
107
108   static const char tok_hdr[] = "${ansi.";
109
110   std::string fmt;
111   while (!format.empty()) {
112     llvm::StringRef left, right;
113     std::tie(left, right) = format.split(tok_hdr);
114
115     fmt.append(left);
116
117     if (left == format && right.empty()) {
118       // The header was not found.  Just exit.
119       break;
120     }
121
122     bool found_code = false;
123     for (const auto &code : codes) {
124       if (!right.consume_front(code.name))
125         continue;
126
127       if (do_color)
128         fmt.append(code.value);
129       found_code = true;
130       break;
131     }
132     format = right;
133     // If we haven't found a valid replacement value, we just copy the string
134     // to the result without any modifications.
135     if (!found_code)
136       fmt.append(tok_hdr);
137   }
138   return fmt;
139 }
140 }
141 }