Escape sequences (colour) and padding with "%8s"%

A

Anton81

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
 
T

Tim Chase

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?

You omit half of the equation: the contents of str_with_escape.
>>> print "hello %c[1mworld%c[0m" % (27, 27)

works just fine for me in Linux (it chokes a bit on Win32 under
cmd.exe, printing the actual escape character, rather than
interpreting it as an ANSI control sequence).

One can even do fancy stuff like:
>>> mapping = {'bold': '\x1b[1m', 'normal':'\x1b[0m', 'blue':'\x1b[34m'}
>>> print "this has some %(bold)sbold%(normal)s text and some
%(blue)sblue%(normal)s text and some %(bold)s%(blue)stext that is
bold and blue%(normal)s in it" % mapping

and it works with %8s as well.

You might prefer to use the standard curses library rather than
try and roll your own...

-tkc
 
A

Anton81

For example:

print '%8s' % '\x1b[34mTEST\x1b[0m'

doesn't not indent 'TEST' whereas

print '%8s' % TEST'

works.
 
G

Gabriel Genellina

For example:

print '%8s' % '\x1b[34mTEST\x1b[0m'

doesn't not indent 'TEST' whereas

print '%8s' % TEST'

works.

If you insist on building the codes yourself instead of using the
standard curses library...

print '\x1b[34m%8s\x1b[0m' % 'TEST'



Gabriel Genellina
Softlab SRL





__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas
 
A

Anton81

If you insist on building the codes yourself instead of using the
standard curses library...

print '\x1b[34m%8s\x1b[0m' % 'TEST'

Will the curses library help? The problem is I need the colour coded in my
string and the "print pattern" is static.

What's the quickest way to solve this without having special print routines,
that handle "colour string objects"?

Anton
 
A

Anthra Norell

Anton,

See if this suits your purpose: http://cheeseshop.python.org/pypi/SE/2.2 beta
Below the dotted line is how it works.

Frederic


----- Original Message -----
From: "Anton81" <[email protected]>
Newsgroups: comp.lang.python
To: <[email protected]>
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'. ::=(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.
# 4. Stream-Edit'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'
 
D

Dennis Lee Bieber

For example:

print '%8s' % '\x1b[34mTEST\x1b[0m'

doesn't not indent 'TEST' whereas
No surprise -- those escape sequences are counted as characters by
the formatting system... You've just told it to print 13 characters in
an 8 character field. Doesn't fit, so it expands the field...
print '%8s' % TEST'

works.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top