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/>. */
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;
33 bool cli_styling = true;
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 /* Name of colors; must correspond to ui_file_style::basic_color. */
42 static const char * const cli_colors[] = {
55 /* Names of intensities; must correspond to
56 ui_file_style::intensity. */
57 static const char * const cli_intensities[] = {
64 /* See cli-style.h. */
66 cli_style_option file_name_style ("filename", ui_file_style::GREEN);
68 /* See cli-style.h. */
70 cli_style_option function_name_style ("function", ui_file_style::YELLOW);
72 /* See cli-style.h. */
74 cli_style_option variable_name_style ("variable", ui_file_style::CYAN);
76 /* See cli-style.h. */
78 cli_style_option address_style ("address", ui_file_style::BLUE);
80 /* See cli-style.h. */
82 cli_style_option highlight_style ("highlight", ui_file_style::RED);
84 /* See cli-style.h. */
86 cli_style_option title_style ("title", ui_file_style::BOLD);
88 /* See cli-style.h. */
90 cli_style_option tui_border_style ("tui-border", ui_file_style::CYAN);
92 /* See cli-style.h. */
94 cli_style_option tui_active_border_style ("tui-active-border",
97 /* See cli-style.h. */
99 cli_style_option metadata_style ("metadata", ui_file_style::DIM);
101 /* See cli-style.h. */
103 cli_style_option version_style ("version", ui_file_style::MAGENTA,
104 ui_file_style::BOLD);
106 /* See cli-style.h. */
108 cli_style_option::cli_style_option (const char *name,
109 ui_file_style::basic_color fg,
110 ui_file_style::intensity intensity)
113 m_foreground (cli_colors[fg - ui_file_style::NONE]),
114 m_background (cli_colors[0]),
115 m_intensity (cli_intensities[intensity])
119 /* See cli-style.h. */
121 cli_style_option::cli_style_option (const char *name,
122 ui_file_style::intensity i)
125 m_foreground (cli_colors[0]),
126 m_background (cli_colors[0]),
127 m_intensity (cli_intensities[i])
131 /* Return the color number corresponding to COLOR. */
134 color_number (const char *color)
136 for (int i = 0; i < ARRAY_SIZE (cli_colors); ++i)
138 if (color == cli_colors[i])
141 gdb_assert_not_reached ("color not found");
144 /* See cli-style.h. */
147 cli_style_option::style () const
149 int fg = color_number (m_foreground);
150 int bg = color_number (m_background);
151 ui_file_style::intensity intensity = ui_file_style::NORMAL;
153 for (int i = 0; i < ARRAY_SIZE (cli_intensities); ++i)
155 if (m_intensity == cli_intensities[i])
157 intensity = (ui_file_style::intensity) i;
162 return ui_file_style (fg, bg, intensity);
165 /* See cli-style.h. */
168 cli_style_option::do_set_value (const char *ignore, int from_tty,
169 struct cmd_list_element *cmd)
171 cli_style_option *cso = (cli_style_option *) cmd->context ();
172 cso->changed.notify ();
175 /* Implements the cli_style_option::do_show_* functions.
176 WHAT and VALUE are the property and value to show.
177 The style for which WHAT is shown is retrieved from CMD context. */
180 do_show (const char *what, struct ui_file *file,
181 struct cmd_list_element *cmd,
184 cli_style_option *cso = (cli_style_option *) cmd->context ();
185 fputs_filtered (_("The "), file);
186 fprintf_styled (file, cso->style (), _("\"%s\" style"), cso->name ());
187 fprintf_filtered (file, _(" %s is: %s\n"), what, value);
190 /* See cli-style.h. */
193 cli_style_option::do_show_foreground (struct ui_file *file, int from_tty,
194 struct cmd_list_element *cmd,
197 do_show (_("foreground color"), file, cmd, value);
200 /* See cli-style.h. */
203 cli_style_option::do_show_background (struct ui_file *file, int from_tty,
204 struct cmd_list_element *cmd,
207 do_show (_("background color"), file, cmd, value);
210 /* See cli-style.h. */
213 cli_style_option::do_show_intensity (struct ui_file *file, int from_tty,
214 struct cmd_list_element *cmd,
217 do_show (_("display intensity"), file, cmd, value);
220 /* See cli-style.h. */
223 cli_style_option::add_setshow_commands (enum command_class theclass,
224 const char *prefix_doc,
225 struct cmd_list_element **set_list,
226 struct cmd_list_element **show_list,
229 add_setshow_prefix_cmd (m_name, theclass, prefix_doc, prefix_doc,
230 &m_set_list, &m_show_list, set_list, show_list);
232 set_show_commands commands;
234 commands = add_setshow_enum_cmd
235 ("foreground", theclass, cli_colors,
237 _("Set the foreground color for this property."),
238 _("Show the foreground color for this property."),
242 &m_set_list, &m_show_list);
243 commands.set->set_context (this);
244 commands.show->set_context (this);
246 commands = add_setshow_enum_cmd
247 ("background", theclass, cli_colors,
249 _("Set the background color for this property."),
250 _("Show the background color for this property."),
254 &m_set_list, &m_show_list);
255 commands.set->set_context (this);
256 commands.show->set_context (this);
260 commands = add_setshow_enum_cmd
261 ("intensity", theclass, cli_intensities,
263 _("Set the display intensity for this property."),
264 _("Show the display intensity for this property."),
268 &m_set_list, &m_show_list);
269 commands.set->set_context (this);
270 commands.show->set_context (this);
274 static cmd_list_element *style_set_list;
275 static cmd_list_element *style_show_list;
278 set_style_enabled (const char *args, int from_tty, struct cmd_list_element *c)
280 g_source_cache.clear ();
281 gdb::observers::styling_changed.notify ();
285 show_style_enabled (struct ui_file *file, int from_tty,
286 struct cmd_list_element *c, const char *value)
289 fprintf_filtered (file, _("CLI output styling is enabled.\n"));
291 fprintf_filtered (file, _("CLI output styling is disabled.\n"));
295 show_style_sources (struct ui_file *file, int from_tty,
296 struct cmd_list_element *c, const char *value)
299 fprintf_filtered (file, _("Source code styling is enabled.\n"));
301 fprintf_filtered (file, _("Source code styling is disabled.\n"));
304 void _initialize_cli_style ();
306 _initialize_cli_style ()
308 add_setshow_prefix_cmd ("style", no_class,
310 Style-specific settings.\n\
311 Configure various style-related variables, such as colors"),
313 Style-specific settings.\n\
314 Configure various style-related variables, such as colors"),
315 &style_set_list, &style_show_list,
316 &setlist, &showlist);
318 add_setshow_boolean_cmd ("enabled", no_class, &cli_styling, _("\
319 Set whether CLI styling is enabled."), _("\
320 Show whether CLI is enabled."), _("\
321 If enabled, output to the terminal is styled."),
322 set_style_enabled, show_style_enabled,
323 &style_set_list, &style_show_list);
325 add_setshow_boolean_cmd ("sources", no_class, &source_styling, _("\
326 Set whether source code styling is enabled."), _("\
327 Show whether source code styling is enabled."), _("\
328 If enabled, source code is styled.\n"
329 #ifdef HAVE_SOURCE_HIGHLIGHT
330 "Note that source styling only works if styling in general is enabled,\n\
331 see \"show style enabled\"."
333 "Source highlighting may be disabled in this installation of gdb, because\n\
334 it was not linked against GNU Source Highlight. However, it might still be\n\
335 available if the appropriate extension is available at runtime."
337 ), set_style_enabled, show_style_sources,
338 &style_set_list, &style_show_list);
340 file_name_style.add_setshow_commands (no_class, _("\
341 Filename display styling.\n\
342 Configure filename colors and display intensity."),
343 &style_set_list, &style_show_list,
346 function_name_style.add_setshow_commands (no_class, _("\
347 Function name display styling.\n\
348 Configure function name colors and display intensity"),
349 &style_set_list, &style_show_list,
352 variable_name_style.add_setshow_commands (no_class, _("\
353 Variable name display styling.\n\
354 Configure variable name colors and display intensity"),
355 &style_set_list, &style_show_list,
358 address_style.add_setshow_commands (no_class, _("\
359 Address display styling.\n\
360 Configure address colors and display intensity"),
361 &style_set_list, &style_show_list,
364 title_style.add_setshow_commands (no_class, _("\
365 Title display styling.\n\
366 Configure title colors and display intensity\n\
367 Some commands (such as \"apropos -v REGEXP\") use the title style to improve\n\
369 &style_set_list, &style_show_list,
372 highlight_style.add_setshow_commands (no_class, _("\
373 Highlight display styling.\n\
374 Configure highlight colors and display intensity\n\
375 Some commands use the highlight style to draw the attention to a part\n\
377 &style_set_list, &style_show_list,
380 metadata_style.add_setshow_commands (no_class, _("\
381 Metadata display styling.\n\
382 Configure metadata colors and display intensity\n\
383 The \"metadata\" style is used when GDB displays information about\n\
384 your data, for example \"<unavailable>\""),
385 &style_set_list, &style_show_list,
388 tui_border_style.add_setshow_commands (no_class, _("\
389 TUI border display styling.\n\
390 Configure TUI border colors\n\
391 The \"tui-border\" style is used when GDB displays the border of a\n\
392 TUI window that does not have the focus."),
393 &style_set_list, &style_show_list,
396 tui_active_border_style.add_setshow_commands (no_class, _("\
397 TUI active border display styling.\n\
398 Configure TUI active border colors\n\
399 The \"tui-active-border\" style is used when GDB displays the border of a\n\
400 TUI window that does have the focus."),
405 version_style.add_setshow_commands (no_class, _("\
406 Version string display styling.\n\
407 Configure colors used to display the GDB version string."),
408 &style_set_list, &style_show_list,