1*0a6a1f1dSLionel Sambuc //
2*0a6a1f1dSLionel Sambuc // Automated Testing Framework (atf)
3*0a6a1f1dSLionel Sambuc //
4*0a6a1f1dSLionel Sambuc // Copyright (c) 2007 The NetBSD Foundation, Inc.
5*0a6a1f1dSLionel Sambuc // All rights reserved.
6*0a6a1f1dSLionel Sambuc //
7*0a6a1f1dSLionel Sambuc // Redistribution and use in source and binary forms, with or without
8*0a6a1f1dSLionel Sambuc // modification, are permitted provided that the following conditions
9*0a6a1f1dSLionel Sambuc // are met:
10*0a6a1f1dSLionel Sambuc // 1. Redistributions of source code must retain the above copyright
11*0a6a1f1dSLionel Sambuc // notice, this list of conditions and the following disclaimer.
12*0a6a1f1dSLionel Sambuc // 2. Redistributions in binary form must reproduce the above copyright
13*0a6a1f1dSLionel Sambuc // notice, this list of conditions and the following disclaimer in the
14*0a6a1f1dSLionel Sambuc // documentation and/or other materials provided with the distribution.
15*0a6a1f1dSLionel Sambuc //
16*0a6a1f1dSLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17*0a6a1f1dSLionel Sambuc // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18*0a6a1f1dSLionel Sambuc // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19*0a6a1f1dSLionel Sambuc // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*0a6a1f1dSLionel Sambuc // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21*0a6a1f1dSLionel Sambuc // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*0a6a1f1dSLionel Sambuc // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23*0a6a1f1dSLionel Sambuc // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*0a6a1f1dSLionel Sambuc // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25*0a6a1f1dSLionel Sambuc // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26*0a6a1f1dSLionel Sambuc // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27*0a6a1f1dSLionel Sambuc // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*0a6a1f1dSLionel Sambuc //
29*0a6a1f1dSLionel Sambuc
30*0a6a1f1dSLionel Sambuc extern "C" {
31*0a6a1f1dSLionel Sambuc #include <sys/ioctl.h>
32*0a6a1f1dSLionel Sambuc
33*0a6a1f1dSLionel Sambuc #include <termios.h>
34*0a6a1f1dSLionel Sambuc #include <unistd.h>
35*0a6a1f1dSLionel Sambuc }
36*0a6a1f1dSLionel Sambuc
37*0a6a1f1dSLionel Sambuc #include <cassert>
38*0a6a1f1dSLionel Sambuc #include <sstream>
39*0a6a1f1dSLionel Sambuc
40*0a6a1f1dSLionel Sambuc #include "env.hpp"
41*0a6a1f1dSLionel Sambuc #include "text.hpp"
42*0a6a1f1dSLionel Sambuc #include "ui.hpp"
43*0a6a1f1dSLionel Sambuc
44*0a6a1f1dSLionel Sambuc namespace impl = tools::ui;
45*0a6a1f1dSLionel Sambuc #define IMPL_NAME "tools::ui"
46*0a6a1f1dSLionel Sambuc
47*0a6a1f1dSLionel Sambuc static
48*0a6a1f1dSLionel Sambuc size_t
terminal_width(void)49*0a6a1f1dSLionel Sambuc terminal_width(void)
50*0a6a1f1dSLionel Sambuc {
51*0a6a1f1dSLionel Sambuc static bool done = false;
52*0a6a1f1dSLionel Sambuc static size_t width = 0;
53*0a6a1f1dSLionel Sambuc
54*0a6a1f1dSLionel Sambuc if (!done) {
55*0a6a1f1dSLionel Sambuc if (tools::env::has("COLUMNS")) {
56*0a6a1f1dSLionel Sambuc const std::string cols = tools::env::get("COLUMNS");
57*0a6a1f1dSLionel Sambuc if (cols.length() > 0) {
58*0a6a1f1dSLionel Sambuc width = tools::text::to_type< size_t >(cols);
59*0a6a1f1dSLionel Sambuc }
60*0a6a1f1dSLionel Sambuc } else {
61*0a6a1f1dSLionel Sambuc struct winsize ws;
62*0a6a1f1dSLionel Sambuc if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1)
63*0a6a1f1dSLionel Sambuc width = ws.ws_col;
64*0a6a1f1dSLionel Sambuc }
65*0a6a1f1dSLionel Sambuc
66*0a6a1f1dSLionel Sambuc if (width >= 80)
67*0a6a1f1dSLionel Sambuc width -= 5;
68*0a6a1f1dSLionel Sambuc
69*0a6a1f1dSLionel Sambuc done = true;
70*0a6a1f1dSLionel Sambuc }
71*0a6a1f1dSLionel Sambuc
72*0a6a1f1dSLionel Sambuc return width;
73*0a6a1f1dSLionel Sambuc }
74*0a6a1f1dSLionel Sambuc
75*0a6a1f1dSLionel Sambuc static
76*0a6a1f1dSLionel Sambuc std::string
format_paragraph(const std::string & text,const std::string & tag,const bool first,const bool repeat,const size_t col)77*0a6a1f1dSLionel Sambuc format_paragraph(const std::string& text,
78*0a6a1f1dSLionel Sambuc const std::string& tag,
79*0a6a1f1dSLionel Sambuc const bool first,
80*0a6a1f1dSLionel Sambuc const bool repeat,
81*0a6a1f1dSLionel Sambuc const size_t col)
82*0a6a1f1dSLionel Sambuc {
83*0a6a1f1dSLionel Sambuc assert(text.find('\n') == std::string::npos);
84*0a6a1f1dSLionel Sambuc
85*0a6a1f1dSLionel Sambuc const std::string pad(col - tag.length(), ' ');
86*0a6a1f1dSLionel Sambuc const std::string fullpad(col, ' ');
87*0a6a1f1dSLionel Sambuc
88*0a6a1f1dSLionel Sambuc std::string formatted;
89*0a6a1f1dSLionel Sambuc if (first || repeat)
90*0a6a1f1dSLionel Sambuc formatted = tag + pad;
91*0a6a1f1dSLionel Sambuc else
92*0a6a1f1dSLionel Sambuc formatted = fullpad;
93*0a6a1f1dSLionel Sambuc assert(formatted.length() == col);
94*0a6a1f1dSLionel Sambuc size_t curcol = col;
95*0a6a1f1dSLionel Sambuc
96*0a6a1f1dSLionel Sambuc const size_t maxcol = terminal_width();
97*0a6a1f1dSLionel Sambuc
98*0a6a1f1dSLionel Sambuc std::vector< std::string > words = tools::text::split(text, " ");
99*0a6a1f1dSLionel Sambuc for (std::vector< std::string >::const_iterator iter = words.begin();
100*0a6a1f1dSLionel Sambuc iter != words.end(); iter++) {
101*0a6a1f1dSLionel Sambuc const std::string& word = *iter;
102*0a6a1f1dSLionel Sambuc
103*0a6a1f1dSLionel Sambuc if (iter != words.begin() && maxcol > 0 &&
104*0a6a1f1dSLionel Sambuc curcol + word.length() + 1 > maxcol) {
105*0a6a1f1dSLionel Sambuc if (repeat)
106*0a6a1f1dSLionel Sambuc formatted += '\n' + tag + pad;
107*0a6a1f1dSLionel Sambuc else
108*0a6a1f1dSLionel Sambuc formatted += '\n' + fullpad;
109*0a6a1f1dSLionel Sambuc curcol = col;
110*0a6a1f1dSLionel Sambuc } else if (iter != words.begin()) {
111*0a6a1f1dSLionel Sambuc formatted += ' ';
112*0a6a1f1dSLionel Sambuc curcol++;
113*0a6a1f1dSLionel Sambuc }
114*0a6a1f1dSLionel Sambuc
115*0a6a1f1dSLionel Sambuc formatted += word;
116*0a6a1f1dSLionel Sambuc curcol += word.length();
117*0a6a1f1dSLionel Sambuc }
118*0a6a1f1dSLionel Sambuc
119*0a6a1f1dSLionel Sambuc return formatted;
120*0a6a1f1dSLionel Sambuc }
121*0a6a1f1dSLionel Sambuc
122*0a6a1f1dSLionel Sambuc std::string
format_error(const std::string & prog_name,const std::string & error)123*0a6a1f1dSLionel Sambuc impl::format_error(const std::string& prog_name, const std::string& error)
124*0a6a1f1dSLionel Sambuc {
125*0a6a1f1dSLionel Sambuc return format_text_with_tag("ERROR: " + error, prog_name + ": ", true);
126*0a6a1f1dSLionel Sambuc }
127*0a6a1f1dSLionel Sambuc
128*0a6a1f1dSLionel Sambuc std::string
format_info(const std::string & prog_name,const std::string & msg)129*0a6a1f1dSLionel Sambuc impl::format_info(const std::string& prog_name, const std::string& msg)
130*0a6a1f1dSLionel Sambuc {
131*0a6a1f1dSLionel Sambuc return format_text_with_tag(msg, prog_name + ": ", true);
132*0a6a1f1dSLionel Sambuc }
133*0a6a1f1dSLionel Sambuc
134*0a6a1f1dSLionel Sambuc std::string
format_text(const std::string & text)135*0a6a1f1dSLionel Sambuc impl::format_text(const std::string& text)
136*0a6a1f1dSLionel Sambuc {
137*0a6a1f1dSLionel Sambuc return format_text_with_tag(text, "", false, 0);
138*0a6a1f1dSLionel Sambuc }
139*0a6a1f1dSLionel Sambuc
140*0a6a1f1dSLionel Sambuc std::string
format_text_with_tag(const std::string & text,const std::string & tag,bool repeat,size_t col)141*0a6a1f1dSLionel Sambuc impl::format_text_with_tag(const std::string& text, const std::string& tag,
142*0a6a1f1dSLionel Sambuc bool repeat, size_t col)
143*0a6a1f1dSLionel Sambuc {
144*0a6a1f1dSLionel Sambuc assert(col == 0 || col >= tag.length());
145*0a6a1f1dSLionel Sambuc if (col == 0)
146*0a6a1f1dSLionel Sambuc col = tag.length();
147*0a6a1f1dSLionel Sambuc
148*0a6a1f1dSLionel Sambuc std::string formatted;
149*0a6a1f1dSLionel Sambuc
150*0a6a1f1dSLionel Sambuc std::vector< std::string > lines = tools::text::split(text, "\n");
151*0a6a1f1dSLionel Sambuc for (std::vector< std::string >::const_iterator iter = lines.begin();
152*0a6a1f1dSLionel Sambuc iter != lines.end(); iter++) {
153*0a6a1f1dSLionel Sambuc const std::string& line = *iter;
154*0a6a1f1dSLionel Sambuc
155*0a6a1f1dSLionel Sambuc formatted += format_paragraph(line, tag, iter == lines.begin(),
156*0a6a1f1dSLionel Sambuc repeat, col);
157*0a6a1f1dSLionel Sambuc if (iter + 1 != lines.end()) {
158*0a6a1f1dSLionel Sambuc if (repeat)
159*0a6a1f1dSLionel Sambuc formatted += "\n" + tag + "\n";
160*0a6a1f1dSLionel Sambuc else
161*0a6a1f1dSLionel Sambuc formatted += "\n\n";
162*0a6a1f1dSLionel Sambuc }
163*0a6a1f1dSLionel Sambuc }
164*0a6a1f1dSLionel Sambuc
165*0a6a1f1dSLionel Sambuc return formatted;
166*0a6a1f1dSLionel Sambuc }
167*0a6a1f1dSLionel Sambuc
168*0a6a1f1dSLionel Sambuc std::string
format_warning(const std::string & prog_name,const std::string & error)169*0a6a1f1dSLionel Sambuc impl::format_warning(const std::string& prog_name, const std::string& error)
170*0a6a1f1dSLionel Sambuc {
171*0a6a1f1dSLionel Sambuc return format_text_with_tag("WARNING: " + error, prog_name + ": ", true);
172*0a6a1f1dSLionel Sambuc }
173