LLDB mainline
AnsiTerminal.h
Go to the documentation of this file.
1//===---------------------AnsiTerminal.h ------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLDB_UTILITY_ANSITERMINAL_H
10#define LLDB_UTILITY_ANSITERMINAL_H
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_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
29
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
38
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
47
48#define ANSI_SPECIAL_FRAMED 51
49#define ANSI_SPECIAL_ENCIRCLED 52
50
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
61
62#define ANSI_ESC_START "\033["
63#define ANSI_ESC_END "m"
64
65#define ANSI_STR(s) #s
66#define ANSI_DEF_STR(s) ANSI_STR(s)
67
68#define ANSI_ESCAPE1(s) ANSI_ESC_START ANSI_DEF_STR(s) ANSI_ESC_END
69
70#define ANSI_1_CTRL(ctrl1) "\033["##ctrl1 ANSI_ESC_END
71#define ANSI_2_CTRL(ctrl1, ctrl2) "\033["##ctrl1 ";"##ctrl2 ANSI_ESC_END
72
73#define ANSI_ESC_START_LEN 2
74
75#include "llvm/ADT/ArrayRef.h"
76#include "llvm/ADT/STLExtras.h"
77#include "llvm/ADT/StringRef.h"
78#include "llvm/Support/Locale.h"
79
80#include <string>
81
82namespace lldb_private {
83
84namespace ansi {
85
86inline std::string FormatAnsiTerminalCodes(llvm::StringRef format,
87 bool do_color = true) {
88 // Convert "${ansi.XXX}" tokens to ansi values or clear them if do_color is
89 // false.
90 // clang-format off
91 static const struct {
92 const char *name;
93 const char *value;
94 } g_color_tokens[] = {
95#define _TO_STR2(_val) #_val
96#define _TO_STR(_val) _TO_STR2(_val)
139#undef _TO_STR
140#undef _TO_STR2
141 };
142 // clang-format on
143 auto codes = llvm::ArrayRef(g_color_tokens);
144
145 static const char tok_hdr[] = "${ansi.";
146
147 std::string fmt;
148 while (!format.empty()) {
149 llvm::StringRef left, right;
150 std::tie(left, right) = format.split(tok_hdr);
151
152 fmt += left;
153
154 if (left == format && right.empty()) {
155 // The header was not found. Just exit.
156 break;
157 }
158
159 bool found_code = false;
160 for (const auto &code : codes) {
161 if (!right.consume_front(code.name))
162 continue;
163
164 if (do_color)
165 fmt.append(code.value);
166 found_code = true;
167 break;
168 }
169 format = right;
170 // If we haven't found a valid replacement value, we just copy the string
171 // to the result without any modifications.
172 if (!found_code)
173 fmt.append(tok_hdr);
174 }
175 return fmt;
176}
177
178inline std::tuple<llvm::StringRef, llvm::StringRef, llvm::StringRef>
179FindNextAnsiSequence(llvm::StringRef str) {
180 llvm::StringRef left;
181 llvm::StringRef right = str;
182
183 while (!right.empty()) {
184 const size_t start = right.find(ANSI_ESC_START);
185
186 // ANSI_ESC_START not found.
187 if (start == llvm::StringRef::npos)
188 return {str, {}, {}};
189
190 // Split the string around the current ANSI_ESC_START.
191 left = str.take_front(left.size() + start);
192 llvm::StringRef escape = right.substr(start);
193 right = right.substr(start + ANSI_ESC_START_LEN + 1);
194
195 const size_t end = right.find_first_not_of("0123456789;");
196
197 // ANSI_ESC_END found.
198 if (end < right.size() && (right[end] == 'm' || right[end] == 'G'))
199 return {left, escape.take_front(ANSI_ESC_START_LEN + 1 + end + 1),
200 right.substr(end + 1)};
201
202 // Maintain the invariant that str == left + right at the start of the loop.
203 left = str.take_front(left.size() + ANSI_ESC_START_LEN + 1);
204 }
205
206 return {str, {}, {}};
207}
208
209inline std::string StripAnsiTerminalCodes(llvm::StringRef str) {
210 std::string stripped;
211 while (!str.empty()) {
212 auto [left, escape, right] = FindNextAnsiSequence(str);
213 stripped += left;
214 str = right;
215 }
216 return stripped;
217}
218
219inline std::string TrimAndPad(llvm::StringRef str, size_t visible_length,
220 char padding = ' ') {
221 std::string result;
222 result.reserve(visible_length);
223 size_t result_visibile_length = 0;
224
225 // Trim the string to the given visible length.
226 while (!str.empty()) {
227 auto [left, escape, right] = FindNextAnsiSequence(str);
228 str = right;
229
230 // Compute the length of the string without escape codes. If it fits, append
231 // it together with the invisible escape code.
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;
236 continue;
237 }
238
239 // The string might contain unicode which means it's not safe to truncate.
240 // Repeatedly trim the string until it its valid unicode and fits.
241 llvm::StringRef trimmed = left;
242 while (!trimmed.empty()) {
243 // This relies on columnWidth returning -2 for invalid/partial unicode
244 // characters, which after conversion to size_t will be larger than the
245 // visible width.
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;
250 break;
251 }
252 trimmed = trimmed.drop_back();
253 }
254 }
255
256 // Pad the string.
257 if (result_visibile_length < visible_length)
258 result.append(visible_length - result_visibile_length, padding);
259
260 return result;
261}
262
263inline size_t ColumnWidth(llvm::StringRef str) {
264 std::string stripped = ansi::StripAnsiTerminalCodes(str);
265 return llvm::sys::locale::columnWidth(stripped);
266}
267
268} // namespace ansi
269} // namespace lldb_private
270
271#endif
#define ANSI_FG_COLOR_BRIGHT_GREEN
#define ANSI_BG_COLOR_BRIGHT_BLUE
#define ANSI_ESC_START
#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_CTRL_ITALIC
#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_CTRL_FAINT
#define ANSI_FG_COLOR_BLUE
#define ANSI_BG_COLOR_BRIGHT_GREEN
#define ANSI_FG_COLOR_BRIGHT_PURPLE
#define ANSI_CTRL_NORMAL
#define ANSI_BG_COLOR_BRIGHT_CYAN
#define ANSI_CTRL_BOLD
#define ANSI_FG_COLOR_WHITE
#define ANSI_BG_COLOR_BRIGHT_PURPLE
#define ANSI_ESC_END
#define ANSI_BG_COLOR_BRIGHT_BLACK
#define _TO_STR(_val)
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.