diff --git a/uuu/uuu.cpp b/uuu/uuu.cpp index af194f8d..0912afbf 100644 --- a/uuu/uuu.cpp +++ b/uuu/uuu.cpp @@ -54,8 +54,6 @@ const char * g_vt_red = "\x1B[91m"; const char * g_vt_kcyn = "\x1B[36m"; const char * g_vt_boldwhite = "\x1B[97m"; -int g_failback_console_width = 300; - size_t g_max_process_width = 0; void clean_vt_color() noexcept @@ -600,12 +598,12 @@ mutex g_callback_mutex; void print_oneline(string str) { - size_t w = get_console_width(); + int w = get_console_width(); if (w <= 3) return; if (g_max_process_width == 0){ - g_max_process_width = min(str.size(), w); + g_max_process_width = min(str.size(), w); } if (str.size() >= w) @@ -767,7 +765,8 @@ bool enable_vt_mode() int get_console_width() { CONSOLE_SCREEN_BUFFER_INFO sbInfo; - GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &sbInfo); + if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &sbInfo)) + return -1; return sbInfo.dwSize.X; } #else @@ -776,10 +775,11 @@ bool enable_vt_mode() { return true; } int get_console_width() { struct winsize w; - if (ioctl(0, TIOCGWINSZ, &w) == -1) + if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == -1) { - return g_failback_console_width; + return -1; } + return w.ws_col; } #endif @@ -1154,6 +1154,19 @@ int main(int argc, char **argv) } } +#ifndef _WIN32 + if (!g_verbose && !isatty(STDOUT_FILENO)) { + cout << "stdout is not a TTY. Using verbose mode" << endl; + g_verbose = 1; + } +#endif + + if (!g_verbose && get_console_width() <= 3) + { + cout << "Failed to get terminal width, or terminal width too small. Using verbose mode" << endl; + g_verbose = 1; + } + signal(SIGINT, ctrl_c_handle); uuu_set_askpasswd(ask_passwd);