问题
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