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