通过 C++ write 语句将文本加粗

JSh*_*hoe 2 c++ sockets formatting networking command-line-interface

我正在通过 telnet 在字典服务器上工作,我希望它以这种格式返回:

  **word** (wordType): wordDef wordDef wordDef wordDef
wordDef wordDef wordDef.
Run Code Online (Sandbox Code Playgroud)

现在我使用以下方式输出代码:

write( my_socket, ("%s", word.data()    ), word.length()    ); // Bold this
write( my_socket, ("%s", theRest.data() ), theRest.length() );
Run Code Online (Sandbox Code Playgroud)

所以我希望第一行加粗。

编辑

抱歉,我忘了提及这是针对命令行的。

Cap*_*ous 5

考虑使用VT100 转义序列之类的东西。由于您的服务器是基于 telnet 的,因此用户可能拥有支持各种终端模式的客户端。

例如,如果您想为 VT100 终端打开粗体,您将输出

ESC[1m
Run Code Online (Sandbox Code Playgroud)

其中“ESC”是字符值 0x1b。切换回正常格式输出

ESC[0m
Run Code Online (Sandbox Code Playgroud)

要在您的应用程序中使用它,您可以将问题中的示例行更改为以下内容。

std::string str = "Hello!"
write( my_socket, "\x1b[1m", 4); // Turn on bold formatting
write( my_socket, str.c_str(), str.size()); // output string
write( my_socket, "\x1b[0m", 4); // Turn all formatting off
Run Code Online (Sandbox Code Playgroud)

还有其他终端模式,例如 VT52、VT220 等。您可能想考虑使用ncurses,尽管如果您需要的只是简单的粗体开/关,它可能有点繁重。