gdb/testsuite: fix gdb.trace/signal.exp on x86
[binutils-gdb/blckswan.git] / gdb / cli / cli-style.c
blob3fd85f4aa863088d07ae893f000f942da6fbb80a
1 /* CLI colorizing
3 Copyright (C) 2018-2022 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #include "defs.h"
21 #include "cli/cli-cmds.h"
22 #include "cli/cli-decode.h"
23 #include "cli/cli-setshow.h"
24 #include "cli/cli-style.h"
25 #include "source-cache.h"
26 #include "observable.h"
28 /* True if styling is enabled. */
30 #if defined (__MSDOS__)
31 bool cli_styling = false;
32 #else
33 bool cli_styling = true;
34 #endif
36 /* True if source styling is enabled. Note that this is only
37 consulted when cli_styling is true. */
39 bool source_styling = true;
41 /* True if disassembler styling is enabled. Note that this is only
42 consulted when cli_styling is true. */
44 bool disassembler_styling = true;
46 /* Name of colors; must correspond to ui_file_style::basic_color. */
47 static const char * const cli_colors[] = {
48 "none",
49 "black",
50 "red",
51 "green",
52 "yellow",
53 "blue",
54 "magenta",
55 "cyan",
56 "white",
57 nullptr
60 /* Names of intensities; must correspond to
61 ui_file_style::intensity. */
62 static const char * const cli_intensities[] = {
63 "normal",
64 "bold",
65 "dim",
66 nullptr
69 /* See cli-style.h. */
71 cli_style_option file_name_style ("filename", ui_file_style::GREEN);
73 /* See cli-style.h. */
75 cli_style_option function_name_style ("function", ui_file_style::YELLOW);
77 /* See cli-style.h. */
79 cli_style_option variable_name_style ("variable", ui_file_style::CYAN);
81 /* See cli-style.h. */
83 cli_style_option address_style ("address", ui_file_style::BLUE);
85 /* See cli-style.h. */
87 cli_style_option highlight_style ("highlight", ui_file_style::RED);
89 /* See cli-style.h. */
91 cli_style_option title_style ("title", ui_file_style::BOLD);
93 /* See cli-style.h. */
95 cli_style_option tui_border_style ("tui-border", ui_file_style::CYAN);
97 /* See cli-style.h. */
99 cli_style_option tui_active_border_style ("tui-active-border",
100 ui_file_style::CYAN);
102 /* See cli-style.h. */
104 cli_style_option metadata_style ("metadata", ui_file_style::DIM);
106 /* See cli-style.h. */
108 cli_style_option version_style ("version", ui_file_style::MAGENTA,
109 ui_file_style::BOLD);
111 /* See cli-style.h. */
113 cli_style_option::cli_style_option (const char *name,
114 ui_file_style::basic_color fg,
115 ui_file_style::intensity intensity)
116 : changed (name),
117 m_name (name),
118 m_foreground (cli_colors[fg - ui_file_style::NONE]),
119 m_background (cli_colors[0]),
120 m_intensity (cli_intensities[intensity])
124 /* See cli-style.h. */
126 cli_style_option::cli_style_option (const char *name,
127 ui_file_style::intensity i)
128 : changed (name),
129 m_name (name),
130 m_foreground (cli_colors[0]),
131 m_background (cli_colors[0]),
132 m_intensity (cli_intensities[i])
136 /* Return the color number corresponding to COLOR. */
138 static int
139 color_number (const char *color)
141 for (int i = 0; i < ARRAY_SIZE (cli_colors); ++i)
143 if (color == cli_colors[i])
144 return i - 1;
146 gdb_assert_not_reached ("color not found");
149 /* See cli-style.h. */
151 ui_file_style
152 cli_style_option::style () const
154 int fg = color_number (m_foreground);
155 int bg = color_number (m_background);
156 ui_file_style::intensity intensity = ui_file_style::NORMAL;
158 for (int i = 0; i < ARRAY_SIZE (cli_intensities); ++i)
160 if (m_intensity == cli_intensities[i])
162 intensity = (ui_file_style::intensity) i;
163 break;
167 return ui_file_style (fg, bg, intensity);
170 /* See cli-style.h. */
172 void
173 cli_style_option::do_set_value (const char *ignore, int from_tty,
174 struct cmd_list_element *cmd)
176 cli_style_option *cso = (cli_style_option *) cmd->context ();
177 cso->changed.notify ();
180 /* Implements the cli_style_option::do_show_* functions.
181 WHAT and VALUE are the property and value to show.
182 The style for which WHAT is shown is retrieved from CMD context. */
184 static void
185 do_show (const char *what, struct ui_file *file,
186 struct cmd_list_element *cmd,
187 const char *value)
189 cli_style_option *cso = (cli_style_option *) cmd->context ();
190 gdb_puts (_("The "), file);
191 fprintf_styled (file, cso->style (), _("\"%s\" style"), cso->name ());
192 gdb_printf (file, _(" %s is: %s\n"), what, value);
195 /* See cli-style.h. */
197 void
198 cli_style_option::do_show_foreground (struct ui_file *file, int from_tty,
199 struct cmd_list_element *cmd,
200 const char *value)
202 do_show (_("foreground color"), file, cmd, value);
205 /* See cli-style.h. */
207 void
208 cli_style_option::do_show_background (struct ui_file *file, int from_tty,
209 struct cmd_list_element *cmd,
210 const char *value)
212 do_show (_("background color"), file, cmd, value);
215 /* See cli-style.h. */
217 void
218 cli_style_option::do_show_intensity (struct ui_file *file, int from_tty,
219 struct cmd_list_element *cmd,
220 const char *value)
222 do_show (_("display intensity"), file, cmd, value);
225 /* See cli-style.h. */
227 void
228 cli_style_option::add_setshow_commands (enum command_class theclass,
229 const char *prefix_doc,
230 struct cmd_list_element **set_list,
231 struct cmd_list_element **show_list,
232 bool skip_intensity)
234 add_setshow_prefix_cmd (m_name, theclass, prefix_doc, prefix_doc,
235 &m_set_list, &m_show_list, set_list, show_list);
237 set_show_commands commands;
239 commands = add_setshow_enum_cmd
240 ("foreground", theclass, cli_colors,
241 &m_foreground,
242 _("Set the foreground color for this property."),
243 _("Show the foreground color for this property."),
244 nullptr,
245 do_set_value,
246 do_show_foreground,
247 &m_set_list, &m_show_list);
248 commands.set->set_context (this);
249 commands.show->set_context (this);
251 commands = add_setshow_enum_cmd
252 ("background", theclass, cli_colors,
253 &m_background,
254 _("Set the background color for this property."),
255 _("Show the background color for this property."),
256 nullptr,
257 do_set_value,
258 do_show_background,
259 &m_set_list, &m_show_list);
260 commands.set->set_context (this);
261 commands.show->set_context (this);
263 if (!skip_intensity)
265 commands = add_setshow_enum_cmd
266 ("intensity", theclass, cli_intensities,
267 &m_intensity,
268 _("Set the display intensity for this property."),
269 _("Show the display intensity for this property."),
270 nullptr,
271 do_set_value,
272 do_show_intensity,
273 &m_set_list, &m_show_list);
274 commands.set->set_context (this);
275 commands.show->set_context (this);
279 static cmd_list_element *style_set_list;
280 static cmd_list_element *style_show_list;
282 /* The command list for 'set style disassembler'. */
284 static cmd_list_element *style_disasm_set_list;
286 /* The command list for 'show style disassembler'. */
288 static cmd_list_element *style_disasm_show_list;
290 static void
291 set_style_enabled (const char *args, int from_tty, struct cmd_list_element *c)
293 g_source_cache.clear ();
294 gdb::observers::styling_changed.notify ();
297 static void
298 show_style_enabled (struct ui_file *file, int from_tty,
299 struct cmd_list_element *c, const char *value)
301 if (cli_styling)
302 gdb_printf (file, _("CLI output styling is enabled.\n"));
303 else
304 gdb_printf (file, _("CLI output styling is disabled.\n"));
307 static void
308 show_style_sources (struct ui_file *file, int from_tty,
309 struct cmd_list_element *c, const char *value)
311 if (source_styling)
312 gdb_printf (file, _("Source code styling is enabled.\n"));
313 else
314 gdb_printf (file, _("Source code styling is disabled.\n"));
317 /* Implement 'show style disassembler'. */
319 static void
320 show_style_disassembler (struct ui_file *file, int from_tty,
321 struct cmd_list_element *c, const char *value)
323 if (disassembler_styling)
324 gdb_printf (file, _("Disassembler output styling is enabled.\n"));
325 else
326 gdb_printf (file, _("Disassembler output styling is disabled.\n"));
329 void _initialize_cli_style ();
330 void
331 _initialize_cli_style ()
333 add_setshow_prefix_cmd ("style", no_class,
334 _("\
335 Style-specific settings.\n\
336 Configure various style-related variables, such as colors"),
337 _("\
338 Style-specific settings.\n\
339 Configure various style-related variables, such as colors"),
340 &style_set_list, &style_show_list,
341 &setlist, &showlist);
343 add_setshow_boolean_cmd ("enabled", no_class, &cli_styling, _("\
344 Set whether CLI styling is enabled."), _("\
345 Show whether CLI is enabled."), _("\
346 If enabled, output to the terminal is styled."),
347 set_style_enabled, show_style_enabled,
348 &style_set_list, &style_show_list);
350 add_setshow_boolean_cmd ("sources", no_class, &source_styling, _("\
351 Set whether source code styling is enabled."), _("\
352 Show whether source code styling is enabled."), _("\
353 If enabled, source code is styled.\n"
354 #ifdef HAVE_SOURCE_HIGHLIGHT
355 "Note that source styling only works if styling in general is enabled,\n\
356 see \"show style enabled\"."
357 #else
358 "Source highlighting may be disabled in this installation of gdb, because\n\
359 it was not linked against GNU Source Highlight. However, it might still be\n\
360 available if the appropriate extension is available at runtime."
361 #endif
362 ), set_style_enabled, show_style_sources,
363 &style_set_list, &style_show_list);
365 add_setshow_prefix_cmd ("disassembler", no_class,
366 _("\
367 Style-specific settings for the disassembler.\n\
368 Configure various disassembler style-related variables."),
369 _("\
370 Style-specific settings for the disassembler.\n\
371 Configure various disassembler style-related variables."),
372 &style_disasm_set_list, &style_disasm_show_list,
373 &style_set_list, &style_show_list);
375 add_setshow_boolean_cmd ("enabled", no_class, &disassembler_styling, _("\
376 Set whether disassembler output styling is enabled."), _("\
377 Show whether disassembler output styling is enabled."), _("\
378 If enabled, disassembler output is styled. Disassembler highlighting\n\
379 requires the Python Pygments library, if this library is not available\n\
380 then disassembler highlighting will not be possible."
381 ), set_style_enabled, show_style_disassembler,
382 &style_disasm_set_list, &style_disasm_show_list);
384 file_name_style.add_setshow_commands (no_class, _("\
385 Filename display styling.\n\
386 Configure filename colors and display intensity."),
387 &style_set_list, &style_show_list,
388 false);
390 function_name_style.add_setshow_commands (no_class, _("\
391 Function name display styling.\n\
392 Configure function name colors and display intensity"),
393 &style_set_list, &style_show_list,
394 false);
396 variable_name_style.add_setshow_commands (no_class, _("\
397 Variable name display styling.\n\
398 Configure variable name colors and display intensity"),
399 &style_set_list, &style_show_list,
400 false);
402 address_style.add_setshow_commands (no_class, _("\
403 Address display styling.\n\
404 Configure address colors and display intensity"),
405 &style_set_list, &style_show_list,
406 false);
408 title_style.add_setshow_commands (no_class, _("\
409 Title display styling.\n\
410 Configure title colors and display intensity\n\
411 Some commands (such as \"apropos -v REGEXP\") use the title style to improve\n\
412 readability."),
413 &style_set_list, &style_show_list,
414 false);
416 highlight_style.add_setshow_commands (no_class, _("\
417 Highlight display styling.\n\
418 Configure highlight colors and display intensity\n\
419 Some commands use the highlight style to draw the attention to a part\n\
420 of their output."),
421 &style_set_list, &style_show_list,
422 false);
424 metadata_style.add_setshow_commands (no_class, _("\
425 Metadata display styling.\n\
426 Configure metadata colors and display intensity\n\
427 The \"metadata\" style is used when GDB displays information about\n\
428 your data, for example \"<unavailable>\""),
429 &style_set_list, &style_show_list,
430 false);
432 tui_border_style.add_setshow_commands (no_class, _("\
433 TUI border display styling.\n\
434 Configure TUI border colors\n\
435 The \"tui-border\" style is used when GDB displays the border of a\n\
436 TUI window that does not have the focus."),
437 &style_set_list, &style_show_list,
438 true);
440 tui_active_border_style.add_setshow_commands (no_class, _("\
441 TUI active border display styling.\n\
442 Configure TUI active border colors\n\
443 The \"tui-active-border\" style is used when GDB displays the border of a\n\
444 TUI window that does have the focus."),
445 &style_set_list,
446 &style_show_list,
447 true);
449 version_style.add_setshow_commands (no_class, _("\
450 Version string display styling.\n\
451 Configure colors used to display the GDB version string."),
452 &style_set_list, &style_show_list,
453 false);