粗体文本通过C ++写语句 [英] Bold text through C++ write statement
问题描述
我通过telnet工作在字典服务器上,我希望以下面的格式返回:
I'm working on a dictionary server via telnet, and I'd like it to return it in this format:
**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() );
所以我想把第一行加粗。
So I'd like that first line to be bolded.
对不起,我忘了提到这是一个命令行。
Sorry, I forgot to mention that this is for a command line.
推荐答案
请考虑使用类似 VT100转义序列。由于您的服务器是基于telnet的用户可能有一个客户端支持各种终端模式。
Consider using using something like VT100 escape sequences. Since your server is telnet based the user is likely to have a client that supports various terminal modes.
例如,如果你想为VT100终端打开粗体,输出
For instance if you wanted to turn on bold for a VT100 terminal you would output
ESC[1m
其中ESC是字符值0x1b。要切换回正常的格式化输出
where "ESC" is the character value 0x1b. To switch back to normal formatting output
ESC[0m
要在您的应用程序中使用它,您可以将问题中的示例行更改为以下内容。
To use this in your application you can change the example lines from your question to the following.
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 查看,虽然如果您需要的只是简单的粗体/ 。
There other terminal modes such as VT52, VT220, etc. You might want to look into using ncurses although it might be a bit heavy if all you need is simple bold on/off.
这篇关于粗体文本通过C ++写语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!