diff --git a/include/dwarfs/terminal.h b/include/dwarfs/terminal.h index f3924505..d30c9c6f 100644 --- a/include/dwarfs/terminal.h +++ b/include/dwarfs/terminal.h @@ -73,6 +73,9 @@ class terminal { virtual std::string colored(std::string text, termcolor color, bool enable = true, termstyle style = termstyle::NORMAL) const = 0; + virtual std::string_view carriage_return() const = 0; + virtual std::string_view rewind_line() const = 0; + virtual std::string_view clear_line() const = 0; }; std::string_view diff --git a/src/dwarfs/console_writer.cpp b/src/dwarfs/console_writer.cpp index ff1d5fda..defa9ab7 100644 --- a/src/dwarfs/console_writer.cpp +++ b/src/dwarfs/console_writer.cpp @@ -155,15 +155,18 @@ console_writer::console_writer(std::shared_ptr term, void console_writer::rewind(int next_rewind_lines) { if (!statebuf_.empty()) { auto& os = log_stream(); + auto& term = this->term(); + auto clear_line = term.clear_line(); + auto rewind_line = term.rewind_line(); - os << '\r'; + os << term.carriage_return(); int num_erase = rewind_lines_ - next_rewind_lines; for (int i = 0; i < rewind_lines_; ++i) { - os << "\x1b[A"; + os << rewind_line; if (num_erase > 0) { - os << "\x1b[2K"; + os << clear_line; --num_erase; } } diff --git a/src/dwarfs/terminal.cpp b/src/dwarfs/terminal.cpp index 85bbad44..e86517f4 100644 --- a/src/dwarfs/terminal.cpp +++ b/src/dwarfs/terminal.cpp @@ -115,6 +115,25 @@ std::string terminal_ansi_colored(std::string_view text, termcolor color, namespace { +class terminal_ansi : public terminal { + public: + std::string_view + color(termcolor color, termstyle style = termstyle::NORMAL) const override { + return terminal_ansi_color(color, style); + } + + std::string colored(std::string text, termcolor color, bool enable = true, + termstyle style = termstyle::NORMAL) const override { + return terminal_ansi_colored(std::move(text), color, enable, style); + } + + std::string_view carriage_return() const override { return "\r"; } + + std::string_view rewind_line() const override { return "\x1b[A"; } + + std::string_view clear_line() const override { return "\x1b[2K"; } +}; + #if defined(_WIN32) void WindowsEmulateVT100Terminal(DWORD std_handle) { @@ -140,7 +159,7 @@ void WindowsEmulateVT100Terminal(DWORD std_handle) { ::SetConsoleMode(hdl, out_mode); } -class terminal_windows : public terminal { +class terminal_windows : public terminal_ansi { public: size_t width() const override { CONSOLE_SCREEN_BUFFER_INFO csbi; @@ -157,21 +176,11 @@ class terminal_windows : public terminal { } return false; } - - std::string_view - color(termcolor color, termstyle style = termstyle::NORMAL) const override { - return terminal_ansi_color(color, style); - } - - std::string colored(std::string text, termcolor color, bool enable = true, - termstyle style = termstyle::NORMAL) const override { - return terminal_ansi_colored(std::move(text), color, enable, style); - } }; #else -class terminal_posix : public terminal { +class terminal_posix : public terminal_ansi { public: size_t width() const override { struct ::winsize w; @@ -189,16 +198,6 @@ class terminal_posix : public terminal { auto term = ::getenv("TERM"); return term && term[0] != '\0' && ::strcmp(term, "dumb") != 0; } - - std::string_view - color(termcolor color, termstyle style = termstyle::NORMAL) const override { - return terminal_ansi_color(color, style); - } - - std::string colored(std::string text, termcolor color, bool enable = true, - termstyle style = termstyle::NORMAL) const override { - return terminal_ansi_colored(std::move(text), color, enable, style); - } }; #endif diff --git a/test/test_helpers.h b/test/test_helpers.h index 9cb8fbea..71199bb8 100644 --- a/test/test_helpers.h +++ b/test/test_helpers.h @@ -160,6 +160,9 @@ class test_terminal : public terminal { std::string_view color(termcolor color, termstyle style) const override; std::string colored(std::string text, termcolor color, bool enable, termstyle style) const override; + std::string_view carriage_return() const override; + std::string_view rewind_line() const override; + std::string_view clear_line() const override; private: std::ostream* out_; diff --git a/test/test_iolayer.cpp b/test/test_iolayer.cpp index 3b81b0cc..5a8603ca 100644 --- a/test/test_iolayer.cpp +++ b/test/test_iolayer.cpp @@ -219,6 +219,12 @@ bool test_terminal::is_fancy(std::ostream& os) const { return fancy_ && (&os == out_ || &os == err_); } +std::string_view test_terminal::carriage_return() const { return ""; } + +std::string_view test_terminal::rewind_line() const { return ""; } + +std::string_view test_terminal::clear_line() const { return ""; } + std::string_view test_terminal::color(termcolor color, termstyle style) const { static constexpr std::array(termcolor::NUM_COLORS)>