changeset 0:df03fa355b60

first working version
author paulo@twcdns.fastsearch.net
date Sat, 27 Mar 2010 00:56:28 -0700
parents
children 2b95aa9e4e46
files rcg.py
diffstat 1 files changed, 55 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rcg.py	Sat Mar 27 00:56:28 2010 -0700
     1.3 @@ -0,0 +1,55 @@
     1.4 +#!/usr/bin/env python
     1.5 +
     1.6 +import sys
     1.7 +import re
     1.8 +import getopt
     1.9 +
    1.10 +#x = "\x1B[1;31;49m" + x
    1.11 +#x = START + c["white"][0] + ";" + c["blue"][1] + END + x + CLEAR
    1.12 +
    1.13 +START = "\x1B["
    1.14 +BOLD = "1;"
    1.15 +END = "m"
    1.16 +CLEAR = START + "0" + END
    1.17 +
    1.18 +c = {
    1.19 +	# color : (fg, bg)
    1.20 +	"black" : ("30", "40"),
    1.21 +	"red" : ("31", "41"),
    1.22 +	"green" : ("32", "42"),
    1.23 +	"brown" : ("33", "43"),
    1.24 +	"blue" : ("34", "44"),
    1.25 +	"magenta" : ("35", "45"),
    1.26 +	"cyan" : ("36", "46"),
    1.27 +	"white" : ("37", "47"),
    1.28 +	"default" : ("39", "49"),
    1.29 +}
    1.30 +
    1.31 +bold = False
    1.32 +fg = None
    1.33 +bg = None
    1.34 +
    1.35 +(optvals, args) = getopt.getopt(sys.argv[1:], "f:b:B")
    1.36 +
    1.37 +for (opt, val) in optvals:
    1.38 +	if opt == '-B':
    1.39 +		bold = True
    1.40 +	elif opt == '-f':
    1.41 +		fg = val
    1.42 +	elif opt == '-b':
    1.43 +		bg = val
    1.44 +
    1.45 +if len(args) > 0:
    1.46 +	for line in sys.stdin:
    1.47 +		repl = START
    1.48 +		if bold:
    1.49 +			repl += BOLD
    1.50 +		if fg is not None:
    1.51 +			repl += c[fg][0]
    1.52 +		if bg is not None:
    1.53 +			repl += ';' + c[bg][1]
    1.54 +		repl += END + r'\1' + CLEAR
    1.55 +		x = re.sub(r'(' + args[0] + r')', repl, line)
    1.56 +		
    1.57 +		sys.stdout.write(x)
    1.58 +		sys.stdout.flush()