21 #ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING 22 #define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x4 26 #include <sys/ioctl.h> 36 const char* term = getenv(
"TERM");
44 if (term &&
string(term) ==
"dumb") {
47 setvbuf(stdout, NULL, _IONBF, 0);
48 console_ = GetStdHandle(STD_OUTPUT_HANDLE);
49 CONSOLE_SCREEN_BUFFER_INFO csbi;
55 const char* clicolor_force = getenv(
"CLICOLOR_FORCE");
62 if (GetConsoleMode(console_, &mode)) {
63 if (!SetConsoleMode(console_, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING)) {
86 CONSOLE_SCREEN_BUFFER_INFO csbi;
87 GetConsoleScreenBufferInfo(console_, &csbi);
89 to_print =
ElideMiddle(to_print, static_cast<size_t>(csbi.dwSize.X));
93 COORD buf_size = { csbi.dwSize.X, 1 };
94 COORD zero_zero = { 0, 0 };
96 csbi.dwCursorPosition.X, csbi.dwCursorPosition.Y,
97 static_cast<SHORT
>(csbi.dwCursorPosition.X + csbi.dwSize.X - 1),
98 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);
110 if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) == 0) && size.ws_col) {
113 printf(
"%s", to_print.c_str());
120 printf(
"%s\n", to_print.c_str());
130 fwrite(data, 1, size, stdout);
143 if (!to_print.empty()) {
bool have_blank_line_
Whether the caret is at the beginning of a blank line.
void Print(std::string to_print, LineType type)
Overprints the current line.
bool supports_color_
Whether we can use ISO 6429 (ANSI) color sequences.
std::string output_buffer_
Buffered console output while console is locked.
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.
std::string line_buffer_
Buffered current line while console is locked.
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 PrintOnNewLine(const std::string &to_print)
Prints a string on a new line, not overprinting previous output.
string ElideMiddle(const string &str, size_t width)