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>
44 namespace impl
= tools::ui
;
45 #define IMPL_NAME "tools::ui"
51 static bool done
= false;
52 static size_t width
= 0;
55 if (tools::env::has("COLUMNS")) {
56 const std::string cols
= tools::env::get("COLUMNS");
57 if (cols
.length() > 0) {
58 width
= tools::text::to_type
< size_t >(cols
);
62 if (ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
) != -1)
77 format_paragraph(const std::string
& text
,
78 const std::string
& tag
,
83 assert(text
.find('\n') == std::string::npos
);
85 const std::string
pad(col
- tag
.length(), ' ');
86 const std::string
fullpad(col
, ' ');
88 std::string formatted
;
90 formatted
= tag
+ pad
;
93 assert(formatted
.length() == col
);
96 const size_t maxcol
= terminal_width();
98 std::vector
< std::string
> words
= tools::text::split(text
, " ");
99 for (std::vector
< std::string
>::const_iterator iter
= words
.begin();
100 iter
!= words
.end(); iter
++) {
101 const std::string
& word
= *iter
;
103 if (iter
!= words
.begin() && maxcol
> 0 &&
104 curcol
+ word
.length() + 1 > maxcol
) {
106 formatted
+= '\n' + tag
+ pad
;
108 formatted
+= '\n' + fullpad
;
110 } else if (iter
!= words
.begin()) {
116 curcol
+= word
.length();
123 impl::format_error(const std::string
& prog_name
, const std::string
& error
)
125 return format_text_with_tag("ERROR: " + error
, prog_name
+ ": ", true);
129 impl::format_info(const std::string
& prog_name
, const std::string
& msg
)
131 return format_text_with_tag(msg
, prog_name
+ ": ", true);
135 impl::format_text(const std::string
& text
)
137 return format_text_with_tag(text
, "", false, 0);
141 impl::format_text_with_tag(const std::string
& text
, const std::string
& tag
,
142 bool repeat
, size_t col
)
144 assert(col
== 0 || col
>= tag
.length());
148 std::string formatted
;
150 std::vector
< std::string
> lines
= tools::text::split(text
, "\n");
151 for (std::vector
< std::string
>::const_iterator iter
= lines
.begin();
152 iter
!= lines
.end(); iter
++) {
153 const std::string
& line
= *iter
;
155 formatted
+= format_paragraph(line
, tag
, iter
== lines
.begin(),
157 if (iter
+ 1 != lines
.end()) {
159 formatted
+= "\n" + tag
+ "\n";
169 impl::format_warning(const std::string
& prog_name
, const std::string
& error
)
171 return format_text_with_tag("WARNING: " + error
, prog_name
+ ": ", true);