Priting Out in COLOURS

Miki Tebeka miki.tebeka at zoran.com
Thu Jun 10 09:12:55 CEST 2004


Hello Satish,

<shamless plug>
>    This must be a simple thing to do, but am not sure about it. I like to
>    know how I can print my statements out,  in colour.
My web site is down so I'm adding the code in the mail.

HTH.

Bye.
--
-------------------------------------------------------------------------
Miki Tebeka <miki.tebeka at zoran.com>
The only difference between children and adults is the price of the toys.

--- ansiprint.py ---

#!/usr/bin/env python
'''Print message using ANSI terminal codes'''

__author__ = "Miki Tebeka <miki.tebeka at zoran.com>"
# $Id: ansiprint.py,v 1.1 2003/09/07 13:26:28 mikit Exp $

# Copyright (c) Miki Tebeka <miki.tebeka at zoran.com> 
# This file is under the GNU Public License (GPL), see
# http://www.gnu.org/copyleft/gpl.html for more details

from sys import stdout, stderr

# Format
bright = 1
dim = 2
underline = 4
blink = 5
reverse = 7
hidden = 8

# Forground
black = 30
red = 31
green = 32
yellow = 33
blue = 34
magenta = 35
cyan = 36
white = 37

# Background
on_black = 40
on_red = 41
on_green = 42
on_yellow = 43
on_blue = 44
on_magenta = 45
on_cyan = 46
on_white = 47

def ansiformat(msg, *args):
    '''Format msg according to args. 

    See http://www.termsys.demon.co.uk/vtansi.htm for more details/
    '''
    return "\033[%sm%s\033[0m" % (";".join(["%s" % f for f in args]), msg)

def ansiprint(msg, *args, **kw):
    '''Print formatted message.

    Should work on ANSI compatible terminal.
    '''

    if kw.get("stderr", False):
        outfo = stderr
    else:
        outfo = stdout
        
    outfo.write(ansiformat(msg, *args))
    outfo.flush()

if __name__ == "__main__":
    from sys import argv, exit
    from os.path import basename
    
    h = {
        "bright" : bright,
        "dim" : dim,
        "underline" : underline,
        "blink" : blink,
        "reverse" : reverse,
        "hidden" : hidden,
        "black" : black,
        "red" : red,
        "green" : green,
        "yellow" : yellow,
        "blue" : blue,
        "magenta" : magenta,
        "cyan" : cyan,
        "white" : white,
        "on_black" : on_black,
        "on_red" : on_red,
        "on_green" : on_green,
        "on_yellow" : on_yellow,
        "on_blue" : on_blue,
        "on_magenta" : on_magenta,
        "on_cyan" : on_cyan,
        "on_white" : on_white
    }

    eg = "e.g. ansiprint hello red on_green underline -> %s" % \
        ansiformat("hello", red, on_green, underline)
    
    # Check command line
    if len(argv) < 2:
        print >> stderr, "usage: %s message [format ...]" % basename(argv[0])
        print >> stderr, eg
        exit(1)
    for i in argv[2:]:
        if i not in h:
            ansiprint("%s: Unknown format\n" % i, red, bright, stderr=True)
            print >> stderr, "Formats can be:",
            msg = ", ".join([ansiformat(f, h[f]) for f in h.keys()])
            print msg
            print >> stderr, eg 
            exit(1)

    # Print
    ansiprint(argv[1], *[h[i] for i in argv[2:]])
    print

--- ansiprint.py ---




More information about the Python-list mailing list