Might need a custom terminal to get this sort of functionality in all circumstances but for things like
tail -f the following
python3 script should work:
from termcolor import cprint
print("Error: please install the python3-termcolor package")
"""Tabs don't seem to be highlighted so expand them to spaces"""
result = ""
col = 0
for c in line:
if c == "\t":
next_col = 8 * ((col // 8) + 1)
result += " " * (next_col - col)
col = next_col
result += c
col += 1
def pad(line, n):
"""Pad a line until it is a multiple of n, to avoid jagged highlighting"""
while (len(line) % n) != 0:
line += " "
# Get the width of the terminal
cols, rows = shutil.get_terminal_size()
odd = True
for line in sys.stdin:
cprint(pad(expandtabs(line.rstrip()), cols), "white", "on_grey")
cprint(pad(expandtabs(line.rstrip()), cols), "grey", "on_white")
odd = not odd
If saved as
zebra.py someplace in your path, it can be added to commands such as:
cat /proc/cpuinfo | zebra.py
tail -f /var/log/kern.log | zebra.py
It requires the
python3-termcolor package and at least Python 3.3 to use the
shutil.get_terminal_size function. There is plenty of room for improvement, could try the
python3-xtermcolor package to gain more than just the few colors at https://pypi.python.org/pypi/termcolor, add functionality to read files from the command line instead of just
I'm not sure it makes output much easier to read, but it certainly looks neat.