refactor: abstract out more terminal operations

This commit is contained in:
Marcus Holland-Moritz 2024-01-05 00:44:45 +01:00
parent 8a21dc4ece
commit daf3e2def6
5 changed files with 39 additions and 25 deletions

View File

@ -73,6 +73,9 @@ class terminal {
virtual std::string virtual std::string
colored(std::string text, termcolor color, bool enable = true, colored(std::string text, termcolor color, bool enable = true,
termstyle style = termstyle::NORMAL) const = 0; 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 std::string_view

View File

@ -155,15 +155,18 @@ console_writer::console_writer(std::shared_ptr<terminal const> term,
void console_writer::rewind(int next_rewind_lines) { void console_writer::rewind(int next_rewind_lines) {
if (!statebuf_.empty()) { if (!statebuf_.empty()) {
auto& os = log_stream(); 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; int num_erase = rewind_lines_ - next_rewind_lines;
for (int i = 0; i < rewind_lines_; ++i) { for (int i = 0; i < rewind_lines_; ++i) {
os << "\x1b[A"; os << rewind_line;
if (num_erase > 0) { if (num_erase > 0) {
os << "\x1b[2K"; os << clear_line;
--num_erase; --num_erase;
} }
} }

View File

@ -115,6 +115,25 @@ std::string terminal_ansi_colored(std::string_view text, termcolor color,
namespace { 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) #if defined(_WIN32)
void WindowsEmulateVT100Terminal(DWORD std_handle) { void WindowsEmulateVT100Terminal(DWORD std_handle) {
@ -140,7 +159,7 @@ void WindowsEmulateVT100Terminal(DWORD std_handle) {
::SetConsoleMode(hdl, out_mode); ::SetConsoleMode(hdl, out_mode);
} }
class terminal_windows : public terminal { class terminal_windows : public terminal_ansi {
public: public:
size_t width() const override { size_t width() const override {
CONSOLE_SCREEN_BUFFER_INFO csbi; CONSOLE_SCREEN_BUFFER_INFO csbi;
@ -157,21 +176,11 @@ class terminal_windows : public terminal {
} }
return false; 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 #else
class terminal_posix : public terminal { class terminal_posix : public terminal_ansi {
public: public:
size_t width() const override { size_t width() const override {
struct ::winsize w; struct ::winsize w;
@ -189,16 +198,6 @@ class terminal_posix : public terminal {
auto term = ::getenv("TERM"); auto term = ::getenv("TERM");
return term && term[0] != '\0' && ::strcmp(term, "dumb") != 0; 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 #endif

View File

@ -160,6 +160,9 @@ class test_terminal : public terminal {
std::string_view color(termcolor color, termstyle style) const override; std::string_view color(termcolor color, termstyle style) const override;
std::string colored(std::string text, termcolor color, bool enable, std::string colored(std::string text, termcolor color, bool enable,
termstyle style) const override; 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: private:
std::ostream* out_; std::ostream* out_;

View File

@ -219,6 +219,12 @@ bool test_terminal::is_fancy(std::ostream& os) const {
return fancy_ && (&os == out_ || &os == err_); return fancy_ && (&os == out_ || &os == err_);
} }
std::string_view test_terminal::carriage_return() const { return "<cr>"; }
std::string_view test_terminal::rewind_line() const { return "<rewind>"; }
std::string_view test_terminal::clear_line() const { return "<clear>"; }
std::string_view test_terminal::color(termcolor color, termstyle style) const { std::string_view test_terminal::color(termcolor color, termstyle style) const {
static constexpr std::array<std::string_view, static constexpr std::array<std::string_view,
static_cast<size_t>(termcolor::NUM_COLORS)> static_cast<size_t>(termcolor::NUM_COLORS)>