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 #include "util.h"
29
LinePrinter()30 LinePrinter::LinePrinter() : have_blank_line_(true), console_locked_(false) {
31 #ifndef _WIN32
32 const char* term = getenv("TERM");
33 smart_terminal_ = isatty(1) && term && string(term) != "dumb";
34 #else
35 // Disable output buffer. It'd be nice to use line buffering but
36 // MSDN says: "For some systems, [_IOLBF] provides line
37 // buffering. However, for Win32, the behavior is the same as _IOFBF
38 // - Full Buffering."
39 setvbuf(stdout, NULL, _IONBF, 0);
40 console_ = GetStdHandle(STD_OUTPUT_HANDLE);
41 CONSOLE_SCREEN_BUFFER_INFO csbi;
42 smart_terminal_ = GetConsoleScreenBufferInfo(console_, &csbi);
43 #endif
44 }
45
Print(string to_print,LineType type)46 void LinePrinter::Print(string to_print, LineType type) {
47 if (console_locked_) {
48 line_buffer_ = to_print;
49 line_type_ = type;
50 return;
51 }
52
53 if (smart_terminal_) {
54 printf("\r"); // Print over previous line, if any.
55 // On Windows, calling a C library function writing to stdout also handles
56 // pausing the executable when the "Pause" key or Ctrl-S is pressed.
57 }
58
59 if (smart_terminal_ && type == ELIDE) {
60 #ifdef _WIN32
61 CONSOLE_SCREEN_BUFFER_INFO csbi;
62 GetConsoleScreenBufferInfo(console_, &csbi);
63
64 to_print = ElideMiddle(to_print, static_cast<size_t>(csbi.dwSize.X));
65 // We don't want to have the cursor spamming back and forth, so instead of
66 // printf use WriteConsoleOutput which updates the contents of the buffer,
67 // but doesn't move the cursor position.
68 COORD buf_size = { csbi.dwSize.X, 1 };
69 COORD zero_zero = { 0, 0 };
70 SMALL_RECT target = {
71 csbi.dwCursorPosition.X, csbi.dwCursorPosition.Y,
72 static_cast<SHORT>(csbi.dwCursorPosition.X + csbi.dwSize.X - 1),
73 csbi.dwCursorPosition.Y
74 };
75 vector<CHAR_INFO> char_data(csbi.dwSize.X);
76 for (size_t i = 0; i < static_cast<size_t>(csbi.dwSize.X); ++i) {
77 char_data[i].Char.AsciiChar = i < to_print.size() ? to_print[i] : ' ';
78 char_data[i].Attributes = csbi.wAttributes;
79 }
80 WriteConsoleOutput(console_, &char_data[0], buf_size, zero_zero, &target);
81 #else
82 // Limit output to width of the terminal if provided so we don't cause
83 // line-wrapping.
84 winsize size;
85 if ((ioctl(0, TIOCGWINSZ, &size) == 0) && size.ws_col) {
86 to_print = ElideMiddle(to_print, size.ws_col);
87 }
88 printf("%s", to_print.c_str());
89 printf("\x1B[K"); // Clear to end of line.
90 fflush(stdout);
91 #endif
92
93 have_blank_line_ = false;
94 } else {
95 printf("%s\n", to_print.c_str());
96 }
97 }
98
PrintOrBuffer(const char * data,size_t size)99 void LinePrinter::PrintOrBuffer(const char* data, size_t size) {
100 if (console_locked_) {
101 output_buffer_.append(data, size);
102 } else {
103 // Avoid printf and C strings, since the actual output might contain null
104 // bytes like UTF-16 does (yuck).
105 fwrite(data, 1, size, stdout);
106 }
107 }
108
PrintOnNewLine(const string & to_print)109 void LinePrinter::PrintOnNewLine(const string& to_print) {
110 if (console_locked_ && !line_buffer_.empty()) {
111 output_buffer_.append(line_buffer_);
112 output_buffer_.append(1, '\n');
113 line_buffer_.clear();
114 }
115 if (!have_blank_line_) {
116 PrintOrBuffer("\n", 1);
117 }
118 if (!to_print.empty()) {
119 PrintOrBuffer(&to_print[0], to_print.size());
120 }
121 have_blank_line_ = to_print.empty() || *to_print.rbegin() == '\n';
122 }
123
SetConsoleLocked(bool locked)124 void LinePrinter::SetConsoleLocked(bool locked) {
125 if (locked == console_locked_)
126 return;
127
128 if (locked)
129 PrintOnNewLine("");
130
131 console_locked_ = locked;
132
133 if (!locked) {
134 PrintOnNewLine(output_buffer_);
135 if (!line_buffer_.empty()) {
136 Print(line_buffer_, line_type_);
137 }
138 output_buffer_.clear();
139 line_buffer_.clear();
140 }
141 }
142