473,405 Members | 2,160 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,405 software developers and data experts.

Colorize expanded tabs

Hi all,
from a string embedding tabs I want to colorize them when expanded:

# Starting from a string:
a= '1234\t5678\t\t90\nqwerty\nasdfg'
# which embeds both tabs and lfs

# printing it you obtain:
print a
# 1234 5678 90
# qwerty
# asdfg

# print automatically expands tabs and interprets the NL.

# to colorize the expanded tabs, I tried:
print a.replace('\t',"\033[41m\t\033[0m") # 41 = red color
# instead it works (in Linux) for the \n (substituted with a red
space):
print a.replace('\n',"\033[41m \033[0m\n")

Can you help me?
Bye.

Dec 4 '05 #1
2 1524
qwweeeit wrote:
Hi all,
from a string embedding tabs I want to colorize them when expanded:

# Starting from a string:
a= '1234\t5678\t\t90\nqwerty\nasdfg'
# which embeds both tabs and lfs

# printing it you obtain:
print a
# 1234 5678 90
# qwerty
# asdfg

# print automatically expands tabs and interprets the NL.

# to colorize the expanded tabs, I tried:
print a.replace('\t',"\033[41m\t\033[0m") # 41 = red color
# instead it works (in Linux) for the \n (substituted with a red
space):
print a.replace('\n',"\033[41m \033[0m\n")

Can you help me?
Bye.


If all else fails you can colorize the tabs after converting them to
spaces:

def splititer(text, token):
# A lazy iter(text.split(token)).
# Probably not worth the effort.
start = 0
while True:
try:
end = text.index(token, start)
except ValueError:
break
yield text[start:end]
start = end + len(token)
yield text[start:]

def colortabs(text, tabcolor, normcolor, tabwidth=8):
parts = splititer(text, "\t")
part = parts.next()
pos = len(part)
yield part
for part in parts:
width = tabwidth - pos % tabwidth
yield tabcolor
yield " " * width
yield normcolor
yield part
pos += width + len(part)

print "\t1234\t5678\t\t90".replace("\t", "\033[41m\t\033[0m")
print "".join(colortabs("\t1234\t5678\t\t90", "\033[41m", "\033[0m"))

Peter

Dec 4 '05 #2
Hi Peter,
thank you for your replay, but I was looking for a very
short routine. I even had in mind to use Linux & bash
(only one command line).
It seems that tab expansion, made by print, prevents
the working of the escape sequences for colors.
In fact, if you replace tab with a given number of spaces,
all works well.
That is :
print "\t1234\t5678\t\t90".replace("\t", "\033[41m \033[0m")
correctly colorizes in red the spaces replacing the tabs...
Unfortunately this is not tab expansion!
Bye.

Dec 4 '05 #3

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

Similar topics

5
by: tanpan | last post by:
Hi i'm looking for free php code which will colorize my html source (structure). please help me if u can :) pozdr puipui
0
by: Francois Verbeeck | last post by:
Dear UseNet readers, Does anyone have any idea on how to colorize selected checkbox in checkboxlist control ? I've quite a huge checkboxlist (approximatively one full screen) and, to improve...
2
by: qwweeeit | last post by:
Hi all, in a previous post I asked help for colorizing expanded tab. I wanted to list text files showing in colors LFs and the expanded tabs. I hoped to use only bash but, being impossible, I...
0
by: Christian Perthen | last post by:
Hi, How do I get Visual Studio 2005 to colorize vbscript syntax as it does in InterDev? I am getting tired of having both apps launched. Thanks Christian
135
by: Xah Lee | last post by:
Tabs versus Spaces in Source Code Xah Lee, 2006-05-13 In coding a computer program, there's often the choices of tabs or spaces for code indentation. There is a large amount of confusion about...
0
by: Marius Matioc | last post by:
New in VS2005 is the colorize inactive feature. However, it seems not to be able to handle _DEBUG. Sections of code bracketed by "#ifdef _DEBUG" are grayed out (or not) in an unpredictable...
4
by: MrNobody | last post by:
Let's say I have an image which is monochrome with areas of white, black and shades of gray. Now I want to turn all the white parts into some RGB color but it also needs to be darkened by the gray...
6
by: Stephen Edwards | last post by:
I am a novice, so bear with me. I designed a website using CSS TABS, all was fine, then I copy and pasted meta name, etc for keywords, tweaking it where necessary for keywords. I did not notice...
7
by: Phil Reynolds | last post by:
I'm using a tab control in Access 2000, and the user requested to have buttons in the form header, instead of the built-in tabs (so that when they scroll down, they can still switch tabs). Now,...
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...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.