Posted on

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")
    os.system("echo |set /p out=\u001b[0m")

Let’s go through the different expressions

  1. |set /p out= This is to supress line ending in the command prompt, see this Stack Overflow answer.
  2. \u001b This is the ANSI escape character
  3. [97;101m This are the colours for white foreground on red background.
  4. [0m 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.


comments powered by Disqus