1 // 2 // Automated Testing Framework (atf) 3 // 4 // Copyright (c) 2007 The NetBSD Foundation, Inc. 5 // All rights reserved. 6 // 7 // Redistribution and use in source and binary forms, with or without 8 // modification, are permitted provided that the following conditions 9 // are met: 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. 15 // 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. 28 // 29 30 extern "C" { 31 #include <sys/ioctl.h> 32 33 #include <termios.h> 34 #include <unistd.h> 35 } 36 37 #include <sstream> 38 39 #include "env.hpp" 40 #include "text.hpp" 41 #include "sanity.hpp" 42 #include "text.hpp" 43 #include "ui.hpp" 44 45 namespace impl = atf::ui; 46 #define IMPL_NAME "atf::ui" 47 48 static 49 size_t 50 terminal_width(void) 51 { 52 static bool done = false; 53 static size_t width = 0; 54 55 if (!done) { 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); 60 } 61 } else { 62 struct winsize ws; 63 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1) 64 width = ws.ws_col; 65 } 66 67 if (width >= 80) 68 width -= 5; 69 70 done = true; 71 } 72 73 return width; 74 } 75 76 static 77 std::string 78 format_paragraph(const std::string& text, 79 const std::string& tag, 80 const bool first, 81 const bool repeat, 82 const size_t col) 83 { 84 PRE(text.find('\n') == std::string::npos); 85 86 const std::string pad(col - tag.length(), ' '); 87 const std::string fullpad(col, ' '); 88 89 std::string formatted; 90 if (first || repeat) 91 formatted = tag + pad; 92 else 93 formatted = fullpad; 94 INV(formatted.length() == col); 95 size_t curcol = col; 96 97 const size_t maxcol = terminal_width(); 98 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; 103 104 if (iter != words.begin() && maxcol > 0 && 105 curcol + word.length() + 1 > maxcol) { 106 if (repeat) 107 formatted += '\n' + tag + pad; 108 else 109 formatted += '\n' + fullpad; 110 curcol = col; 111 } else if (iter != words.begin()) { 112 formatted += ' '; 113 curcol++; 114 } 115 116 formatted += word; 117 curcol += word.length(); 118 } 119 120 return formatted; 121 } 122 123 std::string 124 impl::format_error(const std::string& prog_name, const std::string& error) 125 { 126 return format_text_with_tag("ERROR: " + error, prog_name + ": ", true); 127 } 128 129 std::string 130 impl::format_info(const std::string& prog_name, const std::string& msg) 131 { 132 return format_text_with_tag(msg, prog_name + ": ", true); 133 } 134 135 std::string 136 impl::format_text(const std::string& text) 137 { 138 return format_text_with_tag(text, "", false, 0); 139 } 140 141 std::string 142 impl::format_text_with_tag(const std::string& text, const std::string& tag, 143 bool repeat, size_t col) 144 { 145 PRE(col == 0 || col >= tag.length()); 146 if (col == 0) 147 col = tag.length(); 148 149 std::string formatted; 150 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; 155 156 formatted += format_paragraph(line, tag, iter == lines.begin(), 157 repeat, col); 158 if (iter + 1 != lines.end()) { 159 if (repeat) 160 formatted += "\n" + tag + "\n"; 161 else 162 formatted += "\n\n"; 163 } 164 } 165 166 return formatted; 167 } 168 169 std::string 170 impl::format_warning(const std::string& prog_name, const std::string& error) 171 { 172 return format_text_with_tag("WARNING: " + error, prog_name + ": ", true); 173 } 174