boolean/auto-boolean commands, make "o" ambiguous
[deliverable/binutils-gdb.git] / gdb / cli / cli-style.c
CommitLineData
cbe56571
TT
1/* CLI colorizing
2
42a4f53d 3 Copyright (C) 2018-2019 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"
22#include "cli/cli-style.h"
62f29fda 23#include "source-cache.h"
6f11e682 24#include "observable.h"
cbe56571
TT
25
26/* True if styling is enabled. */
27
e4adb939 28#if defined (__MSDOS__) || defined (__CYGWIN__)
cbe56571
TT
29int cli_styling = 0;
30#else
31int cli_styling = 1;
32#endif
33
d085f989
TT
34/* True if source styling is enabled. Note that this is only
35 consulted when cli_styling is true. */
36
37int source_styling = 1;
38
cbe56571
TT
39/* Name of colors; must correspond to ui_file_style::basic_color. */
40static const char * const cli_colors[] = {
41 "none",
42 "black",
43 "red",
44 "green",
45 "yellow",
46 "blue",
47 "magenta",
48 "cyan",
49 "white",
50 nullptr
51};
52
53/* Names of intensities; must correspond to
54 ui_file_style::intensity. */
55static const char * const cli_intensities[] = {
56 "normal",
57 "bold",
58 "dim",
59 nullptr
60};
61
62/* See cli-style.h. */
63
9303eb2f 64cli_style_option file_name_style ("filename", ui_file_style::GREEN);
cbe56571
TT
65
66/* See cli-style.h. */
67
9303eb2f 68cli_style_option function_name_style ("function", ui_file_style::YELLOW);
cbe56571
TT
69
70/* See cli-style.h. */
71
9303eb2f 72cli_style_option variable_name_style ("variable", ui_file_style::CYAN);
80ae2043
TT
73
74/* See cli-style.h. */
75
9303eb2f 76cli_style_option address_style ("address", ui_file_style::BLUE);
35fb8261
TT
77
78/* See cli-style.h. */
79
9303eb2f
PW
80cli_style_option highlight_style ("highlight", ui_file_style::RED);
81
82/* See cli-style.h. */
83
84cli_style_option title_style ("title", ui_file_style::BOLD);
85
86/* See cli-style.h. */
87
88cli_style_option::cli_style_option (const char *name,
89 ui_file_style::basic_color fg)
90 : m_name (name),
91 m_foreground (cli_colors[fg - ui_file_style::NONE]),
cbe56571
TT
92 m_background (cli_colors[0]),
93 m_intensity (cli_intensities[ui_file_style::NORMAL])
94{
95}
96
9303eb2f
PW
97/* See cli-style.h. */
98
99cli_style_option::cli_style_option (const char *name,
100 ui_file_style::intensity i)
101 : m_name (name),
102 m_foreground (cli_colors[0]),
103 m_background (cli_colors[0]),
104 m_intensity (cli_intensities[i])
105{
106}
107
cbe56571
TT
108/* Return the color number corresponding to COLOR. */
109
110static int
111color_number (const char *color)
112{
113 for (int i = 0; i < ARRAY_SIZE (cli_colors); ++i)
114 {
115 if (color == cli_colors[i])
116 return i - 1;
117 }
118 gdb_assert_not_reached ("color not found");
119}
120
121/* See cli-style.h. */
122
123ui_file_style
124cli_style_option::style () const
125{
126 int fg = color_number (m_foreground);
127 int bg = color_number (m_background);
128 ui_file_style::intensity intensity = ui_file_style::NORMAL;
129
130 for (int i = 0; i < ARRAY_SIZE (cli_intensities); ++i)
131 {
132 if (m_intensity == cli_intensities[i])
133 {
134 intensity = (ui_file_style::intensity) i;
135 break;
136 }
137 }
138
139 return ui_file_style (fg, bg, intensity);
140}
141
9303eb2f
PW
142/* Implements the cli_style_option::do_show_* functions.
143 WHAT and VALUE are the property and value to show.
144 The style for which WHAT is shown is retrieved from CMD context. */
145
146static void
147do_show (const char *what, struct ui_file *file,
148 struct cmd_list_element *cmd,
149 const char *value)
150{
151 cli_style_option *cso = (cli_style_option *) get_cmd_context (cmd);
152 fputs_filtered (_("The "), file);
153 fprintf_styled (file, cso->style (), _("\"%s\" style"), cso->name ());
154 fprintf_filtered (file, _(" %s is: %s\n"), what, value);
155}
156
cbe56571
TT
157/* See cli-style.h. */
158
cbe56571
TT
159void
160cli_style_option::do_show_foreground (struct ui_file *file, int from_tty,
161 struct cmd_list_element *cmd,
162 const char *value)
163{
9303eb2f 164 do_show (_("foreground color"), file, cmd, value);
cbe56571
TT
165}
166
167/* See cli-style.h. */
168
169void
170cli_style_option::do_show_background (struct ui_file *file, int from_tty,
171 struct cmd_list_element *cmd,
172 const char *value)
173{
9303eb2f 174 do_show (_("background color"), file, cmd, value);
cbe56571
TT
175}
176
177/* See cli-style.h. */
178
179void
180cli_style_option::do_show_intensity (struct ui_file *file, int from_tty,
181 struct cmd_list_element *cmd,
182 const char *value)
183{
9303eb2f 184 do_show (_("display intensity"), file, cmd, value);
cbe56571
TT
185}
186
187/* See cli-style.h. */
188
189void
9303eb2f 190cli_style_option::add_setshow_commands (enum command_class theclass,
cbe56571 191 const char *prefix_doc,
cbe56571 192 struct cmd_list_element **set_list,
d73cff18
PW
193 void (*do_set) (const char *args,
194 int from_tty),
195 struct cmd_list_element **show_list,
196 void (*do_show) (const char *args,
197 int from_tty))
cbe56571 198{
9303eb2f
PW
199 m_set_prefix = std::string ("set style ") + m_name + " ";
200 m_show_prefix = std::string ("show style ") + m_name + " ";
cbe56571 201
9303eb2f 202 add_prefix_cmd (m_name, no_class, do_set, prefix_doc, &m_set_list,
ecad3b21 203 m_set_prefix.c_str (), 0, set_list);
9303eb2f 204 add_prefix_cmd (m_name, no_class, do_show, prefix_doc, &m_show_list,
ecad3b21 205 m_show_prefix.c_str (), 0, show_list);
cbe56571
TT
206
207 add_setshow_enum_cmd ("foreground", theclass, cli_colors,
208 &m_foreground,
209 _("Set the foreground color for this property"),
210 _("Show the foreground color for this property"),
211 nullptr,
212 nullptr,
213 do_show_foreground,
9303eb2f 214 &m_set_list, &m_show_list, (void *) this);
cbe56571
TT
215 add_setshow_enum_cmd ("background", theclass, cli_colors,
216 &m_background,
217 _("Set the background color for this property"),
218 _("Show the background color for this property"),
219 nullptr,
220 nullptr,
221 do_show_background,
9303eb2f 222 &m_set_list, &m_show_list, (void *) this);
cbe56571
TT
223 add_setshow_enum_cmd ("intensity", theclass, cli_intensities,
224 &m_intensity,
d73cff18
PW
225 _("Set the display intensity for this property"),
226 _("Show the display intensity for this property"),
cbe56571
TT
227 nullptr,
228 nullptr,
229 do_show_intensity,
9303eb2f 230 &m_set_list, &m_show_list, (void *) this);
cbe56571
TT
231}
232
d73cff18
PW
233static cmd_list_element *style_set_list;
234static cmd_list_element *style_show_list;
235
cbe56571
TT
236static void
237set_style (const char *arg, int from_tty)
238{
d73cff18
PW
239 printf_unfiltered (_("\"set style\" must be followed "
240 "by an appropriate subcommand.\n"));
241 help_list (style_set_list, "set style ", all_commands, gdb_stdout);
cbe56571
TT
242}
243
244static void
245show_style (const char *arg, int from_tty)
246{
d73cff18 247 cmd_show_list (style_show_list, from_tty, "");
cbe56571
TT
248}
249
62f29fda
TT
250static void
251set_style_enabled (const char *args, int from_tty, struct cmd_list_element *c)
252{
253 g_source_cache.clear ();
6f11e682 254 gdb::observers::source_styling_changed.notify ();
62f29fda
TT
255}
256
cbe56571
TT
257static void
258show_style_enabled (struct ui_file *file, int from_tty,
259 struct cmd_list_element *c, const char *value)
260{
261 if (cli_styling)
262 fprintf_filtered (file, _("CLI output styling is enabled.\n"));
263 else
264 fprintf_filtered (file, _("CLI output styling is disabled.\n"));
265}
266
d085f989
TT
267static void
268show_style_sources (struct ui_file *file, int from_tty,
269 struct cmd_list_element *c, const char *value)
270{
271 if (source_styling)
272 fprintf_filtered (file, _("Source code styling is enabled.\n"));
273 else
274 fprintf_filtered (file, _("Source code styling is disabled.\n"));
275}
276
9303eb2f
PW
277/* Builds the "set style NAME " prefix. */
278
279static std::string
280set_style_name (const char *name)
281{
282 std::string result ("set style ");
283
284 result += name;
285 result += " ";
286 return result;
287}
288
cbe56571
TT
289void
290_initialize_cli_style ()
291{
cbe56571
TT
292 add_prefix_cmd ("style", no_class, set_style, _("\
293Style-specific settings\n\
294Configure various style-related variables, such as colors"),
295 &style_set_list, "set style ", 0, &setlist);
296 add_prefix_cmd ("style", no_class, show_style, _("\
297Style-specific settings\n\
298Configure various style-related variables, such as colors"),
299 &style_show_list, "show style ", 0, &showlist);
300
301 add_setshow_boolean_cmd ("enabled", no_class, &cli_styling, _("\
302Set whether CLI styling is enabled."), _("\
303Show whether CLI is enabled."), _("\
304If enabled, output to the terminal is styled."),
62f29fda 305 set_style_enabled, show_style_enabled,
cbe56571
TT
306 &style_set_list, &style_show_list);
307
d085f989
TT
308 add_setshow_boolean_cmd ("sources", no_class, &source_styling, _("\
309Set whether source code styling is enabled."), _("\
310Show whether source code styling is enabled."), _("\
311If enabled, source code is styled.\n"
312#ifdef HAVE_SOURCE_HIGHLIGHT
313"Note that source styling only works if styling in general is enabled,\n\
314see \"show style enabled\"."
315#else
316"Source highlighting is disabled in this installation of gdb, because\n\
317it was not linked against GNU Source Highlight."
318#endif
319 ), set_style_enabled, show_style_sources,
320 &style_set_list, &style_show_list);
321
9303eb2f
PW
322#define STYLE_ADD_SETSHOW_COMMANDS(STYLE, PREFIX_DOC) \
323 STYLE.add_setshow_commands (no_class, PREFIX_DOC, \
d73cff18
PW
324 &style_set_list, \
325 [] (const char *args, int from_tty) \
326 { \
327 help_list \
328 (STYLE.set_list (), \
9303eb2f 329 set_style_name (STYLE.name ()).c_str (), \
d73cff18
PW
330 all_commands, \
331 gdb_stdout); \
332 }, \
333 &style_show_list, \
334 [] (const char *args, int from_tty) \
335 { \
336 cmd_show_list \
337 (STYLE.show_list (), \
338 from_tty, \
339 ""); \
340 })
341
9303eb2f 342 STYLE_ADD_SETSHOW_COMMANDS (file_name_style,
d73cff18 343 _("\
cbe56571 344Filename display styling\n\
d73cff18
PW
345Configure filename colors and display intensity."));
346
9303eb2f 347 STYLE_ADD_SETSHOW_COMMANDS (function_name_style,
d73cff18 348 _("\
cbe56571 349Function name display styling\n\
d73cff18
PW
350Configure function name colors and display intensity"));
351
9303eb2f 352 STYLE_ADD_SETSHOW_COMMANDS (variable_name_style,
d73cff18 353 _("\
80ae2043 354Variable name display styling\n\
d73cff18
PW
355Configure variable name colors and display intensity"));
356
9303eb2f 357 STYLE_ADD_SETSHOW_COMMANDS (address_style,
d73cff18 358 _("\
35fb8261 359Address display styling\n\
d73cff18 360Configure address colors and display intensity"));
9303eb2f
PW
361
362 STYLE_ADD_SETSHOW_COMMANDS (title_style,
363 _("\
364Title display styling\n\
365Configure title colors and display intensity\n\
366Some commands (such as \"apropos -v REGEXP\") use the title style to improve\n\
367readability."));
368
369 STYLE_ADD_SETSHOW_COMMANDS (highlight_style,
370 _("\
371Highlight display styling\n\
372Configure highlight colors and display intensity\n\
373Some commands use the highlight style to draw the attention to a part\n\
374of their output."));
cbe56571 375}
This page took 0.071013 seconds and 4 git commands to generate.