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 */
28 /* foreground colors \e[30m ... \e[37m,
29 background colors \e[40m ... \e[47m */
32 /* foreground colors \e[30m ... \e[37m, \e[90m ... \e[97m
33 background colors \e[40m ... \e[47m, \e[100m ... \e107m */
36 /* foreground colors \e[38;5;0m ... \e[38;5;255m
37 background colors \e[48;5;0m ... \e[48;5;255m */
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 */
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. */
57 /* One of the basic colors that can be handled by ANSI
72 /* Representation of a terminal color. */
78 : m_color_space (c
== NONE
? color_space::MONOCHROME
79 : color_space::ANSI_8COLOR
),
87 if (c
< -1 || c
> 255)
88 error (_("Palette color index %d is out of range."), c
);
90 m_color_space
= color_space::MONOCHROME
;
92 m_color_space
= color_space::ANSI_8COLOR
;
94 m_color_space
= color_space::AIXTERM_16COLOR
;
96 m_color_space
= color_space::XTERM_256COLOR
;
99 color (color_space cs
, int c
)
100 : m_color_space (cs
),
103 if (c
< -1 || c
> 255)
104 error (_("Palette color index %d is out of range."), c
);
106 std::pair
<int, int> range
;
109 case color_space::MONOCHROME
:
112 case color_space::ANSI_8COLOR
:
115 case color_space::AIXTERM_16COLOR
:
118 case color_space::XTERM_256COLOR
:
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
),
139 bool operator== (const color
&other
) const
141 if (m_color_space
!= other
.m_color_space
)
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
;
159 return m_value
< other
.m_value
;
160 if (m_red
< other
.m_red
)
162 if (m_red
== other
.m_red
)
164 if (m_green
< other
.m_green
)
166 if (m_green
== other
.m_green
)
167 return m_blue
< other
.m_blue
;
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
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
;
194 /* Return true if this is one of the colors, stored as int, false
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
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
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 ());
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"
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
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;
246 color_space m_color_space
;
252 uint8_t m_red
, m_green
, m_blue
;
257 /* Intensity settings that are available. */
265 ui_file_style () = default;
267 ui_file_style (color f
, color b
, intensity i
= NORMAL
)
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
292 bool is_default () const
294 return (m_foreground
== NONE
295 && m_background
== NONE
296 && m_intensity
== NORMAL
300 /* Return true if this style specified reverse display; false
302 bool is_reverse () const
307 /* Set/clear the reverse display flag. */
308 void set_reverse (bool reverse
)
313 /* Return the foreground color of this style. */
314 const color
&get_foreground () const
319 /* Set the foreground color of this style. */
320 void set_fg (color c
)
325 /* Return the background color of this style. */
326 const color
&get_background () const
331 /* Set the background color of this style. */
332 void set_bg (color c
)
337 /* Return the intensity of this style. */
338 intensity
get_intensity () const
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
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
356 /* nullptr-terminated list of names corresponding to enum basic_color. */
357 static const std::vector
<const char *> basic_color_enums
;
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 */