feat(terminal): add background color support

This commit is contained in:
Marcus Holland-Moritz 2025-05-28 09:30:07 +02:00
parent b57c3808e7
commit 2c6a94ce61
4 changed files with 84 additions and 0 deletions

View File

@ -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;

View File

@ -194,6 +194,45 @@ std::string_view terminal_ansi::color_impl(termcolor color, termstyle style) {
return colors.at(static_cast<size_t>(color));
}
std::string_view terminal_ansi::bgcolor_impl(termcolor color) {
static constexpr std::array<std::string_view,
static_cast<size_t>(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<size_t>(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 {

View File

@ -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;

View File

@ -286,6 +286,44 @@ std::string_view test_terminal::color(termcolor color, termstyle style) const {
return colors.at(static_cast<size_t>(color));
}
std::string_view test_terminal::bgcolor(termcolor color) const {
static constexpr std::array<std::string_view,
static_cast<size_t>(termcolor::NUM_COLORS)>
// clang-format off
colors = {{
"<bg-normal>",
"<bg-black>",
"<bg-red>",
"<bg-green>",
"<bg-yellow>",
"<bg-blue>",
"<bg-magenta>",
"<bg-cyan>",
"<bg-white>",
"<bg-gray>",
"<bg-bright-black>",
"<bg-bright-red>",
"<bg-bright-green>",
"<bg-bright-yellow>",
"<bg-bright-blue>",
"<bg-bright-magenta>",
"<bg-bright-cyan>",
"<bg-bright-white>",
"<bg-bright-gray>",
"<bg-dim-black>",
"<bg-dim-red>",
"<bg-dim-green>",
"<bg-dim-yellow>",
"<bg-dim-blue>",
"<bg-dim-magenta>",
"<bg-dim-cyan>",
"<bg-dim-white>",
"<bg-dim-gray>",
}}; // clang-format on
return colors.at(static_cast<size_t>(color));
}
std::string test_terminal::colored(std::string text, termcolor color,
bool enable, termstyle style) const {
std::string result;