Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more emphases #2302

Closed
data-man opened this issue May 21, 2021 · 2 comments · Fixed by #2394
Closed

Add more emphases #2302

data-man opened this issue May 21, 2021 · 2 comments · Fixed by #2394

Comments

@data-man
Copy link

int main() {
  fmt::print("normal\n");
  fmt::print(fmt::emphasis::bold,             "bold\n");
  fmt::print(fmt::emphasis::faint,            "faint\n");
  fmt::print(fmt::emphasis::italic,           "italic\n");
  fmt::print(fmt::emphasis::underline,        "underline\n");
  fmt::print(fmt::emphasis::blink,            "blink\n");
  fmt::print(fmt::emphasis::invert,           "invert\n");
  fmt::print(fmt::emphasis::conceal,          "conceal\n");
  fmt::print(fmt::emphasis::strikethrough,    "strikethrough\n");
}

ksnip_20210521-185715_myfmtlib
Really blinked in a real terminal :)
And the conceal style isn't supported by many terminals.

diff --git a/include/fmt/color.h b/include/fmt/color.h
index 8cddbfe1..ab7e8dd0 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 <typename Char> struct ansi_color_escape {
       return;
     }
 
-    for (int i = 0; i < 7; i++) {
+    for (int i = 0; i < 8; i++) {
       buffer[i] = static_cast<Char>(esc[i]);
     }
     rgb color(text_color.value.rgb_color);
@@ -409,16 +417,19 @@ template <typename Char> struct ansi_color_escape {
     buffer[19] = static_cast<Char>(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<uint8_t>(em);
     if (em_bits & static_cast<uint8_t>(emphasis::bold)) em_codes[0] = 1;
-    if (em_bits & static_cast<uint8_t>(emphasis::italic)) em_codes[1] = 3;
-    if (em_bits & static_cast<uint8_t>(emphasis::underline)) em_codes[2] = 4;
-    if (em_bits & static_cast<uint8_t>(emphasis::strikethrough))
-      em_codes[3] = 9;
+    if (em_bits & static_cast<uint8_t>(emphasis::faint)) em_codes[1] = 2;
+    if (em_bits & static_cast<uint8_t>(emphasis::italic)) em_codes[2] = 3;
+    if (em_bits & static_cast<uint8_t>(emphasis::underline)) em_codes[3] = 4;
+    if (em_bits & static_cast<uint8_t>(emphasis::blink)) em_codes[4] = 5;
+    if (em_bits & static_cast<uint8_t>(emphasis::reverse)) em_codes[5] = 7;
+    if (em_bits & static_cast<uint8_t>(emphasis::conceal)) em_codes[6] = 8;
+    if (em_bits & static_cast<uint8_t>(emphasis::strikethrough)) em_codes[7] = 9;

     size_t index = 0;
-    for (int i = 0; i < 4; ++i) {
+    for (int i = 0; i < 8; ++i) {
       if (!em_codes[i]) continue;
       buffer[index++] = static_cast<Char>('\x1b');
       buffer[index++] = static_cast<Char>('[');
@@ -435,7 +446,7 @@ template <typename Char> 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 {
@vitaut
Copy link
Contributor

vitaut commented May 21, 2021

Sounds like a good idea. Could you submit a PR?

@data-man
Copy link
Author

I tried even more emphases (with emphasis : uint16_t ) from https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters

konsole supports Overlined, some terminals supports Doubly underlined...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants