2008-04-08

spice up your tail minus effs with greenbar

I've been missing this tool for a while now. The original "greenbar" was in written in awk, probably by my friend Mark S. It took input and inverted the text of every other line (like greenbar printer paper). It made watching log files scroll up your terminal much easier to parse.

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:

Anonymous said...

Ah! This is very cool!
I think I will be using this quite a lot.

Thank you!

Anonymous said...

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