2 // Automated Testing Framework (atf)
4 // Copyright (c) 2007 The NetBSD Foundation, Inc.
5 // All rights reserved.
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
10 // 1. Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // 2. Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
16 // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/ioctl.h>
45 namespace impl
= atf::ui
;
46 #define IMPL_NAME "atf::ui"
52 static bool done
= false;
53 static size_t width
= 0;
56 if (atf::env::has("COLUMNS")) {
57 const std::string cols
= atf::env::get("COLUMNS");
58 if (cols
.length() > 0) {
59 width
= atf::text::to_type
< size_t >(cols
);
63 if (ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
) != -1)
78 format_paragraph(const std::string
& text
,
79 const std::string
& tag
,
84 PRE(text
.find('\n') == std::string::npos
);
86 const std::string
pad(col
- tag
.length(), ' ');
87 const std::string
fullpad(col
, ' ');
89 std::string formatted
;
91 formatted
= tag
+ pad
;
94 INV(formatted
.length() == col
);
97 const size_t maxcol
= terminal_width();
99 std::vector
< std::string
> words
= atf::text::split(text
, " ");
100 for (std::vector
< std::string
>::const_iterator iter
= words
.begin();
101 iter
!= words
.end(); iter
++) {
102 const std::string
& word
= *iter
;
104 if (iter
!= words
.begin() && maxcol
> 0 &&
105 curcol
+ word
.length() + 1 > maxcol
) {
107 formatted
+= '\n' + tag
+ pad
;
109 formatted
+= '\n' + fullpad
;
111 } else if (iter
!= words
.begin()) {
117 curcol
+= word
.length();
124 impl::format_error(const std::string
& prog_name
, const std::string
& error
)
126 return format_text_with_tag("ERROR: " + error
, prog_name
+ ": ", true);
130 impl::format_info(const std::string
& prog_name
, const std::string
& msg
)
132 return format_text_with_tag(msg
, prog_name
+ ": ", true);
136 impl::format_text(const std::string
& text
)
138 return format_text_with_tag(text
, "", false, 0);
142 impl::format_text_with_tag(const std::string
& text
, const std::string
& tag
,
143 bool repeat
, size_t col
)
145 PRE(col
== 0 || col
>= tag
.length());
149 std::string formatted
;
151 std::vector
< std::string
> lines
= atf::text::split(text
, "\n");
152 for (std::vector
< std::string
>::const_iterator iter
= lines
.begin();
153 iter
!= lines
.end(); iter
++) {
154 const std::string
& line
= *iter
;
156 formatted
+= format_paragraph(line
, tag
, iter
== lines
.begin(),
158 if (iter
+ 1 != lines
.end()) {
160 formatted
+= "\n" + tag
+ "\n";
170 impl::format_warning(const std::string
& prog_name
, const std::string
& error
)
172 return format_text_with_tag("WARNING: " + error
, prog_name
+ ": ", true);