Commit | Line | Data |
---|---|---|
cbe56571 TT |
1 | /* CLI colorizing |
2 | ||
b811d2c2 | 3 | Copyright (C) 2018-2020 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 | ||
06a6207a | 28 | #if defined (__MSDOS__) |
491144b5 | 29 | bool cli_styling = false; |
cbe56571 | 30 | #else |
491144b5 | 31 | bool cli_styling = true; |
cbe56571 TT |
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 | ||
491144b5 | 37 | bool source_styling = true; |
d085f989 | 38 | |
cbe56571 TT |
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 | ||
9303eb2f | 64 | cli_style_option file_name_style ("filename", ui_file_style::GREEN); |
cbe56571 TT |
65 | |
66 | /* See cli-style.h. */ | |
67 | ||
9303eb2f | 68 | cli_style_option function_name_style ("function", ui_file_style::YELLOW); |
cbe56571 TT |
69 | |
70 | /* See cli-style.h. */ | |
71 | ||
9303eb2f | 72 | cli_style_option variable_name_style ("variable", ui_file_style::CYAN); |
80ae2043 TT |
73 | |
74 | /* See cli-style.h. */ | |
75 | ||
9303eb2f | 76 | cli_style_option address_style ("address", ui_file_style::BLUE); |
35fb8261 TT |
77 | |
78 | /* See cli-style.h. */ | |
79 | ||
9303eb2f PW |
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 | ||
a2a7af0c TT |
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 | ||
7f6aba03 TT |
97 | cli_style_option metadata_style ("metadata", ui_file_style::DIM); |
98 | ||
99 | /* See cli-style.h. */ | |
100 | ||
eca1f90c TT |
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 | ||
9303eb2f PW |
116 | cli_style_option::cli_style_option (const char *name, |
117 | ui_file_style::basic_color fg) | |
a2a7af0c TT |
118 | : changed (name), |
119 | m_name (name), | |
9303eb2f | 120 | m_foreground (cli_colors[fg - ui_file_style::NONE]), |
cbe56571 TT |
121 | m_background (cli_colors[0]), |
122 | m_intensity (cli_intensities[ui_file_style::NORMAL]) | |
123 | { | |
124 | } | |
125 | ||
9303eb2f PW |
126 | /* See cli-style.h. */ |
127 | ||
128 | cli_style_option::cli_style_option (const char *name, | |
129 | ui_file_style::intensity i) | |
a2a7af0c TT |
130 | : changed (name), |
131 | m_name (name), | |
9303eb2f PW |
132 | m_foreground (cli_colors[0]), |
133 | m_background (cli_colors[0]), | |
134 | m_intensity (cli_intensities[i]) | |
135 | { | |
136 | } | |
137 | ||
cbe56571 TT |
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 | ||
a2a7af0c TT |
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 | ||
9303eb2f PW |
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 | ||
cbe56571 TT |
197 | /* See cli-style.h. */ |
198 | ||
cbe56571 TT |
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 | { | |
9303eb2f | 204 | do_show (_("foreground color"), file, cmd, value); |
cbe56571 TT |
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 | { | |
9303eb2f | 214 | do_show (_("background color"), file, cmd, value); |
cbe56571 TT |
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 | { | |
9303eb2f | 224 | do_show (_("display intensity"), file, cmd, value); |
cbe56571 TT |
225 | } |
226 | ||
227 | /* See cli-style.h. */ | |
228 | ||
229 | void | |
9303eb2f | 230 | cli_style_option::add_setshow_commands (enum command_class theclass, |
cbe56571 | 231 | const char *prefix_doc, |
cbe56571 | 232 | struct cmd_list_element **set_list, |
d73cff18 | 233 | struct cmd_list_element **show_list, |
a2a7af0c | 234 | bool skip_intensity) |
cbe56571 | 235 | { |
9303eb2f PW |
236 | m_set_prefix = std::string ("set style ") + m_name + " "; |
237 | m_show_prefix = std::string ("show style ") + m_name + " "; | |
cbe56571 | 238 | |
0743fc83 TT |
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); | |
cbe56571 TT |
243 | |
244 | add_setshow_enum_cmd ("foreground", theclass, cli_colors, | |
245 | &m_foreground, | |
590042fc PW |
246 | _("Set the foreground color for this property."), |
247 | _("Show the foreground color for this property."), | |
cbe56571 | 248 | nullptr, |
a2a7af0c | 249 | do_set_value, |
cbe56571 | 250 | do_show_foreground, |
9303eb2f | 251 | &m_set_list, &m_show_list, (void *) this); |
cbe56571 TT |
252 | add_setshow_enum_cmd ("background", theclass, cli_colors, |
253 | &m_background, | |
590042fc PW |
254 | _("Set the background color for this property."), |
255 | _("Show the background color for this property."), | |
cbe56571 | 256 | nullptr, |
a2a7af0c | 257 | do_set_value, |
cbe56571 | 258 | do_show_background, |
9303eb2f | 259 | &m_set_list, &m_show_list, (void *) this); |
a2a7af0c TT |
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); | |
cbe56571 TT |
269 | } |
270 | ||
d73cff18 PW |
271 | static cmd_list_element *style_set_list; |
272 | static cmd_list_element *style_show_list; | |
273 | ||
62f29fda TT |
274 | static void |
275 | set_style_enabled (const char *args, int from_tty, struct cmd_list_element *c) | |
276 | { | |
277 | g_source_cache.clear (); | |
6f11e682 | 278 | gdb::observers::source_styling_changed.notify (); |
62f29fda TT |
279 | } |
280 | ||
cbe56571 TT |
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 | ||
d085f989 TT |
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 | ||
6c265988 | 301 | void _initialize_cli_style (); |
cbe56571 TT |
302 | void |
303 | _initialize_cli_style () | |
304 | { | |
0743fc83 | 305 | add_basic_prefix_cmd ("style", no_class, _("\ |
590042fc | 306 | Style-specific settings.\n\ |
cbe56571 TT |
307 | Configure various style-related variables, such as colors"), |
308 | &style_set_list, "set style ", 0, &setlist); | |
0743fc83 | 309 | add_show_prefix_cmd ("style", no_class, _("\ |
590042fc | 310 | Style-specific settings.\n\ |
cbe56571 TT |
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."), | |
62f29fda | 318 | set_style_enabled, show_style_enabled, |
cbe56571 TT |
319 | &style_set_list, &style_show_list); |
320 | ||
d085f989 TT |
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 | |
f6474de9 TT |
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." | |
d085f989 TT |
332 | #endif |
333 | ), set_style_enabled, show_style_sources, | |
334 | &style_set_list, &style_show_list); | |
335 | ||
0743fc83 | 336 | file_name_style.add_setshow_commands (no_class, _("\ |
590042fc | 337 | Filename display styling.\n\ |
0743fc83 TT |
338 | Configure filename colors and display intensity."), |
339 | &style_set_list, &style_show_list, | |
340 | false); | |
d73cff18 | 341 | |
0743fc83 | 342 | function_name_style.add_setshow_commands (no_class, _("\ |
590042fc | 343 | Function name display styling.\n\ |
0743fc83 TT |
344 | Configure function name colors and display intensity"), |
345 | &style_set_list, &style_show_list, | |
346 | false); | |
d73cff18 | 347 | |
0743fc83 | 348 | variable_name_style.add_setshow_commands (no_class, _("\ |
590042fc | 349 | Variable name display styling.\n\ |
0743fc83 TT |
350 | Configure variable name colors and display intensity"), |
351 | &style_set_list, &style_show_list, | |
352 | false); | |
d73cff18 | 353 | |
0743fc83 | 354 | address_style.add_setshow_commands (no_class, _("\ |
590042fc | 355 | Address display styling.\n\ |
0743fc83 TT |
356 | Configure address colors and display intensity"), |
357 | &style_set_list, &style_show_list, | |
358 | false); | |
9303eb2f | 359 | |
0743fc83 | 360 | title_style.add_setshow_commands (no_class, _("\ |
590042fc | 361 | Title display styling.\n\ |
9303eb2f PW |
362 | Configure title colors and display intensity\n\ |
363 | Some commands (such as \"apropos -v REGEXP\") use the title style to improve\n\ | |
0743fc83 TT |
364 | readability."), |
365 | &style_set_list, &style_show_list, | |
366 | false); | |
9303eb2f | 367 | |
0743fc83 | 368 | highlight_style.add_setshow_commands (no_class, _("\ |
590042fc | 369 | Highlight display styling.\n\ |
9303eb2f PW |
370 | Configure highlight colors and display intensity\n\ |
371 | Some commands use the highlight style to draw the attention to a part\n\ | |
0743fc83 TT |
372 | of their output."), |
373 | &style_set_list, &style_show_list, | |
374 | false); | |
7f6aba03 | 375 | |
0743fc83 | 376 | metadata_style.add_setshow_commands (no_class, _("\ |
4d825eab | 377 | Metadata display styling.\n\ |
7f6aba03 TT |
378 | Configure metadata colors and display intensity\n\ |
379 | The \"metadata\" style is used when GDB displays information about\n\ | |
0743fc83 TT |
380 | your data, for example \"<unavailable>\""), |
381 | &style_set_list, &style_show_list, | |
382 | false); | |
a2a7af0c | 383 | |
eca1f90c TT |
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 | ||
0743fc83 | 411 | tui_border_style.add_setshow_commands (no_class, _("\ |
a2a7af0c TT |
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\ | |
0743fc83 TT |
415 | TUI window that does not have the focus."), |
416 | &style_set_list, &style_show_list, | |
417 | true); | |
a2a7af0c | 418 | |
0743fc83 | 419 | tui_active_border_style.add_setshow_commands (no_class, _("\ |
a2a7af0c TT |
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\ | |
0743fc83 TT |
423 | TUI window that does have the focus."), |
424 | &style_set_list, | |
425 | &style_show_list, | |
426 | true); | |
cbe56571 | 427 | } |