Bold text through C++ write statement

痞子三分冷 提交于 2019-12-19 11:42:55

问题


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.

Right now I'm outputting the code using:

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.

Edit

Sorry, I forgot to mention that this is for a command line.


回答1:


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.

For instance if you wanted to turn on bold for a VT100 terminal you would output

ESC[1m

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

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.



来源:https://stackoverflow.com/questions/16590635/bold-text-through-c-write-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!