473,378 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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
Aug 9 '06 #1
5 2345
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

Aug 9 '06 #2
For example:

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

doesn't not indent 'TEST' whereas

print '%8s' % TEST'

works.
Aug 10 '06 #3
At Thursday 10/8/2006 07:04, Anton81 wrote:
>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

Aug 10 '06 #4
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
Aug 10 '06 #5
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.e4ward.com>
Newsgroups: comp.lang.python
To: <py*********@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')
----------------------------------------------------------------------------------------------------------
Aug 10 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: James Johnson | last post by:
Are there structs in JavaScript? If not, what's the closest thing, or do I just use parallel arrays? I'm populating a JavaScript array from ColdFusion query, but I don't think I can do this: ...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.