Sophie

Sophie

distrib > Fedora > 18 > i386 > by-pkgid > 16c5e5f8b5816c1b1037e59c9a2a8112 > files > 458

check-mk-docs-1.2.2-5.fc18.1.noarch.rpm

#!/usr/bin/python
# This is like egrep, but hilites the exact positions
# of *subexpressions*. This is e.g. usefull for testing
# inventory_processes...

import re, sys

def color(x):
    if x == 0:
	return "\033[0m"
    else:
	return "\033[1;3%dm" % x

def handle_line(r, line):
    m = r.search(line)
    if m:
        start, end = m.span(0)
        spans = []
	if m.lastindex:
	    for i in range(1, m.lastindex+1):
	        spans.append(m.span(i))
	out = ""
	out += line[0:start]
	pos = start
	for b, e in spans:
	    out += color(4)
	    out += line[pos:b]
	    out += color(1)
	    out += line[b:e]
	    pos = e
        out += color(4)
	out += line[pos:end]
	out += color(0)
	out += line[end:]
	print out

r = re.compile(sys.argv[1])
for line in sys.stdin:
    handle_line(r, line[:-1])