Sophie

Sophie

distrib > Mageia > 1 > i586 > by-pkgid > 5179e1fd279995c99a851f747cf31e4c > files > 378

nagios-check_mk-1.1.8-1.mga1.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])