From 2c6a94ce61f934ad0bfd1779caf0230cb7b7c579 Mon Sep 17 00:00:00 2001 From: Marcus Holland-Moritz Date: Wed, 28 May 2025 09:30:07 +0200 Subject: [PATCH] feat(terminal): add background color support --- include/dwarfs/terminal_ansi.h | 2 ++ src/terminal_ansi.cpp | 43 ++++++++++++++++++++++++++++++++++ test/test_helpers.h | 1 + test/test_iolayer.cpp | 38 ++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+) diff --git a/include/dwarfs/terminal_ansi.h b/include/dwarfs/terminal_ansi.h index 1011e023..9dc34126 100644 --- a/include/dwarfs/terminal_ansi.h +++ b/include/dwarfs/terminal_ansi.h @@ -41,6 +41,7 @@ class terminal_ansi : public terminal { static std::string_view color_impl(termcolor color, termstyle style = termstyle::NORMAL); + static std::string_view bgcolor_impl(termcolor color); static std::string colored_impl(std::string_view text, termcolor color, bool enable = true, termstyle style = termstyle::NORMAL); @@ -49,6 +50,7 @@ class terminal_ansi : public terminal { bool is_tty(std::ostream& os) const override; bool is_fancy() const override; std::string_view color(termcolor color, termstyle style) const override; + std::string_view bgcolor(termcolor color) const override; std::string colored(std::string text, termcolor color, bool enable, termstyle style) const override; std::string_view carriage_return() const override; diff --git a/src/terminal_ansi.cpp b/src/terminal_ansi.cpp index 08a1e6ee..91747d99 100644 --- a/src/terminal_ansi.cpp +++ b/src/terminal_ansi.cpp @@ -194,6 +194,45 @@ std::string_view terminal_ansi::color_impl(termcolor color, termstyle style) { return colors.at(static_cast(color)); } +std::string_view terminal_ansi::bgcolor_impl(termcolor color) { + static constexpr std::array(termcolor::NUM_COLORS)> + // clang-format off + bg_colors = {{ + "\033[0m", + "\033[40m", + "\033[41m", + "\033[42m", + "\033[43m", + "\033[44m", + "\033[45m", + "\033[46m", + "\033[47m", + "\033[100m", + "\033[100m", + "\033[101m", + "\033[102m", + "\033[103m", + "\033[104m", + "\033[105m", + "\033[106m", + "\033[107m", + "\033[100m", + "\033[40m", + "\033[41m", + "\033[42m", + "\033[43m", + "\033[44m", + "\033[45m", + "\033[46m", + "\033[47m", + "\033[40m", + }}; + // clang-format on + + return bg_colors.at(static_cast(color)); +} + std::string terminal_ansi::colored_impl(std::string_view text, termcolor color, bool enable, termstyle style) { std::string result; @@ -225,6 +264,10 @@ terminal_ansi::color(termcolor color, return color_impl(color, style); } +std::string_view terminal_ansi::bgcolor(termcolor color) const { + return bgcolor_impl(color); +} + std::string terminal_ansi::colored(std::string text, termcolor color, bool enable = true, termstyle style = termstyle::NORMAL) const { diff --git a/test/test_helpers.h b/test/test_helpers.h index 966a1c05..7436d182 100644 --- a/test/test_helpers.h +++ b/test/test_helpers.h @@ -261,6 +261,7 @@ class test_terminal : public terminal { size_t width() const override; bool is_tty(std::ostream& os) const override; bool is_fancy() const override; + std::string_view bgcolor(termcolor color) const override; std::string_view color(termcolor color, termstyle style) const override; std::string colored(std::string text, termcolor color, bool enable, termstyle style) const override; diff --git a/test/test_iolayer.cpp b/test/test_iolayer.cpp index 061066a5..609a6bac 100644 --- a/test/test_iolayer.cpp +++ b/test/test_iolayer.cpp @@ -286,6 +286,44 @@ std::string_view test_terminal::color(termcolor color, termstyle style) const { return colors.at(static_cast(color)); } +std::string_view test_terminal::bgcolor(termcolor color) const { + static constexpr std::array(termcolor::NUM_COLORS)> + // clang-format off + colors = {{ + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + }}; // clang-format on + + return colors.at(static_cast(color)); +} + std::string test_terminal::colored(std::string text, termcolor color, bool enable, termstyle style) const { std::string result;