1

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

  **word** (wordType): wordDef wordDef wordDef wordDef
wordDef wordDef wordDef.

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

write( my_socket, ("%s", word.data()    ), word.length()    ); // Bold this
write( my_socket, ("%s", theRest.data() ), theRest.length() );

所以我希望第一行加粗。

编辑

对不起,我忘了说这是一个命令行。

4

1 回答 1

4

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

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

ESC[1m

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

ESC[0m

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

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

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

于 2013-05-16T15:16:36.580 回答