]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/cli/cli-style.c
gdb: add context getter/setter to cmd_list_element
[thirdparty/binutils-gdb.git] / gdb / cli / cli-style.c
1 /* CLI colorizing
2
3 Copyright (C) 2018-2021 Free Software Foundation, Inc.
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-decode.h"
23 #include "cli/cli-setshow.h"
24 #include "cli/cli-style.h"
25 #include "source-cache.h"
26 #include "observable.h"
27
28 /* True if styling is enabled. */
29
30 #if defined (__MSDOS__)
31 bool cli_styling = false;
32 #else
33 bool cli_styling = true;
34 #endif
35
36 /* True if source styling is enabled. Note that this is only
37 consulted when cli_styling is true. */
38
39 bool source_styling = true;
40
41 /* Name of colors; must correspond to ui_file_style::basic_color. */
42 static 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. */
57 static const char * const cli_intensities[] = {
58 "normal",
59 "bold",
60 "dim",
61 nullptr
62 };
63
64 /* See cli-style.h. */
65
66 cli_style_option file_name_style ("filename", ui_file_style::GREEN);
67
68 /* See cli-style.h. */
69
70 cli_style_option function_name_style ("function", ui_file_style::YELLOW);
71
72 /* See cli-style.h. */
73
74 cli_style_option variable_name_style ("variable", ui_file_style::CYAN);
75
76 /* See cli-style.h. */
77
78 cli_style_option address_style ("address", ui_file_style::BLUE);
79
80 /* See cli-style.h. */
81
82 cli_style_option highlight_style ("highlight", ui_file_style::RED);
83
84 /* See cli-style.h. */
85
86 cli_style_option title_style ("title", ui_file_style::BOLD);
87
88 /* See cli-style.h. */
89
90 cli_style_option tui_border_style ("tui-border", ui_file_style::CYAN);
91
92 /* See cli-style.h. */
93
94 cli_style_option tui_active_border_style ("tui-active-border",
95 ui_file_style::CYAN);
96
97 /* See cli-style.h. */
98
99 cli_style_option metadata_style ("metadata", ui_file_style::DIM);
100
101 /* See cli-style.h. */
102
103 cli_style_option version_style ("version", ui_file_style::MAGENTA,
104 ui_file_style::BOLD);
105
106 /* See cli-style.h. */
107
108 cli_style_option::cli_style_option (const char *name,
109 ui_file_style::basic_color fg,
110 ui_file_style::intensity intensity)
111 : changed (name),
112 m_name (name),
113 m_foreground (cli_colors[fg - ui_file_style::NONE]),
114 m_background (cli_colors[0]),
115 m_intensity (cli_intensities[intensity])
116 {
117 }
118
119 /* See cli-style.h. */
120
121 cli_style_option::cli_style_option (const char *name,
122 ui_file_style::intensity i)
123 : changed (name),
124 m_name (name),
125 m_foreground (cli_colors[0]),
126 m_background (cli_colors[0]),
127 m_intensity (cli_intensities[i])
128 {
129 }
130
131 /* Return the color number corresponding to COLOR. */
132
133 static int
134 color_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
146 ui_file_style
147 cli_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
165 /* See cli-style.h. */
166
167 void
168 cli_style_option::do_set_value (const char *ignore, int from_tty,
169 struct cmd_list_element *cmd)
170 {
171 cli_style_option *cso = (cli_style_option *) cmd->context ();
172 cso->changed.notify ();
173 }
174
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
179 static void
180 do_show (const char *what, struct ui_file *file,
181 struct cmd_list_element *cmd,
182 const char *value)
183 {
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);
188 }
189
190 /* See cli-style.h. */
191
192 void
193 cli_style_option::do_show_foreground (struct ui_file *file, int from_tty,
194 struct cmd_list_element *cmd,
195 const char *value)
196 {
197 do_show (_("foreground color"), file, cmd, value);
198 }
199
200 /* See cli-style.h. */
201
202 void
203 cli_style_option::do_show_background (struct ui_file *file, int from_tty,
204 struct cmd_list_element *cmd,
205 const char *value)
206 {
207 do_show (_("background color"), file, cmd, value);
208 }
209
210 /* See cli-style.h. */
211
212 void
213 cli_style_option::do_show_intensity (struct ui_file *file, int from_tty,
214 struct cmd_list_element *cmd,
215 const char *value)
216 {
217 do_show (_("display intensity"), file, cmd, value);
218 }
219
220 /* See cli-style.h. */
221
222 void
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,
227 bool skip_intensity)
228 {
229 add_basic_prefix_cmd (m_name, no_class, prefix_doc, &m_set_list,
230 0, set_list);
231 add_show_prefix_cmd (m_name, no_class, prefix_doc, &m_show_list,
232 0, show_list);
233
234 add_setshow_enum_cmd ("foreground", theclass, cli_colors,
235 &m_foreground,
236 _("Set the foreground color for this property."),
237 _("Show the foreground color for this property."),
238 nullptr,
239 do_set_value,
240 do_show_foreground,
241 &m_set_list, &m_show_list, (void *) this);
242 add_setshow_enum_cmd ("background", theclass, cli_colors,
243 &m_background,
244 _("Set the background color for this property."),
245 _("Show the background color for this property."),
246 nullptr,
247 do_set_value,
248 do_show_background,
249 &m_set_list, &m_show_list, (void *) this);
250 if (!skip_intensity)
251 add_setshow_enum_cmd ("intensity", theclass, cli_intensities,
252 &m_intensity,
253 _("Set the display intensity for this property."),
254 _("Show the display intensity for this property."),
255 nullptr,
256 do_set_value,
257 do_show_intensity,
258 &m_set_list, &m_show_list, (void *) this);
259 }
260
261 static cmd_list_element *style_set_list;
262 static cmd_list_element *style_show_list;
263
264 static void
265 set_style_enabled (const char *args, int from_tty, struct cmd_list_element *c)
266 {
267 g_source_cache.clear ();
268 gdb::observers::source_styling_changed.notify ();
269 }
270
271 static void
272 show_style_enabled (struct ui_file *file, int from_tty,
273 struct cmd_list_element *c, const char *value)
274 {
275 if (cli_styling)
276 fprintf_filtered (file, _("CLI output styling is enabled.\n"));
277 else
278 fprintf_filtered (file, _("CLI output styling is disabled.\n"));
279 }
280
281 static void
282 show_style_sources (struct ui_file *file, int from_tty,
283 struct cmd_list_element *c, const char *value)
284 {
285 if (source_styling)
286 fprintf_filtered (file, _("Source code styling is enabled.\n"));
287 else
288 fprintf_filtered (file, _("Source code styling is disabled.\n"));
289 }
290
291 void _initialize_cli_style ();
292 void
293 _initialize_cli_style ()
294 {
295 add_basic_prefix_cmd ("style", no_class, _("\
296 Style-specific settings.\n\
297 Configure various style-related variables, such as colors"),
298 &style_set_list, 0, &setlist);
299 add_show_prefix_cmd ("style", no_class, _("\
300 Style-specific settings.\n\
301 Configure various style-related variables, such as colors"),
302 &style_show_list, 0, &showlist);
303
304 add_setshow_boolean_cmd ("enabled", no_class, &cli_styling, _("\
305 Set whether CLI styling is enabled."), _("\
306 Show whether CLI is enabled."), _("\
307 If enabled, output to the terminal is styled."),
308 set_style_enabled, show_style_enabled,
309 &style_set_list, &style_show_list);
310
311 add_setshow_boolean_cmd ("sources", no_class, &source_styling, _("\
312 Set whether source code styling is enabled."), _("\
313 Show whether source code styling is enabled."), _("\
314 If enabled, source code is styled.\n"
315 #ifdef HAVE_SOURCE_HIGHLIGHT
316 "Note that source styling only works if styling in general is enabled,\n\
317 see \"show style enabled\"."
318 #else
319 "Source highlighting may be disabled in this installation of gdb, because\n\
320 it was not linked against GNU Source Highlight. However, it might still be\n\
321 available if the appropriate extension is available at runtime."
322 #endif
323 ), set_style_enabled, show_style_sources,
324 &style_set_list, &style_show_list);
325
326 file_name_style.add_setshow_commands (no_class, _("\
327 Filename display styling.\n\
328 Configure filename colors and display intensity."),
329 &style_set_list, &style_show_list,
330 false);
331
332 function_name_style.add_setshow_commands (no_class, _("\
333 Function name display styling.\n\
334 Configure function name colors and display intensity"),
335 &style_set_list, &style_show_list,
336 false);
337
338 variable_name_style.add_setshow_commands (no_class, _("\
339 Variable name display styling.\n\
340 Configure variable name colors and display intensity"),
341 &style_set_list, &style_show_list,
342 false);
343
344 address_style.add_setshow_commands (no_class, _("\
345 Address display styling.\n\
346 Configure address colors and display intensity"),
347 &style_set_list, &style_show_list,
348 false);
349
350 title_style.add_setshow_commands (no_class, _("\
351 Title display styling.\n\
352 Configure title colors and display intensity\n\
353 Some commands (such as \"apropos -v REGEXP\") use the title style to improve\n\
354 readability."),
355 &style_set_list, &style_show_list,
356 false);
357
358 highlight_style.add_setshow_commands (no_class, _("\
359 Highlight display styling.\n\
360 Configure highlight colors and display intensity\n\
361 Some commands use the highlight style to draw the attention to a part\n\
362 of their output."),
363 &style_set_list, &style_show_list,
364 false);
365
366 metadata_style.add_setshow_commands (no_class, _("\
367 Metadata display styling.\n\
368 Configure metadata colors and display intensity\n\
369 The \"metadata\" style is used when GDB displays information about\n\
370 your data, for example \"<unavailable>\""),
371 &style_set_list, &style_show_list,
372 false);
373
374 tui_border_style.add_setshow_commands (no_class, _("\
375 TUI border display styling.\n\
376 Configure TUI border colors\n\
377 The \"tui-border\" style is used when GDB displays the border of a\n\
378 TUI window that does not have the focus."),
379 &style_set_list, &style_show_list,
380 true);
381
382 tui_active_border_style.add_setshow_commands (no_class, _("\
383 TUI active border display styling.\n\
384 Configure TUI active border colors\n\
385 The \"tui-active-border\" style is used when GDB displays the border of a\n\
386 TUI window that does have the focus."),
387 &style_set_list,
388 &style_show_list,
389 true);
390
391 version_style.add_setshow_commands (no_class, _("\
392 Version string display styling.\n\
393 Configure colors used to display the GDB version string."),
394 &style_set_list, &style_show_list,
395 false);
396 }