473,602 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2359
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%(n ormal)s text and some
%(blue)sblue%(n ormal)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.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')
----------------------------------------------------------------------------------------------------------
Aug 10 '06 #6

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

Similar topics

7
19247
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: var location = new Array() <cfloop query="loc"> location.name = '#loc.name#' location.address1 = '#loc.address1#'
0
7993
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8404
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8054
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
5867
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5440
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3944
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2418
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1254
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.