21 #ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
22 #define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x4
26 #include <sys/ioctl.h>
37 const char* term = getenv(
"TERM");
41 if (term &&
string(term) ==
"dumb") {
44 console_ = GetStdHandle(STD_OUTPUT_HANDLE);
45 CONSOLE_SCREEN_BUFFER_INFO csbi;
54 if (GetConsoleMode(console_, &mode)) {
55 if (!SetConsoleMode(console_, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING)) {
62 const char* clicolor_force = getenv(
"CLICOLOR_FORCE");
82 CONSOLE_SCREEN_BUFFER_INFO csbi;
83 GetConsoleScreenBufferInfo(console_, &csbi);
88 printf(
"%s\x1B[K", to_print.c_str());
94 COORD buf_size = { csbi.dwSize.X, 1 };
95 COORD zero_zero = { 0, 0 };
96 SMALL_RECT target = { csbi.dwCursorPosition.X, csbi.dwCursorPosition.Y,
97 static_cast<SHORT
>(csbi.dwCursorPosition.X +
99 csbi.dwCursorPosition.Y };
100 vector<CHAR_INFO> char_data(csbi.dwSize.X);
101 for (
size_t i = 0; i < static_cast<size_t>(csbi.dwSize.X); ++i) {
102 char_data[i].Char.AsciiChar = i < to_print.size() ? to_print[i] :
' ';
103 char_data[i].Attributes = csbi.wAttributes;
105 WriteConsoleOutput(console_, &char_data[0], buf_size, zero_zero, &target);
111 if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) == 0) && size.ws_col) {
114 printf(
"%s", to_print.c_str());
121 printf(
"%s\n", to_print.c_str());
132 fwrite(data, 1, size, stdout);
145 if (!to_print.empty()) {
void ElideMiddleInPlace(std::string &str, size_t max_width)
Elide the given string str with '...' in the middle if the length exceeds max_width.
void PrintOrBuffer(const char *data, size_t size)
Print the given data to the console, or buffer it if it is locked.
bool smart_terminal_
Whether we can do fancy terminal control codes.
void Print(std::string to_print, LineType type)
Overprints the current line.
bool console_locked_
Whether console is locked.
bool have_blank_line_
Whether the caret is at the beginning of a blank line.
void SetConsoleLocked(bool locked)
Lock or unlock the console.
std::string output_buffer_
Buffered console output while console is locked.
bool supports_color_
Whether we can use ISO 6429 (ANSI) color sequences.
std::string line_buffer_
Buffered current line while console is locked.
void PrintOnNewLine(const std::string &to_print)
Prints a string on a new line, not overprinting previous output.
LineType line_type_
Buffered line type while console is locked.