ANSI SGR in Python

The code in this gist gives an easy way to use ANSI SGR parameters in terminal using Python.

The code is lightweight and thou it also already has an example I’ll explain a bit more how to use it.


Examples

First we need to import the module:

from ansi_sgr import *

The example below prints all defined SGR codes as their corresponding styles (RED as red, and BOLD as bold):

for idx, code in enumerate(CODE_MAP):
    if code:
        print ftt(code, idx)

This example combines multiple SGR codes and produces yellow bold text on green background:

print ftt('Yellow bold text on green background', 'YELLOW', 'BOLD', 'BG-GREEN')

Also, the order is irrelevant, the code below gives same result as the code above:

print ftt('The order is irrelevant', 'BG-GREEN', 'YELLOW', 'BOLD')

The last code of same type takes precedence:

print ftt("As long as same type of style isn't overwritten", 'BG-GREEN', 'YELLOW', 'BOLD', 'BG-RED')