]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/cli/cli-style.c
Add completion styling
[thirdparty/binutils-gdb.git] / gdb / cli / cli-style.c
1 /* CLI colorizing
2
3 Copyright (C) 2018-2020 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-style.h"
23 #include "source-cache.h"
24 #include "observable.h"
25
26 /* True if styling is enabled. */
27
28 #if defined (__MSDOS__)
29 bool cli_styling = false;
30 #else
31 bool cli_styling = true;
32 #endif
33
34 /* True if source styling is enabled. Note that this is only
35 consulted when cli_styling is true. */
36
37 bool source_styling = true;
38
39 /* Name of colors; must correspond to ui_file_style::basic_color. */
40 static 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. */
55 static const char * const cli_intensities[] = {
56 "normal",
57 "bold",
58 "dim",
59 nullptr
60 };
61
62 /* See cli-style.h. */
63
64 cli_style_option file_name_style ("filename", ui_file_style::GREEN);
65
66 /* See cli-style.h. */
67
68 cli_style_option function_name_style ("function", ui_file_style::YELLOW);
69
70 /* See cli-style.h. */
71
72 cli_style_option variable_name_style ("variable", ui_file_style::CYAN);
73
74 /* See cli-style.h. */
75
76 cli_style_option address_style ("address", ui_file_style::BLUE);
77
78 /* See cli-style.h. */
79
80 cli_style_option highlight_style ("highlight", ui_file_style::RED);
81
82 /* See cli-style.h. */
83
84 cli_style_option title_style ("title", ui_file_style::BOLD);
85
86 /* See cli-style.h. */
87
88 cli_style_option tui_border_style ("tui-border", ui_file_style::CYAN);
89
90 /* See cli-style.h. */
91
92 cli_style_option tui_active_border_style ("tui-active-border",
93 ui_file_style::CYAN);
94
95 /* See cli-style.h. */
96
97 cli_style_option metadata_style ("metadata", ui_file_style::DIM);
98
99 /* See cli-style.h. */
100
101 cli_style_option completion_prefix_style ("completion-prefix",
102 ui_file_style::DIM);
103
104 /* See cli-style.h. */
105
106 cli_style_option completion_difference_style ("completion-difference",
107 ui_file_style::MAGENTA);
108
109 /* See cli-style.h. */
110
111 cli_style_option completion_suffix_style ("completion-suffix",
112 ui_file_style::NONE);
113
114 /* See cli-style.h. */
115
116 cli_style_option::cli_style_option (const char *name,
117 ui_file_style::basic_color fg)
118 : changed (name),
119 m_name (name),
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])
123 {
124 }
125
126 /* See cli-style.h. */
127
128 cli_style_option::cli_style_option (const char *name,
129 ui_file_style::intensity i)
130 : changed (name),
131 m_name (name),
132 m_foreground (cli_colors[0]),
133 m_background (cli_colors[0]),
134 m_intensity (cli_intensities[i])
135 {
136 }
137
138 /* Return the color number corresponding to COLOR. */
139
140 static int
141 color_number (const char *color)
142 {
143 for (int i = 0; i < ARRAY_SIZE (cli_colors); ++i)
144 {
145 if (color == cli_colors[i])
146 return i - 1;
147 }
148 gdb_assert_not_reached ("color not found");
149 }
150
151 /* See cli-style.h. */
152
153 ui_file_style
154 cli_style_option::style () const
155 {
156 int fg = color_number (m_foreground);
157 int bg = color_number (m_background);
158 ui_file_style::intensity intensity = ui_file_style::NORMAL;
159
160 for (int i = 0; i < ARRAY_SIZE (cli_intensities); ++i)
161 {
162 if (m_intensity == cli_intensities[i])
163 {
164 intensity = (ui_file_style::intensity) i;
165 break;
166 }
167 }
168
169 return ui_file_style (fg, bg, intensity);
170 }
171
172 /* See cli-style.h. */
173
174 void
175 cli_style_option::do_set_value (const char *ignore, int from_tty,
176 struct cmd_list_element *cmd)
177 {
178 cli_style_option *cso = (cli_style_option *) get_cmd_context (cmd);
179 cso->changed.notify ();
180 }
181
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. */
185
186 static void
187 do_show (const char *what, struct ui_file *file,
188 struct cmd_list_element *cmd,
189 const char *value)
190 {
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);
195 }
196
197 /* See cli-style.h. */
198
199 void
200 cli_style_option::do_show_foreground (struct ui_file *file, int from_tty,
201 struct cmd_list_element *cmd,
202 const char *value)
203 {
204 do_show (_("foreground color"), file, cmd, value);
205 }
206
207 /* See cli-style.h. */
208
209 void
210 cli_style_option::do_show_background (struct ui_file *file, int from_tty,
211 struct cmd_list_element *cmd,
212 const char *value)
213 {
214 do_show (_("background color"), file, cmd, value);
215 }
216
217 /* See cli-style.h. */
218
219 void
220 cli_style_option::do_show_intensity (struct ui_file *file, int from_tty,
221 struct cmd_list_element *cmd,
222 const char *value)
223 {
224 do_show (_("display intensity"), file, cmd, value);
225 }
226
227 /* See cli-style.h. */
228
229 void
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,
234 bool skip_intensity)
235 {
236 m_set_prefix = std::string ("set style ") + m_name + " ";
237 m_show_prefix = std::string ("show style ") + m_name + " ";
238
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);
243
244 add_setshow_enum_cmd ("foreground", theclass, cli_colors,
245 &m_foreground,
246 _("Set the foreground color for this property."),
247 _("Show the foreground color for this property."),
248 nullptr,
249 do_set_value,
250 do_show_foreground,
251 &m_set_list, &m_show_list, (void *) this);
252 add_setshow_enum_cmd ("background", theclass, cli_colors,
253 &m_background,
254 _("Set the background color for this property."),
255 _("Show the background color for this property."),
256 nullptr,
257 do_set_value,
258 do_show_background,
259 &m_set_list, &m_show_list, (void *) this);
260 if (!skip_intensity)
261 add_setshow_enum_cmd ("intensity", theclass, cli_intensities,
262 &m_intensity,
263 _("Set the display intensity for this property."),
264 _("Show the display intensity for this property."),
265 nullptr,
266 do_set_value,
267 do_show_intensity,
268 &m_set_list, &m_show_list, (void *) this);
269 }
270
271 static cmd_list_element *style_set_list;
272 static cmd_list_element *style_show_list;
273
274 static void
275 set_style_enabled (const char *args, int from_tty, struct cmd_list_element *c)
276 {
277 g_source_cache.clear ();
278 gdb::observers::source_styling_changed.notify ();
279 }
280
281 static void
282 show_style_enabled (struct ui_file *file, int from_tty,
283 struct cmd_list_element *c, const char *value)
284 {
285 if (cli_styling)
286 fprintf_filtered (file, _("CLI output styling is enabled.\n"));
287 else
288 fprintf_filtered (file, _("CLI output styling is disabled.\n"));
289 }
290
291 static void
292 show_style_sources (struct ui_file *file, int from_tty,
293 struct cmd_list_element *c, const char *value)
294 {
295 if (source_styling)
296 fprintf_filtered (file, _("Source code styling is enabled.\n"));
297 else
298 fprintf_filtered (file, _("Source code styling is disabled.\n"));
299 }
300
301 void _initialize_cli_style ();
302 void
303 _initialize_cli_style ()
304 {
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);
313
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);
320
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\"."
328 #else
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."
332 #endif
333 ), set_style_enabled, show_style_sources,
334 &style_set_list, &style_show_list);
335
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,
340 false);
341
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,
346 false);
347
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,
352 false);
353
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,
358 false);
359
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\
364 readability."),
365 &style_set_list, &style_show_list,
366 false);
367
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\
372 of their output."),
373 &style_set_list, &style_show_list,
374 false);
375
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,
382 false);
383
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."),
389 &style_set_list,
390 &style_show_list,
391 false);
392
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."),
398 &style_set_list,
399 &style_show_list,
400 false);
401
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."),
407 &style_set_list,
408 &style_show_list,
409 false);
410
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,
417 true);
418
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."),
424 &style_set_list,
425 &style_show_list,
426 true);
427 }