Add translations for various sub-directories
[binutils-gdb.git] / gdb / ui-style.h
blobd814588254143149e5b24a45f3a3cdd8a24c91da
1 /* Styling for ui_file
2 Copyright (C) 2018-2024 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #ifndef GDB_UI_STYLE_H
20 #define GDB_UI_STYLE_H
22 /* One of the color spaces that usually supported by terminals. */
23 enum class color_space
25 /* one default terminal color */
26 MONOCHROME,
28 /* foreground colors \e[30m ... \e[37m,
29 background colors \e[40m ... \e[47m */
30 ANSI_8COLOR,
32 /* foreground colors \e[30m ... \e[37m, \e[90m ... \e[97m
33 background colors \e[40m ... \e[47m, \e[100m ... \e107m */
34 AIXTERM_16COLOR,
36 /* foreground colors \e[38;5;0m ... \e[38;5;255m
37 background colors \e[48;5;0m ... \e[48;5;255m */
38 XTERM_256COLOR,
40 /* foreground colors \e[38;2;0;0;0m ... \e[38;2;255;255;255m
41 background colors \e[48;2;0;0;0m ... \e[48;2;255;255;255m */
42 RGB_24BIT
45 /* Color spaces supported by terminal. */
46 extern const std::vector<color_space> & colorsupport ();
48 /* Textual representation of C. */
49 extern const char * color_space_name (color_space c);
51 /* Cast C to RESULT and return true if it's value is valid; false otherwise. */
52 extern bool color_space_safe_cast (color_space *result, long c);
54 /* Styles that can be applied to a ui_file. */
55 struct ui_file_style
57 /* One of the basic colors that can be handled by ANSI
58 terminals. */
59 enum basic_color
61 NONE = -1,
62 BLACK,
63 RED,
64 GREEN,
65 YELLOW,
66 BLUE,
67 MAGENTA,
68 CYAN,
69 WHITE
72 /* Representation of a terminal color. */
73 class color
75 public:
77 color (basic_color c)
78 : m_color_space (c == NONE ? color_space::MONOCHROME
79 : color_space::ANSI_8COLOR),
80 m_value (c)
84 color (int c)
85 : m_value (c)
87 if (c < -1 || c > 255)
88 error (_("Palette color index %d is out of range."), c);
89 if (c == -1)
90 m_color_space = color_space::MONOCHROME;
91 else if (c <= 7)
92 m_color_space = color_space::ANSI_8COLOR;
93 else if (c <= 15)
94 m_color_space = color_space::AIXTERM_16COLOR;
95 else
96 m_color_space = color_space::XTERM_256COLOR;
99 color (color_space cs, int c)
100 : m_color_space (cs),
101 m_value (c)
103 if (c < -1 || c > 255)
104 error (_("Palette color index %d is out of range."), c);
106 std::pair<int, int> range;
107 switch (cs)
109 case color_space::MONOCHROME:
110 range = {-1, -1};
111 break;
112 case color_space::ANSI_8COLOR:
113 range = {0, 7};
114 break;
115 case color_space::AIXTERM_16COLOR:
116 range = {0, 15};
117 break;
118 case color_space::XTERM_256COLOR:
119 range = {0, 255};
120 break;
121 default:
122 error (_("Color space %d is incompatible with indexed colors."),
123 static_cast<int> (cs));
126 if (c < range.first || c > range.second)
127 error (_("Color %d is out of range [%d, %d] of color space %d."),
128 c, range.first, range.second, static_cast<int> (cs));
131 color (uint8_t r, uint8_t g, uint8_t b)
132 : m_color_space (color_space::RGB_24BIT),
133 m_red (r),
134 m_green (g),
135 m_blue (b)
139 bool operator== (const color &other) const
141 if (m_color_space != other.m_color_space)
142 return false;
143 if (is_simple ())
144 return m_value == other.m_value;
145 return (m_red == other.m_red && m_green == other.m_green
146 && m_blue == other.m_blue);
149 bool operator!= (const color &other) const
151 return ! (*this == other);
154 bool operator< (const color &other) const
156 if (m_color_space != other.m_color_space)
157 return m_color_space < other.m_color_space;
158 if (is_simple ())
159 return m_value < other.m_value;
160 if (m_red < other.m_red)
161 return true;
162 if (m_red == other.m_red)
164 if (m_green < other.m_green)
165 return true;
166 if (m_green == other.m_green)
167 return m_blue < other.m_blue;
169 return false;
172 color_space colorspace () const
174 return m_color_space;
177 /* Return true if this is the "NONE" color, false otherwise. */
178 bool is_none () const
180 return m_color_space == color_space::MONOCHROME && m_value == NONE;
183 /* Return true if this is one of the basic colors, false
184 otherwise. */
185 bool is_basic () const
187 if (m_color_space == color_space::ANSI_8COLOR
188 || m_color_space == color_space::AIXTERM_16COLOR)
189 return BLACK <= m_value && m_value <= WHITE;
190 else
191 return false;
194 /* Return true if this is one of the colors, stored as int, false
195 otherwise. */
196 bool is_simple () const
198 return m_color_space != color_space::RGB_24BIT;
201 /* Return true if this is one of the indexed colors, false
202 otherwise. */
203 bool is_indexed () const
205 return m_color_space != color_space::RGB_24BIT
206 && m_color_space != color_space::MONOCHROME;
209 /* Return true if this is one of the direct colors (RGB, CMY, CMYK), false
210 otherwise. */
211 bool is_direct () const
213 return m_color_space == color_space::RGB_24BIT;
216 /* Return the value of a simple color. */
217 int get_value () const
219 gdb_assert (is_simple ());
220 return m_value;
223 /* Fill in RGB with the red/green/blue values for this color.
224 This may not be called for basic colors or for the "NONE"
225 color. */
226 void get_rgb (uint8_t *rgb) const;
228 /* Append the ANSI terminal escape sequence for this color to STR.
229 IS_FG indicates whether this is a foreground or background
230 color. */
231 void append_ansi (bool is_fg, std::string *str) const;
233 /* Return the ANSI escape sequence for this color.
234 IS_FG indicates whether this is a foreground or background color. */
235 std::string to_ansi (bool is_fg) const;
237 /* Returns text representation of this object.
238 It is "none", name of a basic color, number or a #RRGGBB hex triplet. */
239 std::string to_string () const;
241 /* Approximates THIS color by closest one from SPACES. */
242 color approximate (const std::vector<color_space> &spaces) const;
244 private:
246 color_space m_color_space;
247 union
249 int m_value;
250 struct
252 uint8_t m_red, m_green, m_blue;
257 /* Intensity settings that are available. */
258 enum intensity
260 NORMAL = 0,
261 BOLD,
265 ui_file_style () = default;
267 ui_file_style (color f, color b, intensity i = NORMAL)
268 : m_foreground (f),
269 m_background (b),
270 m_intensity (i)
274 bool operator== (const ui_file_style &other) const
276 return (m_foreground == other.m_foreground
277 && m_background == other.m_background
278 && m_intensity == other.m_intensity
279 && m_reverse == other.m_reverse);
282 bool operator!= (const ui_file_style &other) const
284 return !(*this == other);
287 /* Return the ANSI escape sequence for this style. */
288 std::string to_ansi () const;
290 /* Return true if this style is the default style; false
291 otherwise. */
292 bool is_default () const
294 return (m_foreground == NONE
295 && m_background == NONE
296 && m_intensity == NORMAL
297 && !m_reverse);
300 /* Return true if this style specified reverse display; false
301 otherwise. */
302 bool is_reverse () const
304 return m_reverse;
307 /* Set/clear the reverse display flag. */
308 void set_reverse (bool reverse)
310 m_reverse = reverse;
313 /* Return the foreground color of this style. */
314 const color &get_foreground () const
316 return m_foreground;
319 /* Set the foreground color of this style. */
320 void set_fg (color c)
322 m_foreground = c;
325 /* Return the background color of this style. */
326 const color &get_background () const
328 return m_background;
331 /* Set the background color of this style. */
332 void set_bg (color c)
334 m_background = c;
337 /* Return the intensity of this style. */
338 intensity get_intensity () const
340 return m_intensity;
343 /* Parse an ANSI escape sequence in BUF, modifying this style. BUF
344 must begin with an ESC character. Return true if an escape
345 sequence was successfully parsed; false otherwise. In either
346 case, N_READ is updated to reflect the number of chars read from
347 BUF. */
348 bool parse (const char *buf, size_t *n_read);
350 /* We need this because we can't pass a reference via va_args. */
351 const ui_file_style *ptr () const
353 return this;
356 /* nullptr-terminated list of names corresponding to enum basic_color. */
357 static const std::vector<const char *> basic_color_enums;
359 private:
361 color m_foreground = NONE;
362 color m_background = NONE;
363 intensity m_intensity = NORMAL;
364 bool m_reverse = false;
367 /* Skip an ANSI escape sequence in BUF. BUF must begin with an ESC
368 character. Return true if an escape sequence was successfully
369 skipped; false otherwise. If an escape sequence was skipped,
370 N_READ is updated to reflect the number of chars read from BUF. */
372 extern bool skip_ansi_escape (const char *buf, int *n_read);
374 #endif /* GDB_UI_STYLE_H */