Here's my python version which has one new feature. It can be given a string argument in which case it will only highlight lines in its input that contain that string.
#!/usr/bin/python -u
import sys
REG = "\x1b[0m"
INV = "\x1b[7m"
try:
pat = sys.argv[1]
except IndexError:
pat = None
i = 0
while 1:
line = sys.stdin.readline()
if not line:
break
if pat:
if pat in line:
print INV, line,
else:
print REG, line,
else:
if i % 2 == 0:
print INV, line,
else:
print REG, line,
i = i + 1
if i:
print REG
It's important to use unbuffered I/O, thus the "-u" flag to python is used along with sys.stdin.readline().
2 comments:
Ah! This is very cool!
I think I will be using this quite a lot.
Thank you!
I tried a rewrite of this using some techniques to make the code shorter, but I can't find a way to get formatted code into your blog comments.
my version
Post a Comment