Colored Output in Console with Python

Playing around with ANSI in a color capable terminal.

#!/usr/bin/env python
import sys

BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)

#following from Python cookbook, #475186
def has_colours(stream):
    if not hasattr(stream, "isatty"):
        return False
    if not stream.isatty():
        return False # auto color only on TTYs
    try:
        import curses
        curses.setupterm()
        return curses.tigetnum("colors") > 2
    except:
        # guess false in case of error
        return False
has_colours = has_colours(sys.stdout)


def printout(text, colour=WHITE):
        if has_colours:
                seq = "\x1b[1;%dm" % (30+colour) + text + "\x1b[0m"
                sys.stdout.write(seq)
        else:
                sys.stdout.write(text)

A simple demo:

<code python>
#
# Test
#
printout("[debug]   ", GREEN)
print("in green")
printout("[warning] ", YELLOW)
print("in yellow")
printout("[error]   ", RED)
print("in red")

#terminal, #python - Posted in the Dev category