From 353be8519fee9fbffe3c0f11379bc902e098f043 Mon Sep 17 00:00:00 2001 From: data-man Date: Fri, 21 May 2021 22:58:35 +0500 Subject: [PATCH] Add more emphases --- include/fmt/color.h | 33 ++++++++++++++++++++++----------- test/color-test.cc | 7 +++++++ 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/include/fmt/color.h b/include/fmt/color.h index 8cddbfe1..3c8246b8 100644 --- a/include/fmt/color.h +++ b/include/fmt/color.h @@ -185,9 +185,17 @@ enum class terminal_color : uint8_t { enum class emphasis : uint8_t { bold = 1, - italic = 1 << 1, - underline = 1 << 2, - strikethrough = 1 << 3 + faint = 1 << 1, + dim = faint, + italic = 1 << 2, + underline = 1 << 3, + blink = 1 << 4, + slow_blink = blink, + reverse = 1 << 5, + invert = reverse, + conceal = 1 << 6, + hide = conceal, + strikethrough = 1 << 7, }; // rgb is a struct for red, green and blue colors. @@ -399,7 +407,7 @@ template struct ansi_color_escape { return; } - for (int i = 0; i < 7; i++) { + for (int i = 0; i < 8; i++) { buffer[i] = static_cast(esc[i]); } rgb color(text_color.value.rgb_color); @@ -409,16 +417,19 @@ template struct ansi_color_escape { buffer[19] = static_cast(0); } FMT_CONSTEXPR ansi_color_escape(emphasis em) FMT_NOEXCEPT { - uint8_t em_codes[4] = {}; + uint8_t em_codes[8] = {}; uint8_t em_bits = static_cast(em); if (em_bits & static_cast(emphasis::bold)) em_codes[0] = 1; - if (em_bits & static_cast(emphasis::italic)) em_codes[1] = 3; - if (em_bits & static_cast(emphasis::underline)) em_codes[2] = 4; - if (em_bits & static_cast(emphasis::strikethrough)) - em_codes[3] = 9; + if (em_bits & static_cast(emphasis::faint)) em_codes[1] = 2; + if (em_bits & static_cast(emphasis::italic)) em_codes[2] = 3; + if (em_bits & static_cast(emphasis::underline)) em_codes[3] = 4; + if (em_bits & static_cast(emphasis::blink)) em_codes[4] = 5; + if (em_bits & static_cast(emphasis::reverse)) em_codes[5] = 7; + if (em_bits & static_cast(emphasis::conceal)) em_codes[6] = 8; + if (em_bits & static_cast(emphasis::strikethrough)) em_codes[7] = 9; size_t index = 0; - for (int i = 0; i < 4; ++i) { + for (int i = 0; i < 7; ++i) { if (!em_codes[i]) continue; buffer[index++] = static_cast('\x1b'); buffer[index++] = static_cast('['); @@ -435,7 +446,7 @@ template struct ansi_color_escape { } private: - Char buffer[7u + 3u * 4u + 1u]; + Char buffer[7u + 3u * 8u + 1u]; static FMT_CONSTEXPR void to_esc(uint8_t c, Char* out, char delimiter) FMT_NOEXCEPT { diff --git a/test/color-test.cc b/test/color-test.cc index 9a38765a..23a4bfc3 100644 --- a/test/color-test.cc +++ b/test/color-test.cc @@ -22,10 +22,17 @@ TEST(color_test, format) { fmt::format(fg(fmt::color::blue) | bg(fmt::color::red), "two color"), "\x1b[38;2;000;000;255m\x1b[48;2;255;000;000mtwo color\x1b[0m"); EXPECT_EQ(fmt::format(fmt::emphasis::bold, "bold"), "\x1b[1mbold\x1b[0m"); + EXPECT_EQ(fmt::format(fmt::emphasis::faint, "faint"), "\x1b[2mfaint\x1b[0m"); EXPECT_EQ(fmt::format(fmt::emphasis::italic, "italic"), "\x1b[3mitalic\x1b[0m"); EXPECT_EQ(fmt::format(fmt::emphasis::underline, "underline"), "\x1b[4munderline\x1b[0m"); + EXPECT_EQ(fmt::format(fmt::emphasis::blink, "blink"), + "\x1b[5mblink\x1b[0m"); + EXPECT_EQ(fmt::format(fmt::emphasis::reverse, "reverse"), + "\x1b[7mreverse\x1b[0m"); + EXPECT_EQ(fmt::format(fmt::emphasis::conceal, "conceal"), + "\x1b[8mconceal\x1b[0m"); EXPECT_EQ(fmt::format(fmt::emphasis::strikethrough, "strikethrough"), "\x1b[9mstrikethrough\x1b[0m"); EXPECT_EQ(