#!/bin/kyberkits

Your screams won't be heard in the server room

Hello, my name is Günter!

I'm a system administrator, software engineer, and open source software enthusiast from Estonia.

ANSI Select Graphic Rendition in Python
February 3, 2015
python, ansi sgr, terminal style, terminal color


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')

Hello World!
January 1, 1970
hello world, python


Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus sed efficitur ligula. Pellentesque ex nunc, pellentesque sit amet enim vel, auctor placerat lectus. Nullam a imperdiet dolor. Aenean tempor massa at ligula mollis ullamcorper. Curabitur porttitor, nisl in lacinia facilisis, nisl urna mattis enim, nec consequat purus est et nibh.

Integer at congue mauris. Phasellus lacus mauris, ornare nec aliquet ut, dictum non nunc. Duis sed nisi eget risus facilisis facilisis eu at leo. Ut dui ipsum, aliquam vitae mi eget, tincidunt rutrum nulla. Phasellus sit amet eleifend quam, non dapibus nisi. Praesent nec magna sit amet sapien mattis bibendum. Sed lobortis faucibus turpis vitae tempus. Etiam at quam elit. Fusce ut dui iaculis, maximus mauris blandit, vulputate ex. Interdum et malesuada fames ac ante ipsum primis in faucibus. Phasellus volutpat turpis non elementum egestas.

Hello Python World

def hello(s):
    return ' '.join(["hello", s]).title()

print hello("world!")