From 40b99f6b0445a95e25ae7cd5067d3c5044a05f8a Mon Sep 17 00:00:00 2001 From: Dragon Date: Thu, 28 Oct 2021 23:55:59 +0300 Subject: [PATCH] Generate single header --- single_include/indicators/indicators.hpp | 107 +++++++++++++++++++++-- 1 file changed, 101 insertions(+), 6 deletions(-) diff --git a/single_include/indicators/indicators.hpp b/single_include/indicators/indicators.hpp index 46a0f05..b9121ca 100644 --- a/single_include/indicators/indicators.hpp +++ b/single_include/indicators/indicators.hpp @@ -1270,12 +1270,39 @@ static inline void show_console_cursor(bool const show) { SetConsoleCursorInfo(out, &cursorInfo); } +static inline void erase_line() { + auto hStdout = GetStdHandle(STD_OUTPUT_HANDLE); + if (!hStdout) + return; + + CONSOLE_SCREEN_BUFFER_INFO csbiInfo; + GetConsoleScreenBufferInfo(hStdout, &csbiInfo); + + COORD cursor; + + cursor.X = 0; + cursor.Y = csbiInfo.dwCursorPosition.Y; + + DWORD count = 0; + + FillConsoleOutputCharacterA(hStdout, ' ', csbiInfo.dwSize.X, cursor, &count); + + FillConsoleOutputAttribute(hStdout, csbiInfo.wAttributes, csbiInfo.dwSize.X, + cursor, &count); + + SetConsoleCursorPosition(hStdout, cursor); +} + #else static inline void show_console_cursor(bool const show) { std::fputs(show ? "\033[?25h" : "\033[?25l", stdout); } +static inline void erase_line() { + std::fputs("\r\033[K", stdout); +} + #endif } // namespace indicators @@ -4307,6 +4334,73 @@ public: #include // #include // #include +// #include +#ifndef INDICATORS_CURSOR_CONTROL +#define INDICATORS_CURSOR_CONTROL + +#if defined(_MSC_VER) +#if !defined(NOMINMAX) +#define NOMINMAX +#endif +#include +#include +#else +#include +#endif + +namespace indicators { + +#if defined(_MSC_VER) + +static inline void show_console_cursor(bool const show) { + HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); + + CONSOLE_CURSOR_INFO cursorInfo; + + GetConsoleCursorInfo(out, &cursorInfo); + cursorInfo.bVisible = show; // set the cursor visibility + SetConsoleCursorInfo(out, &cursorInfo); +} + +static inline void erase_line() { + auto hStdout = GetStdHandle(STD_OUTPUT_HANDLE); + if (!hStdout) + return; + + CONSOLE_SCREEN_BUFFER_INFO csbiInfo; + GetConsoleScreenBufferInfo(hStdout, &csbiInfo); + + COORD cursor; + + cursor.X = 0; + cursor.Y = csbiInfo.dwCursorPosition.Y; + + DWORD count = 0; + + FillConsoleOutputCharacterA(hStdout, ' ', csbiInfo.dwSize.X, cursor, &count); + + FillConsoleOutputAttribute(hStdout, csbiInfo.wAttributes, csbiInfo.dwSize.X, + cursor, &count); + + SetConsoleCursorPosition(hStdout, cursor); +} + +#else + +static inline void show_console_cursor(bool const show) { + std::fputs(show ? "\033[?25h" : "\033[?25l", stdout); +} + +static inline void erase_line() { + std::fputs("\r\033[K", stdout); +} + +#endif + +} // namespace indicators + +#endif +// #include // #include #include #include @@ -4384,8 +4478,11 @@ public: if (hide_bar_when_complete) { // Hide completed bars if (started_) { - for (size_t i = 0; i < incomplete_count_; ++i) - std::cout << "\033[A\r\033[K" << std::flush; + for (size_t i = 0; i < incomplete_count_; ++i) { + move_up(1); + erase_line(); + std::cout << std::flush; + } } incomplete_count_ = 0; for (auto &bar : bars_) { @@ -4399,10 +4496,8 @@ public: started_ = true; } else { // Don't hide any bars - if (started_) { - for (size_t i = 0; i < total_count_; ++i) - std::cout << "\x1b[A"; - } + if (started_) + move_up(static_cast(total_count_)); for (auto &bar : bars_) { bar.get().print_progress(true); std::cout << "\n";