9#ifndef LLDB_UTILITY_ANSITERMINAL_H
10#define LLDB_UTILITY_ANSITERMINAL_H
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
21#define ANSI_FG_COLOR_BRIGHT_BLACK 90
22#define ANSI_FG_COLOR_BRIGHT_RED 91
23#define ANSI_FG_COLOR_BRIGHT_GREEN 92
24#define ANSI_FG_COLOR_BRIGHT_YELLOW 93
25#define ANSI_FG_COLOR_BRIGHT_BLUE 94
26#define ANSI_FG_COLOR_BRIGHT_PURPLE 95
27#define ANSI_FG_COLOR_BRIGHT_CYAN 96
28#define ANSI_FG_COLOR_BRIGHT_WHITE 97
30#define ANSI_BG_COLOR_BLACK 40
31#define ANSI_BG_COLOR_RED 41
32#define ANSI_BG_COLOR_GREEN 42
33#define ANSI_BG_COLOR_YELLOW 43
34#define ANSI_BG_COLOR_BLUE 44
35#define ANSI_BG_COLOR_PURPLE 45
36#define ANSI_BG_COLOR_CYAN 46
37#define ANSI_BG_COLOR_WHITE 47
39#define ANSI_BG_COLOR_BRIGHT_BLACK 100
40#define ANSI_BG_COLOR_BRIGHT_RED 101
41#define ANSI_BG_COLOR_BRIGHT_GREEN 102
42#define ANSI_BG_COLOR_BRIGHT_YELLOW 103
43#define ANSI_BG_COLOR_BRIGHT_BLUE 104
44#define ANSI_BG_COLOR_BRIGHT_PURPLE 105
45#define ANSI_BG_COLOR_BRIGHT_CYAN 106
46#define ANSI_BG_COLOR_BRIGHT_WHITE 107
48#define ANSI_SPECIAL_FRAMED 51
49#define ANSI_SPECIAL_ENCIRCLED 52
51#define ANSI_CTRL_NORMAL 0
52#define ANSI_CTRL_BOLD 1
53#define ANSI_CTRL_FAINT 2
54#define ANSI_CTRL_ITALIC 3
55#define ANSI_CTRL_UNDERLINE 4
56#define ANSI_CTRL_SLOW_BLINK 5
57#define ANSI_CTRL_FAST_BLINK 6
58#define ANSI_CTRL_IMAGE_NEGATIVE 7
59#define ANSI_CTRL_CONCEAL 8
60#define ANSI_CTRL_CROSSED_OUT 9
62#define ANSI_ESC_START "\033["
63#define ANSI_ESC_END "m"
66#define ANSI_DEF_STR(s) ANSI_STR(s)
68#define ANSI_ESCAPE1(s) ANSI_ESC_START ANSI_DEF_STR(s) ANSI_ESC_END
70#define ANSI_1_CTRL(ctrl1) "\033["##ctrl1 ANSI_ESC_END
71#define ANSI_2_CTRL(ctrl1, ctrl2) "\033["##ctrl1 ";"##ctrl2 ANSI_ESC_END
73#define ANSI_ESC_START_LEN 2
75#include "llvm/ADT/ArrayRef.h"
76#include "llvm/ADT/STLExtras.h"
77#include "llvm/ADT/StringRef.h"
78#include "llvm/Support/Locale.h"
87 bool do_color =
true) {
94 } g_color_tokens[] = {
95#define _TO_STR2(_val) #_val
96#define _TO_STR(_val) _TO_STR2(_val)
143 auto codes = llvm::ArrayRef(g_color_tokens);
145 static const char tok_hdr[] =
"${ansi.";
148 while (!format.empty()) {
149 llvm::StringRef left, right;
150 std::tie(left, right) = format.split(tok_hdr);
154 if (left == format && right.empty()) {
159 bool found_code =
false;
160 for (
const auto &code : codes) {
161 if (!right.consume_front(code.name))
165 fmt.append(code.value);
178inline std::tuple<llvm::StringRef, llvm::StringRef, llvm::StringRef>
180 llvm::StringRef left;
181 llvm::StringRef right = str;
183 while (!right.empty()) {
187 if (start == llvm::StringRef::npos)
188 return {str, {}, {}};
191 left = str.take_front(left.size() + start);
192 llvm::StringRef escape = right.substr(start);
195 const size_t end = right.find_first_not_of(
"0123456789;");
198 if (end < right.size() && (right[end] ==
'm' || right[end] ==
'G'))
200 right.substr(end + 1)};
206 return {str, {}, {}};
210 std::string stripped;
211 while (!str.empty()) {
219inline std::string
TrimAndPad(llvm::StringRef str,
size_t visible_length,
220 char padding =
' ') {
222 result.reserve(visible_length);
223 size_t result_visibile_length = 0;
226 while (!str.empty()) {
232 size_t column_width = llvm::sys::locale::columnWidth(left);
233 if (result_visibile_length + column_width <= visible_length) {
234 result.append(left).append(escape);
235 result_visibile_length += column_width;
241 llvm::StringRef trimmed = left;
242 while (!trimmed.empty()) {
246 column_width = llvm::sys::locale::columnWidth(trimmed);
247 if (result_visibile_length + column_width <= visible_length) {
248 result.append(trimmed);
249 result_visibile_length += column_width;
252 trimmed = trimmed.drop_back();
257 if (result_visibile_length < visible_length)
258 result.append(visible_length - result_visibile_length, padding);
265 return llvm::sys::locale::columnWidth(stripped);
#define ANSI_FG_COLOR_BRIGHT_GREEN
#define ANSI_BG_COLOR_BRIGHT_BLUE
#define ANSI_BG_COLOR_PURPLE
#define ANSI_CTRL_UNDERLINE
#define ANSI_BG_COLOR_BRIGHT_WHITE
#define ANSI_BG_COLOR_BRIGHT_RED
#define ANSI_BG_COLOR_RED
#define ANSI_FG_COLOR_BLACK
#define ANSI_FG_COLOR_BRIGHT_CYAN
#define ANSI_FG_COLOR_BRIGHT_BLACK
#define ANSI_BG_COLOR_YELLOW
#define ANSI_FG_COLOR_BRIGHT_WHITE
#define ANSI_BG_COLOR_BLACK
#define ANSI_FG_COLOR_BRIGHT_YELLOW
#define ANSI_FG_COLOR_YELLOW
#define ANSI_FG_COLOR_PURPLE
#define ANSI_BG_COLOR_CYAN
#define ANSI_FG_COLOR_BRIGHT_RED
#define ANSI_BG_COLOR_BRIGHT_YELLOW
#define ANSI_BG_COLOR_GREEN
#define ANSI_CTRL_SLOW_BLINK
#define ANSI_FG_COLOR_RED
#define ANSI_BG_COLOR_BLUE
#define ANSI_FG_COLOR_BRIGHT_BLUE
#define ANSI_FG_COLOR_CYAN
#define ANSI_CTRL_FAST_BLINK
#define ANSI_CTRL_CROSSED_OUT
#define ANSI_CTRL_IMAGE_NEGATIVE
#define ANSI_BG_COLOR_WHITE
#define ANSI_CTRL_CONCEAL
#define ANSI_ESC_START_LEN
#define ANSI_FG_COLOR_GREEN
#define ANSI_FG_COLOR_BLUE
#define ANSI_BG_COLOR_BRIGHT_GREEN
#define ANSI_FG_COLOR_BRIGHT_PURPLE
#define ANSI_BG_COLOR_BRIGHT_CYAN
#define ANSI_FG_COLOR_WHITE
#define ANSI_BG_COLOR_BRIGHT_PURPLE
#define ANSI_BG_COLOR_BRIGHT_BLACK
std::string FormatAnsiTerminalCodes(llvm::StringRef format, bool do_color=true)
size_t ColumnWidth(llvm::StringRef str)
std::tuple< llvm::StringRef, llvm::StringRef, llvm::StringRef > FindNextAnsiSequence(llvm::StringRef str)
std::string TrimAndPad(llvm::StringRef str, size_t visible_length, char padding=' ')
std::string StripAnsiTerminalCodes(llvm::StringRef str)
A class that represents a running process on the host machine.