Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > e5dacb39141c2088e2c30e21fa0b2b06 > files > 86

nagios-check_mk-doc-1.2.3i1-3.mga4.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])