I already knew about the color escapes, I used them in my bash prompt a while ago. Thanks anyway.
What I wanted was to integrate it with the logging module, which I eventually did after a couple of tries and errors.
Here is what I end up with:
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
#The background is set with 40 plus the number of the color, and the foreground with 30
#These are the sequences need to get colored ouput
RESET_SEQ = "\033[0m"
COLOR_SEQ = "\033[1;%dm"
BOLD_SEQ = "\033[1m"
def formatter_message(message, use_color = True):
if use_color:
message = message.replace("$RESET", RESET_SEQ).replace("$BOLD", BOLD_SEQ)
else:
message = message.replace("$RESET", "").replace("$BOLD", "")
return message
COLORS = {
'WARNING': YELLOW,
'INFO': WHITE,
'DEBUG': BLUE,
'CRITICAL': YELLOW,
'ERROR': RED
}
class ColoredFormatter(logging.Formatter):
def __init__(self, msg, use_color = True):
logging.Formatter.__init__(self, msg)
self.use_color = use_color
def format(self, record):
levelname = record.levelname
if self.use_color and levelname in COLORS:
levelname_color = COLOR_SEQ % (30 + COLORS[levelname]) + levelname + RESET_SEQ
record.levelname = levelname_color
return logging.Formatter.format(self, record)
And to use it, create your own Logger:
# Custom logger class with multiple destinations
class ColoredLogger(logging.Logger):
FORMAT = "[$BOLD%(name)-20s$RESET][%(levelname)-18s] %(message)s ($BOLD%(filename)s$RESET:%(lineno)d)"
COLOR_FORMAT = formatter_message(FORMAT, True)
def __init__(self, name):
logging.Logger.__init__(self, name, logging.DEBUG)
color_formatter = ColoredFormatter(self.COLOR_FORMAT)
console = logging.StreamHandler()
console.setFormatter(color_formatter)
self.addHandler(console)
return
logging.setLoggerClass(ColoredLogger)
Just in case anyone else needs it.
Be careful if you're using more than one logger or handler: ColoredFormatter
is changing the record object, which is passed further to other handlers or propagated to other loggers. If you have configured file loggers etc. you probably don't want to have the colors in the log files. To avoid that, it's probably best to simply create a copy of record
with copy.copy()
before manipulating the levelname attribute, or to reset the levelname to the previous value, before returning the formatted string (credit to Michael in the comments).
You should use:
holder.text.setTextColor(Color.RED);
You can use various functions from the Color
class to get the same effect of course.
Color.parseColor
(Manual) (like LEX uses)
text.setTextColor(Color.parseColor("#FFFFFF"));
Color.rgb
and Color.argb
(Manual rgb) (Manual argb) (like Ganapathy uses)
holder.text.setTextColor(Color.rgb(200,0,0));
holder.text.setTextColor(Color.argb(0,200,0,0));
And of course, if you want to define your color in an XML
file, you can do this:
<color name="errorColor">#f00</color>
because the getColor()
function is deprecated1, you need to use it like so:
ContextCompat.getColor(context, R.color.your_color);
You can also insert plain HEX, like so:
myTextView.setTextColor(0xAARRGGBB);
Where you have an alpha-channel first, then the color value.
Check out the complete manual of course, public class Color extends Object.
1This code used to be in here as well:
textView.setTextColor(getResources().getColor(R.color.errorColor));
This method is now deprecated in Android M. You can however use it from the contextCompat in the support library, as the example now shows.
Best Answer
Below you can find colors reference of text to command when running node.js application:
Note
%s
is where in the string (the second argument) gets injected.\x1b[0m
resets the terminal color so it doesn't continue to be the chosen color anymore after this point.Colors reference
EDIT:
For example,
\x1b[31m
is an escape sequence that will be intercepted by your terminal and instructs it to switch to the red color. In fact,\x1b
is the code for the non-printable control characterescape
. Escape sequences dealing only with colors and styles are also known as ANSI escape code and are standardized, so therefore they (should) work on any platform.Wikipedia has a nice comparison of how different terminals display colors https://en.wikipedia.org/wiki/ANSI_escape_code#Colors