Is it possible to display text in a console with a strike-through effect?

后端 未结 2 602
小蘑菇
小蘑菇 2021-01-04 07:59

I have already looked into ANSI escape codes, but it looks like only underlining is supported.

Do I miss something or is there another option?

If it is not p

2条回答
  •  误落风尘
    2021-01-04 09:01

    An alternative solution for applications written in C11 or C++11 is to use the Unicode combining long stroke overlay character.

    In C++11 you can write code something like this:

    #include 
    #include 
    
    std::string strikethrough(const std::string& text) {
      std::string result;
      for (auto ch : text) {
        result.append(u8"\u0336");
        result.push_back(ch);
      }
      return result;
    }
    
    int main() {
      std::cout << strikethrough("strikethrough") << std::endl;
    }
    

    The code prefixes each character in the input text with the stroke overlay \u0336. Note that the function assumes that text is encoded in a singlebyte encoding such as ASCII or Latin. If the input is in UTF-8 it must be converted to UTF-32 first to get the character boundaries.

    The output then is s̶t̶r̶i̶k̶e̶t̶h̶r̶o̶u̶g̶h in a UTF-8 capable terminal. I don't know why the first character has no strike-through, must be a terminal issue. I could work around this by printing at least one character before the strikethrough function call.

    The Unicode solution also generates a slightly different locking in my terminal (terminator) compared to the ANSI escape sequence mentioned above. The former renders the line exactly in the middle of the text whereas the latter renders it a bit below.

提交回复
热议问题