ui.cpp revision 1.1.1.1.12.2 1 1.1.1.1.12.2 yamt // Copyright 2011 Google Inc.
2 1.1.1.1.12.2 yamt // All rights reserved.
3 1.1.1.1.12.2 yamt //
4 1.1.1.1.12.2 yamt // Redistribution and use in source and binary forms, with or without
5 1.1.1.1.12.2 yamt // modification, are permitted provided that the following conditions are
6 1.1.1.1.12.2 yamt // met:
7 1.1.1.1.12.2 yamt //
8 1.1.1.1.12.2 yamt // * Redistributions of source code must retain the above copyright
9 1.1.1.1.12.2 yamt // notice, this list of conditions and the following disclaimer.
10 1.1.1.1.12.2 yamt // * Redistributions in binary form must reproduce the above copyright
11 1.1.1.1.12.2 yamt // notice, this list of conditions and the following disclaimer in the
12 1.1.1.1.12.2 yamt // documentation and/or other materials provided with the distribution.
13 1.1.1.1.12.2 yamt // * Neither the name of Google Inc. nor the names of its contributors
14 1.1.1.1.12.2 yamt // may be used to endorse or promote products derived from this software
15 1.1.1.1.12.2 yamt // without specific prior written permission.
16 1.1.1.1.12.2 yamt //
17 1.1.1.1.12.2 yamt // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 1.1.1.1.12.2 yamt // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 1.1.1.1.12.2 yamt // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 1.1.1.1.12.2 yamt // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 1.1.1.1.12.2 yamt // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 1.1.1.1.12.2 yamt // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 1.1.1.1.12.2 yamt // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 1.1.1.1.12.2 yamt // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 1.1.1.1.12.2 yamt // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 1.1.1.1.12.2 yamt // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 1.1.1.1.12.2 yamt // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.1.1.1.12.2 yamt
29 1.1.1.1.12.2 yamt #include "utils/cmdline/ui.hpp"
30 1.1.1.1.12.2 yamt
31 1.1.1.1.12.2 yamt extern "C" {
32 1.1.1.1.12.2 yamt #include <sys/ioctl.h>
33 1.1.1.1.12.2 yamt
34 1.1.1.1.12.2 yamt #include <unistd.h>
35 1.1.1.1.12.2 yamt }
36 1.1.1.1.12.2 yamt
37 1.1.1.1.12.2 yamt #include <iostream>
38 1.1.1.1.12.2 yamt
39 1.1.1.1.12.2 yamt #include "utils/cmdline/globals.hpp"
40 1.1.1.1.12.2 yamt #include "utils/env.hpp"
41 1.1.1.1.12.2 yamt #include "utils/format/macros.hpp"
42 1.1.1.1.12.2 yamt #include "utils/fs/path.hpp"
43 1.1.1.1.12.2 yamt #include "utils/logging/macros.hpp"
44 1.1.1.1.12.2 yamt #include "utils/optional.ipp"
45 1.1.1.1.12.2 yamt #include "utils/text/operations.ipp"
46 1.1.1.1.12.2 yamt #include "utils/sanity.hpp"
47 1.1.1.1.12.2 yamt #include "utils/text/table.hpp"
48 1.1.1.1.12.2 yamt
49 1.1.1.1.12.2 yamt namespace cmdline = utils::cmdline;
50 1.1.1.1.12.2 yamt namespace text = utils::text;
51 1.1.1.1.12.2 yamt
52 1.1.1.1.12.2 yamt using utils::none;
53 1.1.1.1.12.2 yamt using utils::optional;
54 1.1.1.1.12.2 yamt
55 1.1.1.1.12.2 yamt
56 1.1.1.1.12.2 yamt /// Destructor for the class.
57 1.1.1.1.12.2 yamt cmdline::ui::~ui(void)
58 1.1.1.1.12.2 yamt {
59 1.1.1.1.12.2 yamt }
60 1.1.1.1.12.2 yamt
61 1.1.1.1.12.2 yamt
62 1.1.1.1.12.2 yamt /// Writes a line to stderr.
63 1.1.1.1.12.2 yamt ///
64 1.1.1.1.12.2 yamt /// The written line is printed as is, without being wrapped to fit within the
65 1.1.1.1.12.2 yamt /// screen width.
66 1.1.1.1.12.2 yamt ///
67 1.1.1.1.12.2 yamt /// \param message The line to print, without the trailing newline character.
68 1.1.1.1.12.2 yamt /// \param newline Whether to append a newline to the message or not.
69 1.1.1.1.12.2 yamt void
70 1.1.1.1.12.2 yamt cmdline::ui::err(const std::string& message, const bool newline)
71 1.1.1.1.12.2 yamt {
72 1.1.1.1.12.2 yamt PRE(message.empty() || message[message.length() - 1] != '\n');
73 1.1.1.1.12.2 yamt LI(F("stderr: %s") % message);
74 1.1.1.1.12.2 yamt if (newline)
75 1.1.1.1.12.2 yamt std::cerr << message << "\n";
76 1.1.1.1.12.2 yamt else {
77 1.1.1.1.12.2 yamt std::cerr << message;
78 1.1.1.1.12.2 yamt std::cerr.flush();
79 1.1.1.1.12.2 yamt }
80 1.1.1.1.12.2 yamt }
81 1.1.1.1.12.2 yamt
82 1.1.1.1.12.2 yamt
83 1.1.1.1.12.2 yamt /// Writes a line to stdout.
84 1.1.1.1.12.2 yamt ///
85 1.1.1.1.12.2 yamt /// The written line is printed as is, without being wrapped to fit within the
86 1.1.1.1.12.2 yamt /// screen width.
87 1.1.1.1.12.2 yamt ///
88 1.1.1.1.12.2 yamt /// \param message The line to print, without the trailing newline character.
89 1.1.1.1.12.2 yamt /// \param newline Whether to append a newline to the message or not.
90 1.1.1.1.12.2 yamt void
91 1.1.1.1.12.2 yamt cmdline::ui::out(const std::string& message, const bool newline)
92 1.1.1.1.12.2 yamt {
93 1.1.1.1.12.2 yamt PRE(message.empty() || message[message.length() - 1] != '\n');
94 1.1.1.1.12.2 yamt LI(F("stdout: %s") % message);
95 1.1.1.1.12.2 yamt if (newline)
96 1.1.1.1.12.2 yamt std::cout << message << "\n";
97 1.1.1.1.12.2 yamt else {
98 1.1.1.1.12.2 yamt std::cout << message;
99 1.1.1.1.12.2 yamt std::cout.flush();
100 1.1.1.1.12.2 yamt }
101 1.1.1.1.12.2 yamt }
102 1.1.1.1.12.2 yamt
103 1.1.1.1.12.2 yamt
104 1.1.1.1.12.2 yamt /// Queries the width of the screen.
105 1.1.1.1.12.2 yamt ///
106 1.1.1.1.12.2 yamt /// This information comes first from the COLUMNS environment variable. If not
107 1.1.1.1.12.2 yamt /// present or invalid, and if the stdout of the current process is connected to
108 1.1.1.1.12.2 yamt /// a terminal the width is deduced from the terminal itself. Ultimately, if
109 1.1.1.1.12.2 yamt /// all fails, none is returned. This function shall not raise any errors.
110 1.1.1.1.12.2 yamt ///
111 1.1.1.1.12.2 yamt /// Be aware that the results of this query are cached during execution.
112 1.1.1.1.12.2 yamt /// Subsequent calls to this function will always return the same value even if
113 1.1.1.1.12.2 yamt /// the terminal size has actually changed.
114 1.1.1.1.12.2 yamt ///
115 1.1.1.1.12.2 yamt /// \todo Install a signal handler for SIGWINCH so that we can readjust our
116 1.1.1.1.12.2 yamt /// knowledge of the terminal width when the user resizes the window.
117 1.1.1.1.12.2 yamt ///
118 1.1.1.1.12.2 yamt /// \return The width of the screen if it was possible to determine it, or none
119 1.1.1.1.12.2 yamt /// otherwise.
120 1.1.1.1.12.2 yamt optional< std::size_t >
121 1.1.1.1.12.2 yamt cmdline::ui::screen_width(void) const
122 1.1.1.1.12.2 yamt {
123 1.1.1.1.12.2 yamt static bool done = false;
124 1.1.1.1.12.2 yamt static optional< std::size_t > width = none;
125 1.1.1.1.12.2 yamt
126 1.1.1.1.12.2 yamt if (!done) {
127 1.1.1.1.12.2 yamt const optional< std::string > columns = utils::getenv("COLUMNS");
128 1.1.1.1.12.2 yamt if (columns) {
129 1.1.1.1.12.2 yamt if (columns.get().length() > 0) {
130 1.1.1.1.12.2 yamt try {
131 1.1.1.1.12.2 yamt width = utils::make_optional(
132 1.1.1.1.12.2 yamt utils::text::to_type< std::size_t >(columns.get()));
133 1.1.1.1.12.2 yamt } catch (const utils::text::value_error& e) {
134 1.1.1.1.12.2 yamt LD(F("Ignoring invalid value in COLUMNS variable: %s") %
135 1.1.1.1.12.2 yamt e.what());
136 1.1.1.1.12.2 yamt }
137 1.1.1.1.12.2 yamt }
138 1.1.1.1.12.2 yamt }
139 1.1.1.1.12.2 yamt if (!width) {
140 1.1.1.1.12.2 yamt struct ::winsize ws;
141 1.1.1.1.12.2 yamt if (::ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1)
142 1.1.1.1.12.2 yamt width = optional< std::size_t >(ws.ws_col);
143 1.1.1.1.12.2 yamt }
144 1.1.1.1.12.2 yamt
145 1.1.1.1.12.2 yamt if (width && width.get() >= 80)
146 1.1.1.1.12.2 yamt width.get() -= 5;
147 1.1.1.1.12.2 yamt
148 1.1.1.1.12.2 yamt done = true;
149 1.1.1.1.12.2 yamt }
150 1.1.1.1.12.2 yamt
151 1.1.1.1.12.2 yamt return width;
152 1.1.1.1.12.2 yamt }
153 1.1.1.1.12.2 yamt
154 1.1.1.1.12.2 yamt
155 1.1.1.1.12.2 yamt /// Writes a line to stdout.
156 1.1.1.1.12.2 yamt ///
157 1.1.1.1.12.2 yamt /// The line is wrapped to fit on screen.
158 1.1.1.1.12.2 yamt ///
159 1.1.1.1.12.2 yamt /// \param message The line to print, without the trailing newline character.
160 1.1.1.1.12.2 yamt void
161 1.1.1.1.12.2 yamt cmdline::ui::out_wrap(const std::string& message)
162 1.1.1.1.12.2 yamt {
163 1.1.1.1.12.2 yamt const optional< std::size_t > max_width = screen_width();
164 1.1.1.1.12.2 yamt if (max_width) {
165 1.1.1.1.12.2 yamt const std::vector< std::string > lines = text::refill(
166 1.1.1.1.12.2 yamt message, max_width.get());
167 1.1.1.1.12.2 yamt for (std::vector< std::string >::const_iterator iter = lines.begin();
168 1.1.1.1.12.2 yamt iter != lines.end(); iter++)
169 1.1.1.1.12.2 yamt out(*iter);
170 1.1.1.1.12.2 yamt } else
171 1.1.1.1.12.2 yamt out(message);
172 1.1.1.1.12.2 yamt }
173 1.1.1.1.12.2 yamt
174 1.1.1.1.12.2 yamt
175 1.1.1.1.12.2 yamt /// Writes a line to stdout with a leading tag.
176 1.1.1.1.12.2 yamt ///
177 1.1.1.1.12.2 yamt /// If the line does not fit on the current screen width, the line is broken
178 1.1.1.1.12.2 yamt /// into pieces and the tag is repeated on every line.
179 1.1.1.1.12.2 yamt ///
180 1.1.1.1.12.2 yamt /// \param tag The leading line tag.
181 1.1.1.1.12.2 yamt /// \param message The message to be printed, without the trailing newline
182 1.1.1.1.12.2 yamt /// character.
183 1.1.1.1.12.2 yamt /// \param repeat If true, print the tag on every line; otherwise, indent the
184 1.1.1.1.12.2 yamt /// text of all lines to match the width of the tag on the first line.
185 1.1.1.1.12.2 yamt void
186 1.1.1.1.12.2 yamt cmdline::ui::out_tag_wrap(const std::string& tag, const std::string& message,
187 1.1.1.1.12.2 yamt const bool repeat)
188 1.1.1.1.12.2 yamt {
189 1.1.1.1.12.2 yamt const optional< std::size_t > max_width = screen_width();
190 1.1.1.1.12.2 yamt if (max_width && max_width.get() > tag.length()) {
191 1.1.1.1.12.2 yamt const std::vector< std::string > lines = text::refill(
192 1.1.1.1.12.2 yamt message, max_width.get() - tag.length());
193 1.1.1.1.12.2 yamt for (std::vector< std::string >::const_iterator iter = lines.begin();
194 1.1.1.1.12.2 yamt iter != lines.end(); iter++) {
195 1.1.1.1.12.2 yamt if (repeat || iter == lines.begin())
196 1.1.1.1.12.2 yamt out(F("%s%s") % tag % *iter);
197 1.1.1.1.12.2 yamt else
198 1.1.1.1.12.2 yamt out(F("%s%s") % std::string(tag.length(), ' ') % *iter);
199 1.1.1.1.12.2 yamt }
200 1.1.1.1.12.2 yamt } else {
201 1.1.1.1.12.2 yamt out(F("%s%s") % tag % message);
202 1.1.1.1.12.2 yamt }
203 1.1.1.1.12.2 yamt }
204 1.1.1.1.12.2 yamt
205 1.1.1.1.12.2 yamt
206 1.1.1.1.12.2 yamt /// Writes a table to stdout.
207 1.1.1.1.12.2 yamt ///
208 1.1.1.1.12.2 yamt /// \param table The table to write.
209 1.1.1.1.12.2 yamt /// \param formatter The table formatter to use to convert the table to a
210 1.1.1.1.12.2 yamt /// console representation.
211 1.1.1.1.12.2 yamt /// \param prefix Text to prepend to all the lines of the output table.
212 1.1.1.1.12.2 yamt void
213 1.1.1.1.12.2 yamt cmdline::ui::out_table(const text::table& table,
214 1.1.1.1.12.2 yamt text::table_formatter formatter,
215 1.1.1.1.12.2 yamt const std::string& prefix)
216 1.1.1.1.12.2 yamt {
217 1.1.1.1.12.2 yamt if (table.empty())
218 1.1.1.1.12.2 yamt return;
219 1.1.1.1.12.2 yamt
220 1.1.1.1.12.2 yamt const optional< std::size_t > max_width = screen_width();
221 1.1.1.1.12.2 yamt if (max_width)
222 1.1.1.1.12.2 yamt formatter.set_table_width(max_width.get() - prefix.length());
223 1.1.1.1.12.2 yamt
224 1.1.1.1.12.2 yamt const std::vector< std::string > lines = formatter.format(table);
225 1.1.1.1.12.2 yamt for (std::vector< std::string >::const_iterator iter = lines.begin();
226 1.1.1.1.12.2 yamt iter != lines.end(); ++iter)
227 1.1.1.1.12.2 yamt out(prefix + *iter);
228 1.1.1.1.12.2 yamt }
229 1.1.1.1.12.2 yamt
230 1.1.1.1.12.2 yamt
231 1.1.1.1.12.2 yamt /// Formats and prints an error message.
232 1.1.1.1.12.2 yamt ///
233 1.1.1.1.12.2 yamt /// \param ui_ The user interface object used to print the message.
234 1.1.1.1.12.2 yamt /// \param message The message to print. Must not end with a dot nor with a
235 1.1.1.1.12.2 yamt /// newline character.
236 1.1.1.1.12.2 yamt void
237 1.1.1.1.12.2 yamt cmdline::print_error(ui* ui_, const std::string& message)
238 1.1.1.1.12.2 yamt {
239 1.1.1.1.12.2 yamt PRE(!message.empty() && message[message.length() - 1] != '.');
240 1.1.1.1.12.2 yamt LE(message);
241 1.1.1.1.12.2 yamt ui_->err(F("%s: E: %s.") % cmdline::progname() % message);
242 1.1.1.1.12.2 yamt }
243 1.1.1.1.12.2 yamt
244 1.1.1.1.12.2 yamt
245 1.1.1.1.12.2 yamt /// Formats and prints an informational message.
246 1.1.1.1.12.2 yamt ///
247 1.1.1.1.12.2 yamt /// \param ui_ The user interface object used to print the message.
248 1.1.1.1.12.2 yamt /// \param message The message to print. Must not end with a dot nor with a
249 1.1.1.1.12.2 yamt /// newline character.
250 1.1.1.1.12.2 yamt void
251 1.1.1.1.12.2 yamt cmdline::print_info(ui* ui_, const std::string& message)
252 1.1.1.1.12.2 yamt {
253 1.1.1.1.12.2 yamt PRE(!message.empty() && message[message.length() - 1] != '.');
254 1.1.1.1.12.2 yamt LI(message);
255 1.1.1.1.12.2 yamt ui_->err(F("%s: I: %s.") % cmdline::progname() % message);
256 1.1.1.1.12.2 yamt }
257 1.1.1.1.12.2 yamt
258 1.1.1.1.12.2 yamt
259 1.1.1.1.12.2 yamt /// Formats and prints a warning message.
260 1.1.1.1.12.2 yamt ///
261 1.1.1.1.12.2 yamt /// \param ui_ The user interface object used to print the message.
262 1.1.1.1.12.2 yamt /// \param message The message to print. Must not end with a dot nor with a
263 1.1.1.1.12.2 yamt /// newline character.
264 1.1.1.1.12.2 yamt void
265 1.1.1.1.12.2 yamt cmdline::print_warning(ui* ui_, const std::string& message)
266 1.1.1.1.12.2 yamt {
267 1.1.1.1.12.2 yamt PRE(!message.empty() && message[message.length() - 1] != '.');
268 1.1.1.1.12.2 yamt LW(message);
269 1.1.1.1.12.2 yamt ui_->err(F("%s: W: %s.") % cmdline::progname() % message);
270 1.1.1.1.12.2 yamt }
271