rang - Colors for your Terminal¶
Note
This is an adaptation of original rang documentation (see rang). The code now is not header only as the original one to avoid windows header conflict in complex projects.
Example usage
#include "rang.hpp"
using namespace std;
using namespace rang;
int main()
{
cout << "Plain old text"
<< style::bold << "Rang styled text!!"
<< style::reset << endl;
}
Rang uses iostream objects - cout/clog/cerr to apply
attributes to output text. Since rang aims to support both windows and
unix like systems, it takes care of the os specific details and tries to
provide a uniform interface. Due to incompatiblities b/w different OS
versions, not all kinds of attributes are supported on every system so
rang will try to skip the ones which might produce garbage(instead of
pushing random ANSI escape codes on your streams). Detection of tty is
also handled internally so you don’t need to check if application user
might redirect output to a file.
Need support for non-ansi terminals? Check outTermdbwhich supports virtually all terminals and their capablities.
Apart from setting text attributes, you can also ask rang to override its default behaviour through these methods -
void rang::setControlMode(rang::control);
where rang::control takes - control::Auto - Automatically
detects whether terminal supports color or not(Default) -
control::Off - Turn off colors completely - control::Force -
Force colors even if terminal doesn’t supports them or output is
redirected to non-terminal
void rang::setWinTermMode(rang::winTerm);
where rang::winTerm takes - winTerm::Auto - Checks for newer
windows and picks Ansi otherwise falls back to Native(Default) -
winTerm::Native - This method is supported in all versions of
windows but supports less attributes - winTerm::Ansi - This method
is supported in newer versions of windows and supports rich variety of
attributes
Supported attributes with their compatiblity are listed below -
Text Styles:
Code |
Linux/Win/Others |
Old Win |
|---|---|---|
|
yes |
yes |
|
yes |
no |
|
yes |
no |
|
yes |
no |
|
no |
no |
|
no |
no |
|
yes |
yes |
|
maybe |
yes |
|
yes |
no |
Text Color:
Code |
Linux/Win/Others |
Old Win |
|---|---|---|
|
yes |
yes |
|
yes |
yes |
|
yes |
yes |
|
yes |
yes |
|
yes |
yes |
|
yes |
yes |
|
yes |
yes |
|
yes |
yes |
Background Color:
Code |
Linux/Win/Others |
Old Win |
|---|---|---|
|
yes |
yes |
|
yes |
yes |
|
yes |
yes |
|
yes |
yes |
|
yes |
yes |
|
yes |
yes |
|
yes |
yes |
|
yes |
yes |
Bright Foreground Color:
Code |
Linux/Win/Others |
Old Win |
|---|---|---|
|
yes |
yes |
|
yes |
yes |
|
yes |
yes |
|
yes |
yes |
|
yes |
yes |
|
yes |
yes |
|
yes |
yes |
|
yes |
yes |
Bright Background Color:
Code |
Linux/Win/Others |
Old Win |
|---|---|---|
|
yes |
yes |
|
yes |
yes |
|
yes |
yes |
|
yes |
yes |
|
yes |
yes |
|
yes |
yes |
|
yes |
yes |
|
yes |
yes |
Reset Styles/Colors:
Code |
Linux/Win/Others |
Old Win |
|---|---|---|
|
yes |
yes |
|
yes |
yes |
|
yes |
yes |
My terminal is not detected/gets garbage output!
Check your env variable TERM’s value. Then open an issue
here and make sure to
mention TERM’s value along with your terminal name.
Redirecting ``cout``/``cerr``/``clog`` rdbuf?
Rang doesn’t interfere if you try to redirect cout/cerr/clog
to somewhere else and leaves the decision to the library user. Make sure
you’ve read this
conversation
and check out the example code
here.