Implement help/show values for 'set|show style'.
[deliverable/binutils-gdb.git] / gdb / cli / cli-style.c
1 /* CLI colorizing
2
3 Copyright (C) 2018-2019 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
25 /* True if styling is enabled. */
26
27 #if defined(_WIN32) || defined (__CYGWIN__)
28 int cli_styling = 0;
29 #else
30 int cli_styling = 1;
31 #endif
32
33 /* Name of colors; must correspond to ui_file_style::basic_color. */
34 static const char * const cli_colors[] = {
35 "none",
36 "black",
37 "red",
38 "green",
39 "yellow",
40 "blue",
41 "magenta",
42 "cyan",
43 "white",
44 nullptr
45 };
46
47 /* Names of intensities; must correspond to
48 ui_file_style::intensity. */
49 static const char * const cli_intensities[] = {
50 "normal",
51 "bold",
52 "dim",
53 nullptr
54 };
55
56 /* See cli-style.h. */
57
58 cli_style_option file_name_style (ui_file_style::GREEN);
59
60 /* See cli-style.h. */
61
62 cli_style_option function_name_style (ui_file_style::YELLOW);
63
64 /* See cli-style.h. */
65
66 cli_style_option variable_name_style (ui_file_style::CYAN);
67
68 /* See cli-style.h. */
69
70 cli_style_option address_style (ui_file_style::BLUE);
71
72 /* See cli-style.h. */
73
74 cli_style_option::cli_style_option (ui_file_style::basic_color fg)
75 : m_foreground (cli_colors[fg - ui_file_style::NONE]),
76 m_background (cli_colors[0]),
77 m_intensity (cli_intensities[ui_file_style::NORMAL])
78 {
79 }
80
81 /* Return the color number corresponding to COLOR. */
82
83 static int
84 color_number (const char *color)
85 {
86 for (int i = 0; i < ARRAY_SIZE (cli_colors); ++i)
87 {
88 if (color == cli_colors[i])
89 return i - 1;
90 }
91 gdb_assert_not_reached ("color not found");
92 }
93
94 /* See cli-style.h. */
95
96 ui_file_style
97 cli_style_option::style () const
98 {
99 int fg = color_number (m_foreground);
100 int bg = color_number (m_background);
101 ui_file_style::intensity intensity = ui_file_style::NORMAL;
102
103 for (int i = 0; i < ARRAY_SIZE (cli_intensities); ++i)
104 {
105 if (m_intensity == cli_intensities[i])
106 {
107 intensity = (ui_file_style::intensity) i;
108 break;
109 }
110 }
111
112 return ui_file_style (fg, bg, intensity);
113 }
114
115 /* See cli-style.h. */
116
117 void
118 cli_style_option::do_show_foreground (struct ui_file *file, int from_tty,
119 struct cmd_list_element *cmd,
120 const char *value)
121 {
122 const char *name = (const char *) get_cmd_context (cmd);
123 fprintf_filtered (file, _("The \"%s\" foreground color is: %s\n"),
124 name, value);
125 }
126
127 /* See cli-style.h. */
128
129 void
130 cli_style_option::do_show_background (struct ui_file *file, int from_tty,
131 struct cmd_list_element *cmd,
132 const char *value)
133 {
134 const char *name = (const char *) get_cmd_context (cmd);
135 fprintf_filtered (file, _("The \"%s\" background color is: %s\n"),
136 name, value);
137 }
138
139 /* See cli-style.h. */
140
141 void
142 cli_style_option::do_show_intensity (struct ui_file *file, int from_tty,
143 struct cmd_list_element *cmd,
144 const char *value)
145 {
146 const char *name = (const char *) get_cmd_context (cmd);
147 fprintf_filtered (file, _("The \"%s\" display intensity is: %s\n"),
148 name, value);
149 }
150
151 /* See cli-style.h. */
152
153 void
154 cli_style_option::add_setshow_commands (const char *name,
155 enum command_class theclass,
156 const char *prefix_doc,
157 struct cmd_list_element **set_list,
158 void (*do_set) (const char *args,
159 int from_tty),
160 struct cmd_list_element **show_list,
161 void (*do_show) (const char *args,
162 int from_tty))
163 {
164 m_set_prefix = std::string ("set style ") + name + " ";
165 m_show_prefix = std::string ("show style ") + name + " ";
166
167 add_prefix_cmd (name, no_class, do_set, prefix_doc, &m_set_list,
168 m_set_prefix.c_str (), 0, set_list);
169 add_prefix_cmd (name, no_class, do_show, prefix_doc, &m_show_list,
170 m_show_prefix.c_str (), 0, show_list);
171
172 add_setshow_enum_cmd ("foreground", theclass, cli_colors,
173 &m_foreground,
174 _("Set the foreground color for this property"),
175 _("Show the foreground color for this property"),
176 nullptr,
177 nullptr,
178 do_show_foreground,
179 &m_set_list, &m_show_list, (void *) name);
180 add_setshow_enum_cmd ("background", theclass, cli_colors,
181 &m_background,
182 _("Set the background color for this property"),
183 _("Show the background color for this property"),
184 nullptr,
185 nullptr,
186 do_show_background,
187 &m_set_list, &m_show_list, (void *) name);
188 add_setshow_enum_cmd ("intensity", theclass, cli_intensities,
189 &m_intensity,
190 _("Set the display intensity for this property"),
191 _("Show the display intensity for this property"),
192 nullptr,
193 nullptr,
194 do_show_intensity,
195 &m_set_list, &m_show_list, (void *) name);
196 }
197
198 static cmd_list_element *style_set_list;
199 static cmd_list_element *style_show_list;
200
201 static void
202 set_style (const char *arg, int from_tty)
203 {
204 printf_unfiltered (_("\"set style\" must be followed "
205 "by an appropriate subcommand.\n"));
206 help_list (style_set_list, "set style ", all_commands, gdb_stdout);
207 }
208
209 static void
210 show_style (const char *arg, int from_tty)
211 {
212 cmd_show_list (style_show_list, from_tty, "");
213 }
214
215 static void
216 set_style_enabled (const char *args, int from_tty, struct cmd_list_element *c)
217 {
218 g_source_cache.clear ();
219 }
220
221 static void
222 show_style_enabled (struct ui_file *file, int from_tty,
223 struct cmd_list_element *c, const char *value)
224 {
225 if (cli_styling)
226 fprintf_filtered (file, _("CLI output styling is enabled.\n"));
227 else
228 fprintf_filtered (file, _("CLI output styling is disabled.\n"));
229 }
230
231 void
232 _initialize_cli_style ()
233 {
234 add_prefix_cmd ("style", no_class, set_style, _("\
235 Style-specific settings\n\
236 Configure various style-related variables, such as colors"),
237 &style_set_list, "set style ", 0, &setlist);
238 add_prefix_cmd ("style", no_class, show_style, _("\
239 Style-specific settings\n\
240 Configure various style-related variables, such as colors"),
241 &style_show_list, "show style ", 0, &showlist);
242
243 add_setshow_boolean_cmd ("enabled", no_class, &cli_styling, _("\
244 Set whether CLI styling is enabled."), _("\
245 Show whether CLI is enabled."), _("\
246 If enabled, output to the terminal is styled."),
247 set_style_enabled, show_style_enabled,
248 &style_set_list, &style_show_list);
249
250 #define STYLE_ADD_SETSHOW_COMMANDS(STYLE, NAME, PREFIX_DOC) \
251 STYLE.add_setshow_commands (NAME, no_class, PREFIX_DOC, \
252 &style_set_list, \
253 [] (const char *args, int from_tty) \
254 { \
255 help_list \
256 (STYLE.set_list (), \
257 "set style " NAME " ", \
258 all_commands, \
259 gdb_stdout); \
260 }, \
261 &style_show_list, \
262 [] (const char *args, int from_tty) \
263 { \
264 cmd_show_list \
265 (STYLE.show_list (), \
266 from_tty, \
267 ""); \
268 })
269
270 STYLE_ADD_SETSHOW_COMMANDS (file_name_style, "filename",
271 _("\
272 Filename display styling\n\
273 Configure filename colors and display intensity."));
274
275 STYLE_ADD_SETSHOW_COMMANDS (function_name_style, "function",
276 _("\
277 Function name display styling\n\
278 Configure function name colors and display intensity"));
279
280 STYLE_ADD_SETSHOW_COMMANDS (variable_name_style, "variable",
281 _("\
282 Variable name display styling\n\
283 Configure variable name colors and display intensity"));
284
285 STYLE_ADD_SETSHOW_COMMANDS (address_style, "address",
286 _("\
287 Address display styling\n\
288 Configure address colors and display intensity"));
289 }
This page took 0.036401 seconds and 5 git commands to generate.