I was working on a python project which will be printing lots of data (lines) to the terminal, and I wanted to highlight if there are any errors printed out.
Java has an easy option, just use
System.err.print() instead of the normal
System.out.print(). Python, not so easy.
After some searching around I found the following gist.
My implementation is as following:
def printWhiteOnRed(s): os.system("echo |set /p out=\u001b[97;101m") print(s) os.system("echo |set /p out=\u001b[0m")
Let’s go through the different expressions
|set /p out=
This is to supress line ending in the command prompt, see this Stack Overflow answer.
This is the ANSI escape character
This are the colours for white foreground on red background.
This is the reset code to ensure any further output is in normal style
Haoyi’s Programming Blog goes in-depth on how this can be played around with.