gdb/testsuite: make test names unique in gdb.python/py-format-string.exp
[deliverable/binutils-gdb.git] / gdb / cli / cli-style.c
CommitLineData
cbe56571
TT
1/* CLI colorizing
2
3666a048 3 Copyright (C) 2018-2021 Free Software Foundation, Inc.
cbe56571
TT
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20#include "defs.h"
21#include "cli/cli-cmds.h"
9d2d8a16 22#include "cli/cli-setshow.h"
cbe56571 23#include "cli/cli-style.h"
62f29fda 24#include "source-cache.h"
6f11e682 25#include "observable.h"
cbe56571
TT
26
27/* True if styling is enabled. */
28
06a6207a 29#if defined (__MSDOS__)
491144b5 30bool cli_styling = false;
cbe56571 31#else
491144b5 32bool cli_styling = true;
cbe56571
TT
33#endif
34
d085f989
TT
35/* True if source styling is enabled. Note that this is only
36 consulted when cli_styling is true. */
37
491144b5 38bool source_styling = true;
d085f989 39
cbe56571
TT
40/* Name of colors; must correspond to ui_file_style::basic_color. */
41static const char * const cli_colors[] = {
42 "none",
43 "black",
44 "red",
45 "green",
46 "yellow",
47 "blue",
48 "magenta",
49 "cyan",
50 "white",
51 nullptr
52};
53
54/* Names of intensities; must correspond to
55 ui_file_style::intensity. */
56static const char * const cli_intensities[] = {
57 "normal",
58 "bold",
59 "dim",
60 nullptr
61};
62
63/* See cli-style.h. */
64
9303eb2f 65cli_style_option file_name_style ("filename", ui_file_style::GREEN);
cbe56571
TT
66
67/* See cli-style.h. */
68
9303eb2f 69cli_style_option function_name_style ("function", ui_file_style::YELLOW);
cbe56571
TT
70
71/* See cli-style.h. */
72
9303eb2f 73cli_style_option variable_name_style ("variable", ui_file_style::CYAN);
80ae2043
TT
74
75/* See cli-style.h. */
76
9303eb2f 77cli_style_option address_style ("address", ui_file_style::BLUE);
35fb8261
TT
78
79/* See cli-style.h. */
80
9303eb2f
PW
81cli_style_option highlight_style ("highlight", ui_file_style::RED);
82
83/* See cli-style.h. */
84
85cli_style_option title_style ("title", ui_file_style::BOLD);
86
87/* See cli-style.h. */
88
a2a7af0c
TT
89cli_style_option tui_border_style ("tui-border", ui_file_style::CYAN);
90
91/* See cli-style.h. */
92
93cli_style_option tui_active_border_style ("tui-active-border",
94 ui_file_style::CYAN);
95
96/* See cli-style.h. */
97
7f6aba03
TT
98cli_style_option metadata_style ("metadata", ui_file_style::DIM);
99
100/* See cli-style.h. */
101
9d2d8a16
AB
102cli_style_option version_style ("version", ui_file_style::MAGENTA,
103 ui_file_style::BOLD);
104
105/* See cli-style.h. */
106
9303eb2f 107cli_style_option::cli_style_option (const char *name,
9d2d8a16
AB
108 ui_file_style::basic_color fg,
109 ui_file_style::intensity intensity)
a2a7af0c
TT
110 : changed (name),
111 m_name (name),
9303eb2f 112 m_foreground (cli_colors[fg - ui_file_style::NONE]),
cbe56571 113 m_background (cli_colors[0]),
9d2d8a16 114 m_intensity (cli_intensities[intensity])
cbe56571
TT
115{
116}
117
9303eb2f
PW
118/* See cli-style.h. */
119
120cli_style_option::cli_style_option (const char *name,
121 ui_file_style::intensity i)
a2a7af0c
TT
122 : changed (name),
123 m_name (name),
9303eb2f
PW
124 m_foreground (cli_colors[0]),
125 m_background (cli_colors[0]),
126 m_intensity (cli_intensities[i])
127{
128}
129
cbe56571
TT
130/* Return the color number corresponding to COLOR. */
131
132static int
133color_number (const char *color)
134{
135 for (int i = 0; i < ARRAY_SIZE (cli_colors); ++i)
136 {
137 if (color == cli_colors[i])
138 return i - 1;
139 }
140 gdb_assert_not_reached ("color not found");
141}
142
143/* See cli-style.h. */
144
145ui_file_style
146cli_style_option::style () const
147{
148 int fg = color_number (m_foreground);
149 int bg = color_number (m_background);
150 ui_file_style::intensity intensity = ui_file_style::NORMAL;
151
152 for (int i = 0; i < ARRAY_SIZE (cli_intensities); ++i)
153 {
154 if (m_intensity == cli_intensities[i])
155 {
156 intensity = (ui_file_style::intensity) i;
157 break;
158 }
159 }
160
161 return ui_file_style (fg, bg, intensity);
162}
163
a2a7af0c
TT
164/* See cli-style.h. */
165
166void
167cli_style_option::do_set_value (const char *ignore, int from_tty,
168 struct cmd_list_element *cmd)
169{
170 cli_style_option *cso = (cli_style_option *) get_cmd_context (cmd);
171 cso->changed.notify ();
172}
173
9303eb2f
PW
174/* Implements the cli_style_option::do_show_* functions.
175 WHAT and VALUE are the property and value to show.
176 The style for which WHAT is shown is retrieved from CMD context. */
177
178static void
179do_show (const char *what, struct ui_file *file,
180 struct cmd_list_element *cmd,
181 const char *value)
182{
183 cli_style_option *cso = (cli_style_option *) get_cmd_context (cmd);
184 fputs_filtered (_("The "), file);
185 fprintf_styled (file, cso->style (), _("\"%s\" style"), cso->name ());
186 fprintf_filtered (file, _(" %s is: %s\n"), what, value);
187}
188
cbe56571
TT
189/* See cli-style.h. */
190
cbe56571
TT
191void
192cli_style_option::do_show_foreground (struct ui_file *file, int from_tty,
193 struct cmd_list_element *cmd,
194 const char *value)
195{
9303eb2f 196 do_show (_("foreground color"), file, cmd, value);
cbe56571
TT
197}
198
199/* See cli-style.h. */
200
201void
202cli_style_option::do_show_background (struct ui_file *file, int from_tty,
203 struct cmd_list_element *cmd,
204 const char *value)
205{
9303eb2f 206 do_show (_("background color"), file, cmd, value);
cbe56571
TT
207}
208
209/* See cli-style.h. */
210
211void
212cli_style_option::do_show_intensity (struct ui_file *file, int from_tty,
213 struct cmd_list_element *cmd,
214 const char *value)
215{
9303eb2f 216 do_show (_("display intensity"), file, cmd, value);
cbe56571
TT
217}
218
219/* See cli-style.h. */
220
221void
9303eb2f 222cli_style_option::add_setshow_commands (enum command_class theclass,
cbe56571 223 const char *prefix_doc,
cbe56571 224 struct cmd_list_element **set_list,
d73cff18 225 struct cmd_list_element **show_list,
a2a7af0c 226 bool skip_intensity)
cbe56571 227{
9303eb2f
PW
228 m_set_prefix = std::string ("set style ") + m_name + " ";
229 m_show_prefix = std::string ("show style ") + m_name + " ";
cbe56571 230
0743fc83
TT
231 add_basic_prefix_cmd (m_name, no_class, prefix_doc, &m_set_list,
232 m_set_prefix.c_str (), 0, set_list);
233 add_show_prefix_cmd (m_name, no_class, prefix_doc, &m_show_list,
234 m_show_prefix.c_str (), 0, show_list);
cbe56571
TT
235
236 add_setshow_enum_cmd ("foreground", theclass, cli_colors,
237 &m_foreground,
590042fc
PW
238 _("Set the foreground color for this property."),
239 _("Show the foreground color for this property."),
cbe56571 240 nullptr,
a2a7af0c 241 do_set_value,
cbe56571 242 do_show_foreground,
9303eb2f 243 &m_set_list, &m_show_list, (void *) this);
cbe56571
TT
244 add_setshow_enum_cmd ("background", theclass, cli_colors,
245 &m_background,
590042fc
PW
246 _("Set the background color for this property."),
247 _("Show the background color for this property."),
cbe56571 248 nullptr,
a2a7af0c 249 do_set_value,
cbe56571 250 do_show_background,
9303eb2f 251 &m_set_list, &m_show_list, (void *) this);
a2a7af0c
TT
252 if (!skip_intensity)
253 add_setshow_enum_cmd ("intensity", theclass, cli_intensities,
254 &m_intensity,
255 _("Set the display intensity for this property."),
256 _("Show the display intensity for this property."),
257 nullptr,
258 do_set_value,
259 do_show_intensity,
260 &m_set_list, &m_show_list, (void *) this);
cbe56571
TT
261}
262
d73cff18
PW
263static cmd_list_element *style_set_list;
264static cmd_list_element *style_show_list;
265
62f29fda
TT
266static void
267set_style_enabled (const char *args, int from_tty, struct cmd_list_element *c)
268{
269 g_source_cache.clear ();
6f11e682 270 gdb::observers::source_styling_changed.notify ();
62f29fda
TT
271}
272
cbe56571
TT
273static void
274show_style_enabled (struct ui_file *file, int from_tty,
275 struct cmd_list_element *c, const char *value)
276{
277 if (cli_styling)
278 fprintf_filtered (file, _("CLI output styling is enabled.\n"));
279 else
280 fprintf_filtered (file, _("CLI output styling is disabled.\n"));
281}
282
d085f989
TT
283static void
284show_style_sources (struct ui_file *file, int from_tty,
285 struct cmd_list_element *c, const char *value)
286{
287 if (source_styling)
288 fprintf_filtered (file, _("Source code styling is enabled.\n"));
289 else
290 fprintf_filtered (file, _("Source code styling is disabled.\n"));
291}
292
6c265988 293void _initialize_cli_style ();
cbe56571
TT
294void
295_initialize_cli_style ()
296{
0743fc83 297 add_basic_prefix_cmd ("style", no_class, _("\
590042fc 298Style-specific settings.\n\
cbe56571
TT
299Configure various style-related variables, such as colors"),
300 &style_set_list, "set style ", 0, &setlist);
0743fc83 301 add_show_prefix_cmd ("style", no_class, _("\
590042fc 302Style-specific settings.\n\
cbe56571
TT
303Configure various style-related variables, such as colors"),
304 &style_show_list, "show style ", 0, &showlist);
305
306 add_setshow_boolean_cmd ("enabled", no_class, &cli_styling, _("\
307Set whether CLI styling is enabled."), _("\
308Show whether CLI is enabled."), _("\
309If enabled, output to the terminal is styled."),
62f29fda 310 set_style_enabled, show_style_enabled,
cbe56571
TT
311 &style_set_list, &style_show_list);
312
d085f989
TT
313 add_setshow_boolean_cmd ("sources", no_class, &source_styling, _("\
314Set whether source code styling is enabled."), _("\
315Show whether source code styling is enabled."), _("\
316If enabled, source code is styled.\n"
317#ifdef HAVE_SOURCE_HIGHLIGHT
318"Note that source styling only works if styling in general is enabled,\n\
319see \"show style enabled\"."
320#else
f6474de9
TT
321"Source highlighting may be disabled in this installation of gdb, because\n\
322it was not linked against GNU Source Highlight. However, it might still be\n\
323available if the appropriate extension is available at runtime."
d085f989
TT
324#endif
325 ), set_style_enabled, show_style_sources,
326 &style_set_list, &style_show_list);
327
0743fc83 328 file_name_style.add_setshow_commands (no_class, _("\
590042fc 329Filename display styling.\n\
0743fc83
TT
330Configure filename colors and display intensity."),
331 &style_set_list, &style_show_list,
332 false);
d73cff18 333
0743fc83 334 function_name_style.add_setshow_commands (no_class, _("\
590042fc 335Function name display styling.\n\
0743fc83
TT
336Configure function name colors and display intensity"),
337 &style_set_list, &style_show_list,
338 false);
d73cff18 339
0743fc83 340 variable_name_style.add_setshow_commands (no_class, _("\
590042fc 341Variable name display styling.\n\
0743fc83
TT
342Configure variable name colors and display intensity"),
343 &style_set_list, &style_show_list,
344 false);
d73cff18 345
0743fc83 346 address_style.add_setshow_commands (no_class, _("\
590042fc 347Address display styling.\n\
0743fc83
TT
348Configure address colors and display intensity"),
349 &style_set_list, &style_show_list,
350 false);
9303eb2f 351
0743fc83 352 title_style.add_setshow_commands (no_class, _("\
590042fc 353Title display styling.\n\
9303eb2f
PW
354Configure title colors and display intensity\n\
355Some commands (such as \"apropos -v REGEXP\") use the title style to improve\n\
0743fc83
TT
356readability."),
357 &style_set_list, &style_show_list,
358 false);
9303eb2f 359
0743fc83 360 highlight_style.add_setshow_commands (no_class, _("\
590042fc 361Highlight display styling.\n\
9303eb2f
PW
362Configure highlight colors and display intensity\n\
363Some commands use the highlight style to draw the attention to a part\n\
0743fc83
TT
364of their output."),
365 &style_set_list, &style_show_list,
366 false);
7f6aba03 367
0743fc83 368 metadata_style.add_setshow_commands (no_class, _("\
4d825eab 369Metadata display styling.\n\
7f6aba03
TT
370Configure metadata colors and display intensity\n\
371The \"metadata\" style is used when GDB displays information about\n\
0743fc83
TT
372your data, for example \"<unavailable>\""),
373 &style_set_list, &style_show_list,
374 false);
a2a7af0c 375
0743fc83 376 tui_border_style.add_setshow_commands (no_class, _("\
a2a7af0c
TT
377TUI border display styling.\n\
378Configure TUI border colors\n\
379The \"tui-border\" style is used when GDB displays the border of a\n\
0743fc83
TT
380TUI window that does not have the focus."),
381 &style_set_list, &style_show_list,
382 true);
a2a7af0c 383
0743fc83 384 tui_active_border_style.add_setshow_commands (no_class, _("\
a2a7af0c
TT
385TUI active border display styling.\n\
386Configure TUI active border colors\n\
387The \"tui-active-border\" style is used when GDB displays the border of a\n\
0743fc83
TT
388TUI window that does have the focus."),
389 &style_set_list,
390 &style_show_list,
391 true);
9d2d8a16
AB
392
393 version_style.add_setshow_commands (no_class, _("\
394Version string display styling.\n\
395Configure colors used to display the GDB version string."),
396 &style_set_list, &style_show_list,
397 false);
cbe56571 398}
This page took 0.219223 seconds and 4 git commands to generate.