21 #ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING 22 #define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x4 26 #include <sys/ioctl.h> 35 const char* term = getenv(
"TERM");
42 setvbuf(stdout, NULL, _IONBF, 0);
43 console_ = GetStdHandle(STD_OUTPUT_HANDLE);
44 CONSOLE_SCREEN_BUFFER_INFO csbi;
49 const char* clicolor_force = getenv(
"CLICOLOR_FORCE");
56 if (GetConsoleMode(console_, &mode)) {
57 SetConsoleMode(console_, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
78 CONSOLE_SCREEN_BUFFER_INFO csbi;
79 GetConsoleScreenBufferInfo(console_, &csbi);
81 to_print =
ElideMiddle(to_print, static_cast<size_t>(csbi.dwSize.X));
85 COORD buf_size = { csbi.dwSize.X, 1 };
86 COORD zero_zero = { 0, 0 };
88 csbi.dwCursorPosition.X, csbi.dwCursorPosition.Y,
89 static_cast<SHORT
>(csbi.dwCursorPosition.X + csbi.dwSize.X - 1),
90 csbi.dwCursorPosition.Y
92 vector<CHAR_INFO> char_data(csbi.dwSize.X);
93 for (
size_t i = 0; i < static_cast<size_t>(csbi.dwSize.X); ++i) {
94 char_data[i].Char.AsciiChar = i < to_print.size() ? to_print[i] :
' ';
95 char_data[i].Attributes = csbi.wAttributes;
97 WriteConsoleOutput(console_, &char_data[0], buf_size, zero_zero, &target);
102 if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) == 0) && size.ws_col) {
105 printf(
"%s", to_print.c_str());
112 printf(
"%s\n", to_print.c_str());
122 fwrite(data, 1, size, stdout);
135 if (!to_print.empty()) {
bool have_blank_line_
Whether the caret is at the beginning of a blank line.
bool supports_color_
Whether we can use ISO 6429 (ANSI) color sequences.
void PrintOnNewLine(const string &to_print)
Prints a string on a new line, not overprinting previous output.
bool console_locked_
Whether console is locked.
LineType line_type_
Buffered line type while console is locked.
void SetConsoleLocked(bool locked)
Lock or unlock the console.
void PrintOrBuffer(const char *data, size_t size)
Print the given data to the console, or buffer it if it is locked.
string line_buffer_
Buffered current line while console is locked.
void Print(string to_print, LineType type)
Overprints the current line.
bool smart_terminal_
Whether we can do fancy terminal control codes.
string output_buffer_
Buffered console output while console is locked.
string ElideMiddle(const string &str, size_t width)
Elide the given string str with '...' in the middle if the length exceeds width.