How to change text color & background in mac terminal?

a 夏天 提交于 2019-12-23 14:55:39

问题


I'm writing a program that needs to write text in different colors. I have the escape code RED "\e[31m". However, if I make red on a red background, that won't work. So I also want to change the background color. I've tried using this "\e[31m\e[94m" or even \e[4Xm to do text on a different background, however, this isn't working. Any ideas to get red text on a white background?

This is in the format std::cout << "COLORNAME" ....

Thanks!


回答1:


Multiple SGR options can be chained together, and in fact more compactly than you've been trying. The SGR syntax takes the general form of:

\e[ options m

where options can consist of one* or more numbers separated by semicolons, which can include a number of things, but especially:

  • 1: bold
  • 30 – 37: set foreground color 0 – 7 (black, red, green, yellow, blue, magenta, cyan, white)
  • 40 – 47: set background color 0 – 7 (ditto)
  • 90 – 97: set foreground color 8 – 15 (bright versions of 0 – 7; "bright black" is a dark gray)
  • 100 – 107: set background color 8 – 15 (ditto)

In your case, red text on a white background would be \e[31;47m.


(*: Or no numbers at all, in which case it resets all graphical options.)



来源:https://stackoverflow.com/questions/25089180/how-to-change-text-color-background-in-mac-terminal

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