23 #include <sys/ioctl.h>
31 const char* term = getenv(
"TERM");
38 setvbuf(stdout, NULL, _IONBF, 0);
39 console_ = GetStdHandle(STD_OUTPUT_HANDLE);
40 CONSOLE_SCREEN_BUFFER_INFO csbi;
53 CONSOLE_SCREEN_BUFFER_INFO csbi;
54 GetConsoleScreenBufferInfo(console_, &csbi);
61 csbi.dwCursorPosition.X = 0;
62 SetConsoleCursorPosition(console_, csbi.dwCursorPosition);
69 size_t width =
static_cast<size_t>(csbi.dwSize.X) - 1;
74 GetConsoleScreenBufferInfo(console_, &csbi);
75 COORD buf_size = { csbi.dwSize.X, 1 };
76 COORD zero_zero = { 0, 0 };
78 csbi.dwCursorPosition.X, csbi.dwCursorPosition.Y,
79 static_cast<SHORT
>(csbi.dwCursorPosition.X + csbi.dwSize.X - 1),
80 csbi.dwCursorPosition.Y
82 CHAR_INFO* char_data =
new CHAR_INFO[csbi.dwSize.X];
83 memset(char_data, 0,
sizeof(CHAR_INFO) * csbi.dwSize.X);
84 for (
int i = 0; i < csbi.dwSize.X; ++i) {
85 char_data[i].Char.AsciiChar =
' ';
86 char_data[i].Attributes = csbi.wAttributes;
88 for (
size_t i = 0; i < to_print.size(); ++i)
89 char_data[i].Char.AsciiChar = to_print[i];
90 WriteConsoleOutput(console_, char_data, buf_size, zero_zero, &target);
96 if ((ioctl(0, TIOCGWINSZ, &size) == 0) && size.ws_col) {
99 printf(
"%s", to_print.c_str());
106 printf(
"%s\n", to_print.c_str());
116 fwrite(data, 1, size, stdout);
129 if (!to_print.empty()) {
bool have_blank_line_
Whether the caret is at the beginning of a blank line.
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.