3 Copyright (C) 2018-2020 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-style.h"
23 #include "source-cache.h"
24 #include "observable.h"
26 /* True if styling is enabled. */
28 #if defined (__MSDOS__)
29 bool cli_styling
= false;
31 bool cli_styling
= true;
34 /* True if source styling is enabled. Note that this is only
35 consulted when cli_styling is true. */
37 bool source_styling
= true;
39 /* Name of colors; must correspond to ui_file_style::basic_color. */
40 static const char * const cli_colors
[] = {
53 /* Names of intensities; must correspond to
54 ui_file_style::intensity. */
55 static const char * const cli_intensities
[] = {
62 /* See cli-style.h. */
64 cli_style_option
file_name_style ("filename", ui_file_style::GREEN
);
66 /* See cli-style.h. */
68 cli_style_option
function_name_style ("function", ui_file_style::YELLOW
);
70 /* See cli-style.h. */
72 cli_style_option
variable_name_style ("variable", ui_file_style::CYAN
);
74 /* See cli-style.h. */
76 cli_style_option
address_style ("address", ui_file_style::BLUE
);
78 /* See cli-style.h. */
80 cli_style_option
highlight_style ("highlight", ui_file_style::RED
);
82 /* See cli-style.h. */
84 cli_style_option
title_style ("title", ui_file_style::BOLD
);
86 /* See cli-style.h. */
88 cli_style_option
tui_border_style ("tui-border", ui_file_style::CYAN
);
90 /* See cli-style.h. */
92 cli_style_option
tui_active_border_style ("tui-active-border",
95 /* See cli-style.h. */
97 cli_style_option
metadata_style ("metadata", ui_file_style::DIM
);
99 /* See cli-style.h. */
101 cli_style_option
completion_prefix_style ("completion-prefix",
104 /* See cli-style.h. */
106 cli_style_option
completion_difference_style ("completion-difference",
107 ui_file_style::MAGENTA
);
109 /* See cli-style.h. */
111 cli_style_option
completion_suffix_style ("completion-suffix",
112 ui_file_style::NONE
);
114 /* See cli-style.h. */
116 cli_style_option::cli_style_option (const char *name
,
117 ui_file_style::basic_color fg
)
120 m_foreground (cli_colors
[fg
- ui_file_style::NONE
]),
121 m_background (cli_colors
[0]),
122 m_intensity (cli_intensities
[ui_file_style::NORMAL
])
126 /* See cli-style.h. */
128 cli_style_option::cli_style_option (const char *name
,
129 ui_file_style::intensity i
)
132 m_foreground (cli_colors
[0]),
133 m_background (cli_colors
[0]),
134 m_intensity (cli_intensities
[i
])
138 /* Return the color number corresponding to COLOR. */
141 color_number (const char *color
)
143 for (int i
= 0; i
< ARRAY_SIZE (cli_colors
); ++i
)
145 if (color
== cli_colors
[i
])
148 gdb_assert_not_reached ("color not found");
151 /* See cli-style.h. */
154 cli_style_option::style () const
156 int fg
= color_number (m_foreground
);
157 int bg
= color_number (m_background
);
158 ui_file_style::intensity intensity
= ui_file_style::NORMAL
;
160 for (int i
= 0; i
< ARRAY_SIZE (cli_intensities
); ++i
)
162 if (m_intensity
== cli_intensities
[i
])
164 intensity
= (ui_file_style::intensity
) i
;
169 return ui_file_style (fg
, bg
, intensity
);
172 /* See cli-style.h. */
175 cli_style_option::do_set_value (const char *ignore
, int from_tty
,
176 struct cmd_list_element
*cmd
)
178 cli_style_option
*cso
= (cli_style_option
*) get_cmd_context (cmd
);
179 cso
->changed
.notify ();
182 /* Implements the cli_style_option::do_show_* functions.
183 WHAT and VALUE are the property and value to show.
184 The style for which WHAT is shown is retrieved from CMD context. */
187 do_show (const char *what
, struct ui_file
*file
,
188 struct cmd_list_element
*cmd
,
191 cli_style_option
*cso
= (cli_style_option
*) get_cmd_context (cmd
);
192 fputs_filtered (_("The "), file
);
193 fprintf_styled (file
, cso
->style (), _("\"%s\" style"), cso
->name ());
194 fprintf_filtered (file
, _(" %s is: %s\n"), what
, value
);
197 /* See cli-style.h. */
200 cli_style_option::do_show_foreground (struct ui_file
*file
, int from_tty
,
201 struct cmd_list_element
*cmd
,
204 do_show (_("foreground color"), file
, cmd
, value
);
207 /* See cli-style.h. */
210 cli_style_option::do_show_background (struct ui_file
*file
, int from_tty
,
211 struct cmd_list_element
*cmd
,
214 do_show (_("background color"), file
, cmd
, value
);
217 /* See cli-style.h. */
220 cli_style_option::do_show_intensity (struct ui_file
*file
, int from_tty
,
221 struct cmd_list_element
*cmd
,
224 do_show (_("display intensity"), file
, cmd
, value
);
227 /* See cli-style.h. */
230 cli_style_option::add_setshow_commands (enum command_class theclass
,
231 const char *prefix_doc
,
232 struct cmd_list_element
**set_list
,
233 struct cmd_list_element
**show_list
,
236 m_set_prefix
= std::string ("set style ") + m_name
+ " ";
237 m_show_prefix
= std::string ("show style ") + m_name
+ " ";
239 add_basic_prefix_cmd (m_name
, no_class
, prefix_doc
, &m_set_list
,
240 m_set_prefix
.c_str (), 0, set_list
);
241 add_show_prefix_cmd (m_name
, no_class
, prefix_doc
, &m_show_list
,
242 m_show_prefix
.c_str (), 0, show_list
);
244 add_setshow_enum_cmd ("foreground", theclass
, cli_colors
,
246 _("Set the foreground color for this property."),
247 _("Show the foreground color for this property."),
251 &m_set_list
, &m_show_list
, (void *) this);
252 add_setshow_enum_cmd ("background", theclass
, cli_colors
,
254 _("Set the background color for this property."),
255 _("Show the background color for this property."),
259 &m_set_list
, &m_show_list
, (void *) this);
261 add_setshow_enum_cmd ("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
, (void *) this);
271 static cmd_list_element
*style_set_list
;
272 static cmd_list_element
*style_show_list
;
275 set_style_enabled (const char *args
, int from_tty
, struct cmd_list_element
*c
)
277 g_source_cache
.clear ();
278 gdb::observers::source_styling_changed
.notify ();
282 show_style_enabled (struct ui_file
*file
, int from_tty
,
283 struct cmd_list_element
*c
, const char *value
)
286 fprintf_filtered (file
, _("CLI output styling is enabled.\n"));
288 fprintf_filtered (file
, _("CLI output styling is disabled.\n"));
292 show_style_sources (struct ui_file
*file
, int from_tty
,
293 struct cmd_list_element
*c
, const char *value
)
296 fprintf_filtered (file
, _("Source code styling is enabled.\n"));
298 fprintf_filtered (file
, _("Source code styling is disabled.\n"));
301 void _initialize_cli_style ();
303 _initialize_cli_style ()
305 add_basic_prefix_cmd ("style", no_class
, _("\
306 Style-specific settings.\n\
307 Configure various style-related variables, such as colors"),
308 &style_set_list
, "set style ", 0, &setlist
);
309 add_show_prefix_cmd ("style", no_class
, _("\
310 Style-specific settings.\n\
311 Configure various style-related variables, such as colors"),
312 &style_show_list
, "show style ", 0, &showlist
);
314 add_setshow_boolean_cmd ("enabled", no_class
, &cli_styling
, _("\
315 Set whether CLI styling is enabled."), _("\
316 Show whether CLI is enabled."), _("\
317 If enabled, output to the terminal is styled."),
318 set_style_enabled
, show_style_enabled
,
319 &style_set_list
, &style_show_list
);
321 add_setshow_boolean_cmd ("sources", no_class
, &source_styling
, _("\
322 Set whether source code styling is enabled."), _("\
323 Show whether source code styling is enabled."), _("\
324 If enabled, source code is styled.\n"
325 #ifdef HAVE_SOURCE_HIGHLIGHT
326 "Note that source styling only works if styling in general is enabled,\n\
327 see \"show style enabled\"."
329 "Source highlighting may be disabled in this installation of gdb, because\n\
330 it was not linked against GNU Source Highlight. However, it might still be\n\
331 available if the appropriate extension is available at runtime."
333 ), set_style_enabled
, show_style_sources
,
334 &style_set_list
, &style_show_list
);
336 file_name_style
.add_setshow_commands (no_class
, _("\
337 Filename display styling.\n\
338 Configure filename colors and display intensity."),
339 &style_set_list
, &style_show_list
,
342 function_name_style
.add_setshow_commands (no_class
, _("\
343 Function name display styling.\n\
344 Configure function name colors and display intensity"),
345 &style_set_list
, &style_show_list
,
348 variable_name_style
.add_setshow_commands (no_class
, _("\
349 Variable name display styling.\n\
350 Configure variable name colors and display intensity"),
351 &style_set_list
, &style_show_list
,
354 address_style
.add_setshow_commands (no_class
, _("\
355 Address display styling.\n\
356 Configure address colors and display intensity"),
357 &style_set_list
, &style_show_list
,
360 title_style
.add_setshow_commands (no_class
, _("\
361 Title display styling.\n\
362 Configure title colors and display intensity\n\
363 Some commands (such as \"apropos -v REGEXP\") use the title style to improve\n\
365 &style_set_list
, &style_show_list
,
368 highlight_style
.add_setshow_commands (no_class
, _("\
369 Highlight display styling.\n\
370 Configure highlight colors and display intensity\n\
371 Some commands use the highlight style to draw the attention to a part\n\
373 &style_set_list
, &style_show_list
,
376 metadata_style
.add_setshow_commands (no_class
, _("\
377 Metadata display styling.\n\
378 Configure metadata colors and display intensity\n\
379 The \"metadata\" style is used when GDB displays information about\n\
380 your data, for example \"<unavailable>\""),
381 &style_set_list
, &style_show_list
,
384 completion_prefix_style
.add_setshow_commands (no_class
, _("\
385 Completion prefix display styling.\n\
386 Configure completion prefix colors and display intensity\n\
387 The \"completion-prefix\" style is used when GDB displays the shared\n\
388 prefix common to the possible completions."),
393 completion_difference_style
.add_setshow_commands (no_class
, _("\
394 Completion difference display styling.\n\
395 Configure completion difference colors and display intensity\n\
396 The \"completion-difference\" style is used when GDB displays the\n\
397 character that differs between the possible completions."),
402 completion_suffix_style
.add_setshow_commands (no_class
, _("\
403 Completion suffix display styling.\n\
404 Configure completion suffix colors and display intensity\n\
405 The \"completion-suffix\" style is used when GDB displays the suffix\n\
406 of the possible completions."),
411 tui_border_style
.add_setshow_commands (no_class
, _("\
412 TUI border display styling.\n\
413 Configure TUI border colors\n\
414 The \"tui-border\" style is used when GDB displays the border of a\n\
415 TUI window that does not have the focus."),
416 &style_set_list
, &style_show_list
,
419 tui_active_border_style
.add_setshow_commands (no_class
, _("\
420 TUI active border display styling.\n\
421 Configure TUI active border colors\n\
422 The \"tui-active-border\" style is used when GDB displays the border of a\n\
423 TUI window that does have the focus."),