| Elliott Hughes | b708d16 | 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 |  | 
| Elliott Hughes | 77f539a | 2015-12-08 16:01:15 -0800 | [diff] [blame] | 46 | LinePrinter::LinePrinter() : have_blank_line_(true) { | 
| Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 47 | #ifndef _WIN32 | 
|  | 48 | const char* term = getenv("TERM"); | 
| David Pursell | c5b8ad8 | 2015-10-28 14:29:51 -0700 | [diff] [blame] | 49 | smart_terminal_ = unix_isatty(1) && term && string(term) != "dumb"; | 
| Elliott Hughes | b708d16 | 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 |  | 
| Elliott Hughes | 77f539a | 2015-12-08 16:01:15 -0800 | [diff] [blame] | 62 | static void Out(const std::string& s) { | 
|  | 63 | // Avoid printf and C strings, since the actual output might contain null | 
|  | 64 | // bytes like UTF-16 does (yuck). | 
|  | 65 | fwrite(s.data(), 1, s.size(), stdout); | 
|  | 66 | } | 
|  | 67 |  | 
| Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 68 | void LinePrinter::Print(string to_print, LineType type) { | 
| Elliott Hughes | 77f539a | 2015-12-08 16:01:15 -0800 | [diff] [blame] | 69 | if (!smart_terminal_) { | 
| Elliott Hughes | d68ad69 | 2016-01-08 15:47:07 -0800 | [diff] [blame] | 70 | Out(to_print + "\n"); | 
| Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 71 | return; | 
|  | 72 | } | 
|  | 73 |  | 
| Elliott Hughes | 77f539a | 2015-12-08 16:01:15 -0800 | [diff] [blame] | 74 | // Print over previous line, if any. | 
|  | 75 | // On Windows, calling a C library function writing to stdout also handles | 
|  | 76 | // pausing the executable when the "Pause" key or Ctrl-S is pressed. | 
|  | 77 | printf("\r"); | 
| Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 78 |  | 
| Elliott Hughes | 77f539a | 2015-12-08 16:01:15 -0800 | [diff] [blame] | 79 | if (type == INFO) { | 
| Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 80 | #ifdef _WIN32 | 
|  | 81 | CONSOLE_SCREEN_BUFFER_INFO csbi; | 
|  | 82 | GetConsoleScreenBufferInfo(console_, &csbi); | 
|  | 83 |  | 
| Spencer Low | d21dc82 | 2015-11-12 15:20:15 -0800 | [diff] [blame] | 84 | // TODO: std::wstring to_print_wide; if (!android::base::UTF8ToWide(to_print, &to_print_wide)... | 
| Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 85 | // TODO: wstring ElideMiddle. | 
|  | 86 | to_print = ElideMiddle(to_print, static_cast<size_t>(csbi.dwSize.X)); | 
|  | 87 | // We don't want to have the cursor spamming back and forth, so instead of | 
|  | 88 | // printf use WriteConsoleOutput which updates the contents of the buffer, | 
|  | 89 | // but doesn't move the cursor position. | 
|  | 90 | COORD buf_size = { csbi.dwSize.X, 1 }; | 
|  | 91 | COORD zero_zero = { 0, 0 }; | 
|  | 92 | SMALL_RECT target = { | 
|  | 93 | csbi.dwCursorPosition.X, csbi.dwCursorPosition.Y, | 
|  | 94 | static_cast<SHORT>(csbi.dwCursorPosition.X + csbi.dwSize.X - 1), | 
|  | 95 | csbi.dwCursorPosition.Y | 
|  | 96 | }; | 
|  | 97 | vector<CHAR_INFO> char_data(csbi.dwSize.X); | 
|  | 98 | for (size_t i = 0; i < static_cast<size_t>(csbi.dwSize.X); ++i) { | 
|  | 99 | // TODO: UnicodeChar instead of AsciiChar, to_print_wide[i]. | 
|  | 100 | char_data[i].Char.AsciiChar = i < to_print.size() ? to_print[i] : ' '; | 
|  | 101 | char_data[i].Attributes = csbi.wAttributes; | 
|  | 102 | } | 
|  | 103 | // TODO: WriteConsoleOutputW. | 
|  | 104 | WriteConsoleOutput(console_, &char_data[0], buf_size, zero_zero, &target); | 
|  | 105 | #else | 
|  | 106 | // Limit output to width of the terminal if provided so we don't cause | 
|  | 107 | // line-wrapping. | 
|  | 108 | winsize size; | 
|  | 109 | if ((ioctl(0, TIOCGWINSZ, &size) == 0) && size.ws_col) { | 
|  | 110 | to_print = ElideMiddle(to_print, size.ws_col); | 
|  | 111 | } | 
| Elliott Hughes | 77f539a | 2015-12-08 16:01:15 -0800 | [diff] [blame] | 112 | Out(to_print); | 
| Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 113 | printf("\x1B[K");  // Clear to end of line. | 
|  | 114 | fflush(stdout); | 
|  | 115 | #endif | 
|  | 116 |  | 
|  | 117 | have_blank_line_ = false; | 
|  | 118 | } else { | 
| Elliott Hughes | 77f539a | 2015-12-08 16:01:15 -0800 | [diff] [blame] | 119 | Out(to_print); | 
|  | 120 | Out("\n"); | 
|  | 121 | have_blank_line_ = true; | 
| Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 122 | } | 
|  | 123 | } | 
|  | 124 |  | 
| Elliott Hughes | 77f539a | 2015-12-08 16:01:15 -0800 | [diff] [blame] | 125 | void LinePrinter::KeepInfoLine() { | 
|  | 126 | if (!have_blank_line_) Out("\n"); | 
| Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 127 | } |