mirror of
https://github.com/mhx/dwarfs.git
synced 2025-09-11 13:30:47 -04:00
refactor: abstract out more terminal operations
This commit is contained in:
parent
8a21dc4ece
commit
daf3e2def6
@ -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
|
||||
|
@ -155,15 +155,18 @@ console_writer::console_writer(std::shared_ptr<terminal const> 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;
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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_;
|
||||
|
@ -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 "<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 {
|
||||
static constexpr std::array<std::string_view,
|
||||
static_cast<size_t>(termcolor::NUM_COLORS)>
|
||||
|
Loading…
x
Reference in New Issue
Block a user