Commit | Line | Data |
---|---|---|
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__) |
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 | ||
7f6aba03 TT |
88 | cli_style_option metadata_style ("metadata", ui_file_style::DIM); |
89 | ||
90 | /* See cli-style.h. */ | |
91 | ||
9303eb2f PW |
92 | cli_style_option::cli_style_option (const char *name, |
93 | ui_file_style::basic_color fg) | |
94 | : m_name (name), | |
95 | m_foreground (cli_colors[fg - ui_file_style::NONE]), | |
cbe56571 TT |
96 | m_background (cli_colors[0]), |
97 | m_intensity (cli_intensities[ui_file_style::NORMAL]) | |
98 | { | |
99 | } | |
100 | ||
9303eb2f PW |
101 | /* See cli-style.h. */ |
102 | ||
103 | cli_style_option::cli_style_option (const char *name, | |
104 | ui_file_style::intensity i) | |
105 | : m_name (name), | |
106 | m_foreground (cli_colors[0]), | |
107 | m_background (cli_colors[0]), | |
108 | m_intensity (cli_intensities[i]) | |
109 | { | |
110 | } | |
111 | ||
cbe56571 TT |
112 | /* Return the color number corresponding to COLOR. */ |
113 | ||
114 | static int | |
115 | color_number (const char *color) | |
116 | { | |
117 | for (int i = 0; i < ARRAY_SIZE (cli_colors); ++i) | |
118 | { | |
119 | if (color == cli_colors[i]) | |
120 | return i - 1; | |
121 | } | |
122 | gdb_assert_not_reached ("color not found"); | |
123 | } | |
124 | ||
125 | /* See cli-style.h. */ | |
126 | ||
127 | ui_file_style | |
128 | cli_style_option::style () const | |
129 | { | |
130 | int fg = color_number (m_foreground); | |
131 | int bg = color_number (m_background); | |
132 | ui_file_style::intensity intensity = ui_file_style::NORMAL; | |
133 | ||
134 | for (int i = 0; i < ARRAY_SIZE (cli_intensities); ++i) | |
135 | { | |
136 | if (m_intensity == cli_intensities[i]) | |
137 | { | |
138 | intensity = (ui_file_style::intensity) i; | |
139 | break; | |
140 | } | |
141 | } | |
142 | ||
143 | return ui_file_style (fg, bg, intensity); | |
144 | } | |
145 | ||
9303eb2f PW |
146 | /* Implements the cli_style_option::do_show_* functions. |
147 | WHAT and VALUE are the property and value to show. | |
148 | The style for which WHAT is shown is retrieved from CMD context. */ | |
149 | ||
150 | static void | |
151 | do_show (const char *what, struct ui_file *file, | |
152 | struct cmd_list_element *cmd, | |
153 | const char *value) | |
154 | { | |
155 | cli_style_option *cso = (cli_style_option *) get_cmd_context (cmd); | |
156 | fputs_filtered (_("The "), file); | |
157 | fprintf_styled (file, cso->style (), _("\"%s\" style"), cso->name ()); | |
158 | fprintf_filtered (file, _(" %s is: %s\n"), what, value); | |
159 | } | |
160 | ||
cbe56571 TT |
161 | /* See cli-style.h. */ |
162 | ||
cbe56571 TT |
163 | void |
164 | cli_style_option::do_show_foreground (struct ui_file *file, int from_tty, | |
165 | struct cmd_list_element *cmd, | |
166 | const char *value) | |
167 | { | |
9303eb2f | 168 | do_show (_("foreground color"), file, cmd, value); |
cbe56571 TT |
169 | } |
170 | ||
171 | /* See cli-style.h. */ | |
172 | ||
173 | void | |
174 | cli_style_option::do_show_background (struct ui_file *file, int from_tty, | |
175 | struct cmd_list_element *cmd, | |
176 | const char *value) | |
177 | { | |
9303eb2f | 178 | do_show (_("background color"), file, cmd, value); |
cbe56571 TT |
179 | } |
180 | ||
181 | /* See cli-style.h. */ | |
182 | ||
183 | void | |
184 | cli_style_option::do_show_intensity (struct ui_file *file, int from_tty, | |
185 | struct cmd_list_element *cmd, | |
186 | const char *value) | |
187 | { | |
9303eb2f | 188 | do_show (_("display intensity"), file, cmd, value); |
cbe56571 TT |
189 | } |
190 | ||
191 | /* See cli-style.h. */ | |
192 | ||
193 | void | |
9303eb2f | 194 | cli_style_option::add_setshow_commands (enum command_class theclass, |
cbe56571 | 195 | const char *prefix_doc, |
cbe56571 | 196 | struct cmd_list_element **set_list, |
d73cff18 PW |
197 | void (*do_set) (const char *args, |
198 | int from_tty), | |
199 | struct cmd_list_element **show_list, | |
200 | void (*do_show) (const char *args, | |
201 | int from_tty)) | |
cbe56571 | 202 | { |
9303eb2f PW |
203 | m_set_prefix = std::string ("set style ") + m_name + " "; |
204 | m_show_prefix = std::string ("show style ") + m_name + " "; | |
cbe56571 | 205 | |
9303eb2f | 206 | add_prefix_cmd (m_name, no_class, do_set, prefix_doc, &m_set_list, |
ecad3b21 | 207 | m_set_prefix.c_str (), 0, set_list); |
9303eb2f | 208 | add_prefix_cmd (m_name, no_class, do_show, prefix_doc, &m_show_list, |
ecad3b21 | 209 | m_show_prefix.c_str (), 0, show_list); |
cbe56571 TT |
210 | |
211 | add_setshow_enum_cmd ("foreground", theclass, cli_colors, | |
212 | &m_foreground, | |
590042fc PW |
213 | _("Set the foreground color for this property."), |
214 | _("Show the foreground color for this property."), | |
cbe56571 TT |
215 | nullptr, |
216 | nullptr, | |
217 | do_show_foreground, | |
9303eb2f | 218 | &m_set_list, &m_show_list, (void *) this); |
cbe56571 TT |
219 | add_setshow_enum_cmd ("background", theclass, cli_colors, |
220 | &m_background, | |
590042fc PW |
221 | _("Set the background color for this property."), |
222 | _("Show the background color for this property."), | |
cbe56571 TT |
223 | nullptr, |
224 | nullptr, | |
225 | do_show_background, | |
9303eb2f | 226 | &m_set_list, &m_show_list, (void *) this); |
cbe56571 TT |
227 | add_setshow_enum_cmd ("intensity", theclass, cli_intensities, |
228 | &m_intensity, | |
590042fc PW |
229 | _("Set the display intensity for this property."), |
230 | _("Show the display intensity for this property."), | |
cbe56571 TT |
231 | nullptr, |
232 | nullptr, | |
233 | do_show_intensity, | |
9303eb2f | 234 | &m_set_list, &m_show_list, (void *) this); |
cbe56571 TT |
235 | } |
236 | ||
d73cff18 PW |
237 | static cmd_list_element *style_set_list; |
238 | static cmd_list_element *style_show_list; | |
239 | ||
cbe56571 TT |
240 | static void |
241 | set_style (const char *arg, int from_tty) | |
242 | { | |
d73cff18 PW |
243 | printf_unfiltered (_("\"set style\" must be followed " |
244 | "by an appropriate subcommand.\n")); | |
245 | help_list (style_set_list, "set style ", all_commands, gdb_stdout); | |
cbe56571 TT |
246 | } |
247 | ||
248 | static void | |
249 | show_style (const char *arg, int from_tty) | |
250 | { | |
d73cff18 | 251 | cmd_show_list (style_show_list, from_tty, ""); |
cbe56571 TT |
252 | } |
253 | ||
62f29fda TT |
254 | static void |
255 | set_style_enabled (const char *args, int from_tty, struct cmd_list_element *c) | |
256 | { | |
257 | g_source_cache.clear (); | |
6f11e682 | 258 | gdb::observers::source_styling_changed.notify (); |
62f29fda TT |
259 | } |
260 | ||
cbe56571 TT |
261 | static void |
262 | show_style_enabled (struct ui_file *file, int from_tty, | |
263 | struct cmd_list_element *c, const char *value) | |
264 | { | |
265 | if (cli_styling) | |
266 | fprintf_filtered (file, _("CLI output styling is enabled.\n")); | |
267 | else | |
268 | fprintf_filtered (file, _("CLI output styling is disabled.\n")); | |
269 | } | |
270 | ||
d085f989 TT |
271 | static void |
272 | show_style_sources (struct ui_file *file, int from_tty, | |
273 | struct cmd_list_element *c, const char *value) | |
274 | { | |
275 | if (source_styling) | |
276 | fprintf_filtered (file, _("Source code styling is enabled.\n")); | |
277 | else | |
278 | fprintf_filtered (file, _("Source code styling is disabled.\n")); | |
279 | } | |
280 | ||
9303eb2f PW |
281 | /* Builds the "set style NAME " prefix. */ |
282 | ||
283 | static std::string | |
284 | set_style_name (const char *name) | |
285 | { | |
286 | std::string result ("set style "); | |
287 | ||
288 | result += name; | |
289 | result += " "; | |
290 | return result; | |
291 | } | |
292 | ||
cbe56571 TT |
293 | void |
294 | _initialize_cli_style () | |
295 | { | |
cbe56571 | 296 | add_prefix_cmd ("style", no_class, set_style, _("\ |
590042fc | 297 | Style-specific settings.\n\ |
cbe56571 TT |
298 | Configure various style-related variables, such as colors"), |
299 | &style_set_list, "set style ", 0, &setlist); | |
300 | add_prefix_cmd ("style", no_class, show_style, _("\ | |
590042fc | 301 | Style-specific settings.\n\ |
cbe56571 TT |
302 | Configure various style-related variables, such as colors"), |
303 | &style_show_list, "show style ", 0, &showlist); | |
304 | ||
305 | add_setshow_boolean_cmd ("enabled", no_class, &cli_styling, _("\ | |
306 | Set whether CLI styling is enabled."), _("\ | |
307 | Show whether CLI is enabled."), _("\ | |
308 | If enabled, output to the terminal is styled."), | |
62f29fda | 309 | set_style_enabled, show_style_enabled, |
cbe56571 TT |
310 | &style_set_list, &style_show_list); |
311 | ||
d085f989 TT |
312 | add_setshow_boolean_cmd ("sources", no_class, &source_styling, _("\ |
313 | Set whether source code styling is enabled."), _("\ | |
314 | Show whether source code styling is enabled."), _("\ | |
315 | If enabled, source code is styled.\n" | |
316 | #ifdef HAVE_SOURCE_HIGHLIGHT | |
317 | "Note that source styling only works if styling in general is enabled,\n\ | |
318 | see \"show style enabled\"." | |
319 | #else | |
320 | "Source highlighting is disabled in this installation of gdb, because\n\ | |
321 | it was not linked against GNU Source Highlight." | |
322 | #endif | |
323 | ), set_style_enabled, show_style_sources, | |
324 | &style_set_list, &style_show_list); | |
325 | ||
9303eb2f PW |
326 | #define STYLE_ADD_SETSHOW_COMMANDS(STYLE, PREFIX_DOC) \ |
327 | STYLE.add_setshow_commands (no_class, PREFIX_DOC, \ | |
d73cff18 PW |
328 | &style_set_list, \ |
329 | [] (const char *args, int from_tty) \ | |
330 | { \ | |
331 | help_list \ | |
332 | (STYLE.set_list (), \ | |
9303eb2f | 333 | set_style_name (STYLE.name ()).c_str (), \ |
d73cff18 PW |
334 | all_commands, \ |
335 | gdb_stdout); \ | |
336 | }, \ | |
337 | &style_show_list, \ | |
338 | [] (const char *args, int from_tty) \ | |
339 | { \ | |
340 | cmd_show_list \ | |
341 | (STYLE.show_list (), \ | |
342 | from_tty, \ | |
343 | ""); \ | |
344 | }) | |
345 | ||
9303eb2f | 346 | STYLE_ADD_SETSHOW_COMMANDS (file_name_style, |
d73cff18 | 347 | _("\ |
590042fc | 348 | Filename display styling.\n\ |
d73cff18 PW |
349 | Configure filename colors and display intensity.")); |
350 | ||
9303eb2f | 351 | STYLE_ADD_SETSHOW_COMMANDS (function_name_style, |
d73cff18 | 352 | _("\ |
590042fc | 353 | Function name display styling.\n\ |
d73cff18 PW |
354 | Configure function name colors and display intensity")); |
355 | ||
9303eb2f | 356 | STYLE_ADD_SETSHOW_COMMANDS (variable_name_style, |
d73cff18 | 357 | _("\ |
590042fc | 358 | Variable name display styling.\n\ |
d73cff18 PW |
359 | Configure variable name colors and display intensity")); |
360 | ||
9303eb2f | 361 | STYLE_ADD_SETSHOW_COMMANDS (address_style, |
d73cff18 | 362 | _("\ |
590042fc | 363 | Address display styling.\n\ |
d73cff18 | 364 | Configure address colors and display intensity")); |
9303eb2f PW |
365 | |
366 | STYLE_ADD_SETSHOW_COMMANDS (title_style, | |
367 | _("\ | |
590042fc | 368 | Title display styling.\n\ |
9303eb2f PW |
369 | Configure title colors and display intensity\n\ |
370 | Some commands (such as \"apropos -v REGEXP\") use the title style to improve\n\ | |
371 | readability.")); | |
372 | ||
373 | STYLE_ADD_SETSHOW_COMMANDS (highlight_style, | |
374 | _("\ | |
590042fc | 375 | Highlight display styling.\n\ |
9303eb2f PW |
376 | Configure highlight colors and display intensity\n\ |
377 | Some commands use the highlight style to draw the attention to a part\n\ | |
378 | of their output.")); | |
7f6aba03 TT |
379 | |
380 | STYLE_ADD_SETSHOW_COMMANDS (metadata_style, | |
381 | _("\ | |
4d825eab | 382 | Metadata display styling.\n\ |
7f6aba03 TT |
383 | Configure metadata colors and display intensity\n\ |
384 | The \"metadata\" style is used when GDB displays information about\n\ | |
385 | your data, for example \"<unavailable>\"")); | |
cbe56571 | 386 | } |