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__) |
cbe56571 TT |
29 | int cli_styling = 0; |
30 | #else | |
31 | int 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 | ||
37 | int source_styling = 1; | |
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 | ||
64 | cli_style_option file_name_style (ui_file_style::GREEN); | |
65 | ||
66 | /* See cli-style.h. */ | |
67 | ||
68 | cli_style_option function_name_style (ui_file_style::YELLOW); | |
69 | ||
70 | /* See cli-style.h. */ | |
71 | ||
80ae2043 TT |
72 | cli_style_option variable_name_style (ui_file_style::CYAN); |
73 | ||
74 | /* See cli-style.h. */ | |
75 | ||
35fb8261 TT |
76 | cli_style_option address_style (ui_file_style::BLUE); |
77 | ||
78 | /* See cli-style.h. */ | |
79 | ||
cbe56571 TT |
80 | cli_style_option::cli_style_option (ui_file_style::basic_color fg) |
81 | : m_foreground (cli_colors[fg - ui_file_style::NONE]), | |
82 | m_background (cli_colors[0]), | |
83 | m_intensity (cli_intensities[ui_file_style::NORMAL]) | |
84 | { | |
85 | } | |
86 | ||
87 | /* Return the color number corresponding to COLOR. */ | |
88 | ||
89 | static int | |
90 | color_number (const char *color) | |
91 | { | |
92 | for (int i = 0; i < ARRAY_SIZE (cli_colors); ++i) | |
93 | { | |
94 | if (color == cli_colors[i]) | |
95 | return i - 1; | |
96 | } | |
97 | gdb_assert_not_reached ("color not found"); | |
98 | } | |
99 | ||
100 | /* See cli-style.h. */ | |
101 | ||
102 | ui_file_style | |
103 | cli_style_option::style () const | |
104 | { | |
105 | int fg = color_number (m_foreground); | |
106 | int bg = color_number (m_background); | |
107 | ui_file_style::intensity intensity = ui_file_style::NORMAL; | |
108 | ||
109 | for (int i = 0; i < ARRAY_SIZE (cli_intensities); ++i) | |
110 | { | |
111 | if (m_intensity == cli_intensities[i]) | |
112 | { | |
113 | intensity = (ui_file_style::intensity) i; | |
114 | break; | |
115 | } | |
116 | } | |
117 | ||
118 | return ui_file_style (fg, bg, intensity); | |
119 | } | |
120 | ||
121 | /* See cli-style.h. */ | |
122 | ||
cbe56571 TT |
123 | void |
124 | cli_style_option::do_show_foreground (struct ui_file *file, int from_tty, | |
125 | struct cmd_list_element *cmd, | |
126 | const char *value) | |
127 | { | |
128 | const char *name = (const char *) get_cmd_context (cmd); | |
129 | fprintf_filtered (file, _("The \"%s\" foreground color is: %s\n"), | |
130 | name, value); | |
131 | } | |
132 | ||
133 | /* See cli-style.h. */ | |
134 | ||
135 | void | |
136 | cli_style_option::do_show_background (struct ui_file *file, int from_tty, | |
137 | struct cmd_list_element *cmd, | |
138 | const char *value) | |
139 | { | |
140 | const char *name = (const char *) get_cmd_context (cmd); | |
141 | fprintf_filtered (file, _("The \"%s\" background color is: %s\n"), | |
142 | name, value); | |
143 | } | |
144 | ||
145 | /* See cli-style.h. */ | |
146 | ||
147 | void | |
148 | cli_style_option::do_show_intensity (struct ui_file *file, int from_tty, | |
149 | struct cmd_list_element *cmd, | |
150 | const char *value) | |
151 | { | |
152 | const char *name = (const char *) get_cmd_context (cmd); | |
153 | fprintf_filtered (file, _("The \"%s\" display intensity is: %s\n"), | |
154 | name, value); | |
155 | } | |
156 | ||
157 | /* See cli-style.h. */ | |
158 | ||
159 | void | |
160 | cli_style_option::add_setshow_commands (const char *name, | |
161 | enum command_class theclass, | |
162 | const char *prefix_doc, | |
cbe56571 | 163 | struct cmd_list_element **set_list, |
d73cff18 PW |
164 | void (*do_set) (const char *args, |
165 | int from_tty), | |
166 | struct cmd_list_element **show_list, | |
167 | void (*do_show) (const char *args, | |
168 | int from_tty)) | |
cbe56571 | 169 | { |
d73cff18 PW |
170 | m_set_prefix = std::string ("set style ") + name + " "; |
171 | m_show_prefix = std::string ("show style ") + name + " "; | |
cbe56571 TT |
172 | |
173 | add_prefix_cmd (name, no_class, do_set, prefix_doc, &m_set_list, | |
ecad3b21 | 174 | m_set_prefix.c_str (), 0, set_list); |
cbe56571 | 175 | add_prefix_cmd (name, no_class, do_show, prefix_doc, &m_show_list, |
ecad3b21 | 176 | m_show_prefix.c_str (), 0, show_list); |
cbe56571 TT |
177 | |
178 | add_setshow_enum_cmd ("foreground", theclass, cli_colors, | |
179 | &m_foreground, | |
180 | _("Set the foreground color for this property"), | |
181 | _("Show the foreground color for this property"), | |
182 | nullptr, | |
183 | nullptr, | |
184 | do_show_foreground, | |
185 | &m_set_list, &m_show_list, (void *) name); | |
186 | add_setshow_enum_cmd ("background", theclass, cli_colors, | |
187 | &m_background, | |
188 | _("Set the background color for this property"), | |
189 | _("Show the background color for this property"), | |
190 | nullptr, | |
191 | nullptr, | |
192 | do_show_background, | |
193 | &m_set_list, &m_show_list, (void *) name); | |
194 | add_setshow_enum_cmd ("intensity", theclass, cli_intensities, | |
195 | &m_intensity, | |
d73cff18 PW |
196 | _("Set the display intensity for this property"), |
197 | _("Show the display intensity for this property"), | |
cbe56571 TT |
198 | nullptr, |
199 | nullptr, | |
200 | do_show_intensity, | |
201 | &m_set_list, &m_show_list, (void *) name); | |
202 | } | |
203 | ||
d73cff18 PW |
204 | static cmd_list_element *style_set_list; |
205 | static cmd_list_element *style_show_list; | |
206 | ||
cbe56571 TT |
207 | static void |
208 | set_style (const char *arg, int from_tty) | |
209 | { | |
d73cff18 PW |
210 | printf_unfiltered (_("\"set style\" must be followed " |
211 | "by an appropriate subcommand.\n")); | |
212 | help_list (style_set_list, "set style ", all_commands, gdb_stdout); | |
cbe56571 TT |
213 | } |
214 | ||
215 | static void | |
216 | show_style (const char *arg, int from_tty) | |
217 | { | |
d73cff18 | 218 | cmd_show_list (style_show_list, from_tty, ""); |
cbe56571 TT |
219 | } |
220 | ||
62f29fda TT |
221 | static void |
222 | set_style_enabled (const char *args, int from_tty, struct cmd_list_element *c) | |
223 | { | |
224 | g_source_cache.clear (); | |
6f11e682 | 225 | gdb::observers::source_styling_changed.notify (); |
62f29fda TT |
226 | } |
227 | ||
cbe56571 TT |
228 | static void |
229 | show_style_enabled (struct ui_file *file, int from_tty, | |
230 | struct cmd_list_element *c, const char *value) | |
231 | { | |
232 | if (cli_styling) | |
233 | fprintf_filtered (file, _("CLI output styling is enabled.\n")); | |
234 | else | |
235 | fprintf_filtered (file, _("CLI output styling is disabled.\n")); | |
236 | } | |
237 | ||
d085f989 TT |
238 | static void |
239 | show_style_sources (struct ui_file *file, int from_tty, | |
240 | struct cmd_list_element *c, const char *value) | |
241 | { | |
242 | if (source_styling) | |
243 | fprintf_filtered (file, _("Source code styling is enabled.\n")); | |
244 | else | |
245 | fprintf_filtered (file, _("Source code styling is disabled.\n")); | |
246 | } | |
247 | ||
cbe56571 TT |
248 | void |
249 | _initialize_cli_style () | |
250 | { | |
cbe56571 TT |
251 | add_prefix_cmd ("style", no_class, set_style, _("\ |
252 | Style-specific settings\n\ | |
253 | Configure various style-related variables, such as colors"), | |
254 | &style_set_list, "set style ", 0, &setlist); | |
255 | add_prefix_cmd ("style", no_class, show_style, _("\ | |
256 | Style-specific settings\n\ | |
257 | Configure various style-related variables, such as colors"), | |
258 | &style_show_list, "show style ", 0, &showlist); | |
259 | ||
260 | add_setshow_boolean_cmd ("enabled", no_class, &cli_styling, _("\ | |
261 | Set whether CLI styling is enabled."), _("\ | |
262 | Show whether CLI is enabled."), _("\ | |
263 | If enabled, output to the terminal is styled."), | |
62f29fda | 264 | set_style_enabled, show_style_enabled, |
cbe56571 TT |
265 | &style_set_list, &style_show_list); |
266 | ||
d085f989 TT |
267 | add_setshow_boolean_cmd ("sources", no_class, &source_styling, _("\ |
268 | Set whether source code styling is enabled."), _("\ | |
269 | Show whether source code styling is enabled."), _("\ | |
270 | If enabled, source code is styled.\n" | |
271 | #ifdef HAVE_SOURCE_HIGHLIGHT | |
272 | "Note that source styling only works if styling in general is enabled,\n\ | |
273 | see \"show style enabled\"." | |
274 | #else | |
275 | "Source highlighting is disabled in this installation of gdb, because\n\ | |
276 | it was not linked against GNU Source Highlight." | |
277 | #endif | |
278 | ), set_style_enabled, show_style_sources, | |
279 | &style_set_list, &style_show_list); | |
280 | ||
d73cff18 PW |
281 | #define STYLE_ADD_SETSHOW_COMMANDS(STYLE, NAME, PREFIX_DOC) \ |
282 | STYLE.add_setshow_commands (NAME, no_class, PREFIX_DOC, \ | |
283 | &style_set_list, \ | |
284 | [] (const char *args, int from_tty) \ | |
285 | { \ | |
286 | help_list \ | |
287 | (STYLE.set_list (), \ | |
288 | "set style " NAME " ", \ | |
289 | all_commands, \ | |
290 | gdb_stdout); \ | |
291 | }, \ | |
292 | &style_show_list, \ | |
293 | [] (const char *args, int from_tty) \ | |
294 | { \ | |
295 | cmd_show_list \ | |
296 | (STYLE.show_list (), \ | |
297 | from_tty, \ | |
298 | ""); \ | |
299 | }) | |
300 | ||
301 | STYLE_ADD_SETSHOW_COMMANDS (file_name_style, "filename", | |
302 | _("\ | |
cbe56571 | 303 | Filename display styling\n\ |
d73cff18 PW |
304 | Configure filename colors and display intensity.")); |
305 | ||
306 | STYLE_ADD_SETSHOW_COMMANDS (function_name_style, "function", | |
307 | _("\ | |
cbe56571 | 308 | Function name display styling\n\ |
d73cff18 PW |
309 | Configure function name colors and display intensity")); |
310 | ||
311 | STYLE_ADD_SETSHOW_COMMANDS (variable_name_style, "variable", | |
312 | _("\ | |
80ae2043 | 313 | Variable name display styling\n\ |
d73cff18 PW |
314 | Configure variable name colors and display intensity")); |
315 | ||
316 | STYLE_ADD_SETSHOW_COMMANDS (address_style, "address", | |
317 | _("\ | |
35fb8261 | 318 | Address display styling\n\ |
d73cff18 | 319 | Configure address colors and display intensity")); |
cbe56571 | 320 | } |