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