Commit | Line | Data |
---|---|---|
cbe56571 TT |
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::cli_style_option (ui_file_style::basic_color fg) | |
66 | : m_foreground (cli_colors[fg - ui_file_style::NONE]), | |
67 | m_background (cli_colors[0]), | |
68 | m_intensity (cli_intensities[ui_file_style::NORMAL]) | |
69 | { | |
70 | } | |
71 | ||
72 | /* Return the color number corresponding to COLOR. */ | |
73 | ||
74 | static int | |
75 | color_number (const char *color) | |
76 | { | |
77 | for (int i = 0; i < ARRAY_SIZE (cli_colors); ++i) | |
78 | { | |
79 | if (color == cli_colors[i]) | |
80 | return i - 1; | |
81 | } | |
82 | gdb_assert_not_reached ("color not found"); | |
83 | } | |
84 | ||
85 | /* See cli-style.h. */ | |
86 | ||
87 | ui_file_style | |
88 | cli_style_option::style () const | |
89 | { | |
90 | int fg = color_number (m_foreground); | |
91 | int bg = color_number (m_background); | |
92 | ui_file_style::intensity intensity = ui_file_style::NORMAL; | |
93 | ||
94 | for (int i = 0; i < ARRAY_SIZE (cli_intensities); ++i) | |
95 | { | |
96 | if (m_intensity == cli_intensities[i]) | |
97 | { | |
98 | intensity = (ui_file_style::intensity) i; | |
99 | break; | |
100 | } | |
101 | } | |
102 | ||
103 | return ui_file_style (fg, bg, intensity); | |
104 | } | |
105 | ||
106 | /* See cli-style.h. */ | |
107 | ||
108 | void | |
109 | cli_style_option::do_set (const char *args, int from_tty) | |
110 | { | |
111 | } | |
112 | ||
113 | /* See cli-style.h. */ | |
114 | ||
115 | void | |
116 | cli_style_option::do_show (const char *args, int from_tty) | |
117 | { | |
118 | } | |
119 | ||
120 | /* See cli-style.h. */ | |
121 | ||
122 | void | |
123 | cli_style_option::do_show_foreground (struct ui_file *file, int from_tty, | |
124 | struct cmd_list_element *cmd, | |
125 | const char *value) | |
126 | { | |
127 | const char *name = (const char *) get_cmd_context (cmd); | |
128 | fprintf_filtered (file, _("The \"%s\" foreground color is: %s\n"), | |
129 | name, value); | |
130 | } | |
131 | ||
132 | /* See cli-style.h. */ | |
133 | ||
134 | void | |
135 | cli_style_option::do_show_background (struct ui_file *file, int from_tty, | |
136 | struct cmd_list_element *cmd, | |
137 | const char *value) | |
138 | { | |
139 | const char *name = (const char *) get_cmd_context (cmd); | |
140 | fprintf_filtered (file, _("The \"%s\" background color is: %s\n"), | |
141 | name, value); | |
142 | } | |
143 | ||
144 | /* See cli-style.h. */ | |
145 | ||
146 | void | |
147 | cli_style_option::do_show_intensity (struct ui_file *file, int from_tty, | |
148 | struct cmd_list_element *cmd, | |
149 | const char *value) | |
150 | { | |
151 | const char *name = (const char *) get_cmd_context (cmd); | |
152 | fprintf_filtered (file, _("The \"%s\" display intensity is: %s\n"), | |
153 | name, value); | |
154 | } | |
155 | ||
156 | /* See cli-style.h. */ | |
157 | ||
158 | void | |
159 | cli_style_option::add_setshow_commands (const char *name, | |
160 | enum command_class theclass, | |
161 | const char *prefix_doc, | |
162 | const char *prefixname, | |
163 | struct cmd_list_element **set_list, | |
164 | struct cmd_list_element **show_list) | |
165 | { | |
166 | m_show_prefix = std::string ("set ") + prefixname + " "; | |
167 | m_show_prefix = std::string ("show ") + prefixname + " "; | |
168 | ||
169 | add_prefix_cmd (name, no_class, do_set, prefix_doc, &m_set_list, | |
170 | m_show_prefix.c_str (), 0, set_list); | |
171 | add_prefix_cmd (name, no_class, do_show, prefix_doc, &m_show_list, | |
172 | m_set_prefix.c_str (), 0, show_list); | |
173 | ||
174 | add_setshow_enum_cmd ("foreground", theclass, cli_colors, | |
175 | &m_foreground, | |
176 | _("Set the foreground color for this property"), | |
177 | _("Show the foreground color for this property"), | |
178 | nullptr, | |
179 | nullptr, | |
180 | do_show_foreground, | |
181 | &m_set_list, &m_show_list, (void *) name); | |
182 | add_setshow_enum_cmd ("background", theclass, cli_colors, | |
183 | &m_background, | |
184 | _("Set the background color for this property"), | |
185 | _("Show the background color for this property"), | |
186 | nullptr, | |
187 | nullptr, | |
188 | do_show_background, | |
189 | &m_set_list, &m_show_list, (void *) name); | |
190 | add_setshow_enum_cmd ("intensity", theclass, cli_intensities, | |
191 | &m_intensity, | |
192 | _("Set the display intensity color for this property"), | |
193 | _("\ | |
194 | Show the display intensity color for this property"), | |
195 | nullptr, | |
196 | nullptr, | |
197 | do_show_intensity, | |
198 | &m_set_list, &m_show_list, (void *) name); | |
199 | } | |
200 | ||
201 | static void | |
202 | set_style (const char *arg, int from_tty) | |
203 | { | |
204 | } | |
205 | ||
206 | static void | |
207 | show_style (const char *arg, int from_tty) | |
208 | { | |
209 | } | |
210 | ||
211 | static void | |
212 | show_style_enabled (struct ui_file *file, int from_tty, | |
213 | struct cmd_list_element *c, const char *value) | |
214 | { | |
215 | if (cli_styling) | |
216 | fprintf_filtered (file, _("CLI output styling is enabled.\n")); | |
217 | else | |
218 | fprintf_filtered (file, _("CLI output styling is disabled.\n")); | |
219 | } | |
220 | ||
221 | void | |
222 | _initialize_cli_style () | |
223 | { | |
224 | static cmd_list_element *style_set_list; | |
225 | static cmd_list_element *style_show_list; | |
226 | ||
227 | add_prefix_cmd ("style", no_class, set_style, _("\ | |
228 | Style-specific settings\n\ | |
229 | Configure various style-related variables, such as colors"), | |
230 | &style_set_list, "set style ", 0, &setlist); | |
231 | add_prefix_cmd ("style", no_class, show_style, _("\ | |
232 | Style-specific settings\n\ | |
233 | Configure various style-related variables, such as colors"), | |
234 | &style_show_list, "show style ", 0, &showlist); | |
235 | ||
236 | add_setshow_boolean_cmd ("enabled", no_class, &cli_styling, _("\ | |
237 | Set whether CLI styling is enabled."), _("\ | |
238 | Show whether CLI is enabled."), _("\ | |
239 | If enabled, output to the terminal is styled."), | |
240 | NULL, show_style_enabled, | |
241 | &style_set_list, &style_show_list); | |
242 | ||
243 | file_name_style.add_setshow_commands ("filename", no_class, | |
244 | _("\ | |
245 | Filename display styling\n\ | |
246 | Configure filename colors and display intensity."), | |
247 | "style filename", | |
248 | &style_set_list, | |
249 | &style_show_list); | |
250 | function_name_style.add_setshow_commands ("function", no_class, | |
251 | _("\ | |
252 | Function name display styling\n\ | |
253 | Configure function name colors and display intensity"), | |
254 | "style function", | |
255 | &style_set_list, | |
256 | &style_show_list); | |
257 | } |