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