1 // Copyright 2011 The Kyua Authors.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "utils/cmdline/ui.hpp"
31 #if defined(HAVE_CONFIG_H)
36 #include <sys/param.h>
37 #include <sys/ioctl.h>
39 #if defined(HAVE_TERMIOS_H)
47 #include "utils/cmdline/globals.hpp"
48 #include "utils/env.hpp"
49 #include "utils/format/macros.hpp"
50 #include "utils/fs/path.hpp"
51 #include "utils/logging/macros.hpp"
52 #include "utils/optional.ipp"
53 #include "utils/text/operations.ipp"
54 #include "utils/text/table.hpp"
56 namespace cmdline = utils::cmdline;
57 namespace text = utils::text;
60 using utils::optional;
63 /// Destructor for the class.
64 cmdline::ui::~ui(void)
69 /// Writes a single line to stderr.
71 /// The written line is printed as is, without being wrapped to fit within the
72 /// screen width. If the caller wants to print more than one line, it shall
73 /// invoke this function once per line.
75 /// \param message The line to print. Should not include a trailing newline
77 /// \param newline Whether to append a newline to the message or not.
79 cmdline::ui::err(const std::string& message, const bool newline)
81 LI(F("stderr: %s") % message);
83 std::cerr << message << "\n";
91 /// Writes a single line to stdout.
93 /// The written line is printed as is, without being wrapped to fit within the
94 /// screen width. If the caller wants to print more than one line, it shall
95 /// invoke this function once per line.
97 /// \param message The line to print. Should not include a trailing newline
99 /// \param newline Whether to append a newline to the message or not.
101 cmdline::ui::out(const std::string& message, const bool newline)
103 LI(F("stdout: %s") % message);
105 std::cout << message << "\n";
107 std::cout << message;
113 /// Queries the width of the screen.
115 /// This information comes first from the COLUMNS environment variable. If not
116 /// present or invalid, and if the stdout of the current process is connected to
117 /// a terminal the width is deduced from the terminal itself. Ultimately, if
118 /// all fails, none is returned. This function shall not raise any errors.
120 /// Be aware that the results of this query are cached during execution.
121 /// Subsequent calls to this function will always return the same value even if
122 /// the terminal size has actually changed.
124 /// \todo Install a signal handler for SIGWINCH so that we can readjust our
125 /// knowledge of the terminal width when the user resizes the window.
127 /// \return The width of the screen if it was possible to determine it, or none
129 optional< std::size_t >
130 cmdline::ui::screen_width(void) const
132 static bool done = false;
133 static optional< std::size_t > width = none;
136 const optional< std::string > columns = utils::getenv("COLUMNS");
138 if (columns.get().length() > 0) {
140 width = utils::make_optional(
141 utils::text::to_type< std::size_t >(columns.get()));
142 } catch (const utils::text::value_error& e) {
143 LD(F("Ignoring invalid value in COLUMNS variable: %s") %
150 if (::ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1)
151 width = optional< std::size_t >(ws.ws_col);
154 if (width && width.get() >= 80)
164 /// Writes a line to stdout.
166 /// The line is wrapped to fit on screen.
168 /// \param message The line to print, without the trailing newline character.
170 cmdline::ui::out_wrap(const std::string& message)
172 const optional< std::size_t > max_width = screen_width();
174 const std::vector< std::string > lines = text::refill(
175 message, max_width.get());
176 for (std::vector< std::string >::const_iterator iter = lines.begin();
177 iter != lines.end(); iter++)
184 /// Writes a line to stdout with a leading tag.
186 /// If the line does not fit on the current screen width, the line is broken
187 /// into pieces and the tag is repeated on every line.
189 /// \param tag The leading line tag.
190 /// \param message The message to be printed, without the trailing newline
192 /// \param repeat If true, print the tag on every line; otherwise, indent the
193 /// text of all lines to match the width of the tag on the first line.
195 cmdline::ui::out_tag_wrap(const std::string& tag, const std::string& message,
198 const optional< std::size_t > max_width = screen_width();
199 if (max_width && max_width.get() > tag.length()) {
200 const std::vector< std::string > lines = text::refill(
201 message, max_width.get() - tag.length());
202 for (std::vector< std::string >::const_iterator iter = lines.begin();
203 iter != lines.end(); iter++) {
204 if (repeat || iter == lines.begin())
205 out(F("%s%s") % tag % *iter);
207 out(F("%s%s") % std::string(tag.length(), ' ') % *iter);
210 out(F("%s%s") % tag % message);
215 /// Writes a table to stdout.
217 /// \param table The table to write.
218 /// \param formatter The table formatter to use to convert the table to a
219 /// console representation.
220 /// \param prefix Text to prepend to all the lines of the output table.
222 cmdline::ui::out_table(const text::table& table,
223 text::table_formatter formatter,
224 const std::string& prefix)
229 const optional< std::size_t > max_width = screen_width();
231 formatter.set_table_width(max_width.get() - prefix.length());
233 const std::vector< std::string > lines = formatter.format(table);
234 for (std::vector< std::string >::const_iterator iter = lines.begin();
235 iter != lines.end(); ++iter)
240 /// Formats and prints an error message.
242 /// \param ui_ The user interface object used to print the message.
243 /// \param message The message to print. Should not end with a newline
246 cmdline::print_error(ui* ui_, const std::string& message)
249 ui_->err(F("%s: E: %s") % cmdline::progname() % message);
253 /// Formats and prints an informational message.
255 /// \param ui_ The user interface object used to print the message.
256 /// \param message The message to print. Should not end with a newline
259 cmdline::print_info(ui* ui_, const std::string& message)
262 ui_->err(F("%s: I: %s") % cmdline::progname() % message);
266 /// Formats and prints a warning message.
268 /// \param ui_ The user interface object used to print the message.
269 /// \param message The message to print. Should not end with a newline
272 cmdline::print_warning(ui* ui_, const std::string& message)
275 ui_->err(F("%s: W: %s") % cmdline::progname() % message);