Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 1 | // Copyright 2013 Google Inc. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include "line_printer.h" |
| 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #ifdef _WIN32 |
| 20 | #include <windows.h> |
| 21 | #else |
| 22 | #include <unistd.h> |
| 23 | #include <sys/ioctl.h> |
| 24 | #include <termios.h> |
| 25 | #include <sys/time.h> |
| 26 | #endif |
| 27 | |
| 28 | // Make sure printf is really adb_printf which works for UTF-8 on Windows. |
| 29 | #include <sysdeps.h> |
| 30 | |
| 31 | // Stuff from ninja's util.h that's needed below. |
| 32 | #include <vector> |
| 33 | using namespace std; |
| 34 | string ElideMiddle(const string& str, size_t width) { |
| 35 | const int kMargin = 3; // Space for "...". |
| 36 | string result = str; |
| 37 | if (result.size() + kMargin > width) { |
| 38 | size_t elide_size = (width - kMargin) / 2; |
| 39 | result = result.substr(0, elide_size) |
| 40 | + "..." |
| 41 | + result.substr(result.size() - elide_size, elide_size); |
| 42 | } |
| 43 | return result; |
| 44 | } |
| 45 | |
| 46 | LinePrinter::LinePrinter() : have_blank_line_(true), console_locked_(false) { |
| 47 | #ifndef _WIN32 |
| 48 | const char* term = getenv("TERM"); |
David Pursell | 5880536 | 2015-10-28 14:29:51 -0700 | [diff] [blame^] | 49 | smart_terminal_ = unix_isatty(1) && term && string(term) != "dumb"; |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 50 | #else |
| 51 | // Disable output buffer. It'd be nice to use line buffering but |
| 52 | // MSDN says: "For some systems, [_IOLBF] provides line |
| 53 | // buffering. However, for Win32, the behavior is the same as _IOFBF |
| 54 | // - Full Buffering." |
| 55 | setvbuf(stdout, NULL, _IONBF, 0); |
| 56 | console_ = GetStdHandle(STD_OUTPUT_HANDLE); |
| 57 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 58 | smart_terminal_ = GetConsoleScreenBufferInfo(console_, &csbi); |
| 59 | #endif |
| 60 | } |
| 61 | |
| 62 | void LinePrinter::Print(string to_print, LineType type) { |
| 63 | if (console_locked_) { |
| 64 | line_buffer_ = to_print; |
| 65 | line_type_ = type; |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | if (smart_terminal_) { |
| 70 | printf("\r"); // Print over previous line, if any. |
| 71 | // On Windows, calling a C library function writing to stdout also handles |
| 72 | // pausing the executable when the "Pause" key or Ctrl-S is pressed. |
| 73 | } |
| 74 | |
| 75 | if (smart_terminal_ && type == ELIDE) { |
| 76 | #ifdef _WIN32 |
| 77 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 78 | GetConsoleScreenBufferInfo(console_, &csbi); |
| 79 | |
| 80 | // TODO: const std::wstring to_print_wide = widen(to_print); |
| 81 | // TODO: wstring ElideMiddle. |
| 82 | to_print = ElideMiddle(to_print, static_cast<size_t>(csbi.dwSize.X)); |
| 83 | // We don't want to have the cursor spamming back and forth, so instead of |
| 84 | // printf use WriteConsoleOutput which updates the contents of the buffer, |
| 85 | // but doesn't move the cursor position. |
| 86 | COORD buf_size = { csbi.dwSize.X, 1 }; |
| 87 | COORD zero_zero = { 0, 0 }; |
| 88 | SMALL_RECT target = { |
| 89 | csbi.dwCursorPosition.X, csbi.dwCursorPosition.Y, |
| 90 | static_cast<SHORT>(csbi.dwCursorPosition.X + csbi.dwSize.X - 1), |
| 91 | csbi.dwCursorPosition.Y |
| 92 | }; |
| 93 | vector<CHAR_INFO> char_data(csbi.dwSize.X); |
| 94 | for (size_t i = 0; i < static_cast<size_t>(csbi.dwSize.X); ++i) { |
| 95 | // TODO: UnicodeChar instead of AsciiChar, to_print_wide[i]. |
| 96 | char_data[i].Char.AsciiChar = i < to_print.size() ? to_print[i] : ' '; |
| 97 | char_data[i].Attributes = csbi.wAttributes; |
| 98 | } |
| 99 | // TODO: WriteConsoleOutputW. |
| 100 | WriteConsoleOutput(console_, &char_data[0], buf_size, zero_zero, &target); |
| 101 | #else |
| 102 | // Limit output to width of the terminal if provided so we don't cause |
| 103 | // line-wrapping. |
| 104 | winsize size; |
| 105 | if ((ioctl(0, TIOCGWINSZ, &size) == 0) && size.ws_col) { |
| 106 | to_print = ElideMiddle(to_print, size.ws_col); |
| 107 | } |
| 108 | printf("%s", to_print.c_str()); |
| 109 | printf("\x1B[K"); // Clear to end of line. |
| 110 | fflush(stdout); |
| 111 | #endif |
| 112 | |
| 113 | have_blank_line_ = false; |
| 114 | } else { |
| 115 | printf("%s\n", to_print.c_str()); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | void LinePrinter::PrintOrBuffer(const char* data, size_t size) { |
| 120 | if (console_locked_) { |
| 121 | output_buffer_.append(data, size); |
| 122 | } else { |
| 123 | // Avoid printf and C strings, since the actual output might contain null |
| 124 | // bytes like UTF-16 does (yuck). |
| 125 | fwrite(data, 1, size, stdout); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | void LinePrinter::PrintOnNewLine(const string& to_print) { |
| 130 | if (console_locked_ && !line_buffer_.empty()) { |
| 131 | output_buffer_.append(line_buffer_); |
| 132 | output_buffer_.append(1, '\n'); |
| 133 | line_buffer_.clear(); |
| 134 | } |
| 135 | if (!have_blank_line_) { |
| 136 | PrintOrBuffer("\n", 1); |
| 137 | } |
| 138 | if (!to_print.empty()) { |
| 139 | PrintOrBuffer(&to_print[0], to_print.size()); |
| 140 | } |
| 141 | have_blank_line_ = to_print.empty() || *to_print.rbegin() == '\n'; |
| 142 | } |
| 143 | |
| 144 | void LinePrinter::SetConsoleLocked(bool locked) { |
| 145 | if (locked == console_locked_) |
| 146 | return; |
| 147 | |
| 148 | if (locked) |
| 149 | PrintOnNewLine(""); |
| 150 | |
| 151 | console_locked_ = locked; |
| 152 | |
| 153 | if (!locked) { |
| 154 | PrintOnNewLine(output_buffer_); |
| 155 | if (!line_buffer_.empty()) { |
| 156 | Print(line_buffer_, line_type_); |
| 157 | } |
| 158 | output_buffer_.clear(); |
| 159 | line_buffer_.clear(); |
| 160 | } |
| 161 | } |