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