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