The colorama module (version 0.4.1) allows us to easily create colored Terminal text. We're going to use this to highlight good and bad events to the user. For example, when a plugin completes without errors, we display that with a green font. Similarly, we will print encountered errors in red. The colorama module can be installed with pip:
pip install colorama==0.4.1
Traditionally, printing colored text to the Terminal is achieved by a series of escape characters on Linux or macOS systems. This, however, won't work for Windows operating systems. The following are examples of ANSI escape characters being used to create colored text in Linux or macOS Terminals:
The color format is the escape character, \033, followed by an open bracket and then the desired color code. We can change the background color in addition to the foreground color and even do both at the same time by separating the codes with a semicolon. The color code, 31m, sets the foreground text to red. The color code, 47m, sets the background to white. Notice in the second example, in the preceding screenshot, m designates the end of the color codes and should therefore only follow the final color code.
We can use the colorama and call built-in variables, which are aliases for the desired ANSI codes. This makes our code more readable and best of all works with Windows Command Prompts after calling colorama.init() at the beginning of your script:
The colorama module has three main formatting options: Fore, Back, and Style. These allow us to make changes to the foreground or background text color and its style, respectively. The colors available for the foreground and background include: black, red, green, yellow, blue, magenta, cyan, and white.
It's possible to change other text properties using ANSI escape characters, such as if we wanted to make the text dimmer or brighter. ANSI color codes and other information on the colorama library is available at https://pypi.python.org/pypi/colorama.