Anton,
See if this suits your purpose:
http://cheeseshop.python.org/pypi/SE/2.2%20beta
Below the dotted line is how it works.
Frederic
----- Original Message -----
From: "Anton81" <fo***@anton.e4 ward.com>
Newsgroups: comp.lang.pytho n
To: <py*********@py thon.org>
Sent: Wednesday, August 09, 2006 7:48 PM
Subject: Escape sequences (colour) and padding with "%8s"%
Hi all!
I used escape sequences to produce colour output, but a construct like
print "%8s" % str_with_escape
doesn't do the right thing. I suppose the padding counts the escape
characters, too.
What could be a solution?
Anton
----------------------------------------------------------------------------------------------------------
# 1. List your ansi codes and invent concise place holders. Define each one like this 'symbol=ansi_es cape'.
>>ansi_escape s = '''
::=(x1b)[0m # Cancel all previous escapes
:B:=(x1b)[1m # bold
:blue:=(x1b)[34m
# etc.
'''
# 2. Mark your text up with your symbols
marked_up_text = "this has some :B:bold:: text and some :blue:blue:: text and some :B::blue:text that is bold and blue::"
# 3 Make an SE Stream Editor.
>>import SE
Ansiizer = SE.SE (ansi_escapes)
# 4. Stream-Edit
>>ansiized_te xt = Ansiizer (marked_up_text )
ansiized_te xt
'this has some \x1b[1mbold\x1b[0mtext and some \x1b[34mblue\x1b[0m text and some \x1b[1m\x1b[34mtext that is bold and blue\x1b[0m'
----------------------------------------------------------------------------------------------------------
# Alternatively you may first write your definitions into a file. You then make your ansiizing Stream-Editor simply by naming the
file, say 'your_path/ansi_definition s.se'.
>>Ansiizer = SE.SE ('your_path/ansi_definition s.se')
----------------------------------------------------------------------------------------------------------
# You can do files directly
>>Ansiizer ('some_path/text_file', 'some_path/text_file_ansii zed')
----------------------------------------------------------------------------------------------------------