Escape sequences (colour) and padding with "%8s"%
Anthra Norell
anthra.norell at tiscalinet.ch
Thu Aug 10 06:44:33 EDT 2006
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" <forum at anton.e4ward.com>
Newsgroups: comp.lang.python
To: <python-list at python.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_escape'.
>>> ansi_escapes = '''
::=(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_text = Ansiizer (marked_up_text)
>>> ansiized_text
'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_definitions.se'.
>>> Ansiizer = SE.SE ('your_path/ansi_definitions.se')
----------------------------------------------------------------------------------------------------------
# You can do files directly
>>> Ansiizer ('some_path/text_file', 'some_path/text_file_ansiized')
----------------------------------------------------------------------------------------------------------
More information about the Python-list
mailing list