annotate rcg.py @ 19:ddc56395ee8b

update index.* documentation with usage text
author paulo@localhost
date Thu, 29 Mar 2012 00:06:10 -0700
parents 6627edb46f36
children
rev   line source
paulo@0 1 #!/usr/bin/env python
paulo@0 2
paulo@17 3 # rcg.py -- regex colored grep
paulo@17 4 # Copyright (C) 2010 by Paulo Ang (pbba13@gmail.com)
paulo@17 5 #
paulo@17 6 # This program is free software: you can redistribute it and/or modify
paulo@17 7 # it under the terms of the GNU General Public License as published by
paulo@17 8 # the Free Software Foundation, either version 3 of the License, or
paulo@17 9 # (at your option) any later version.
paulo@17 10 #
paulo@17 11 # This program is distributed in the hope that it will be useful,
paulo@17 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
paulo@17 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
paulo@17 14 # GNU General Public License for more details.
paulo@17 15 #
paulo@17 16 # You should have received a copy of the GNU General Public License
paulo@17 17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
paulo@17 18
paulo@17 19
paulo@0 20 import sys
paulo@0 21 import re
paulo@0 22 import getopt
paulo@0 23
paulo@0 24 #x = "\x1B[1;31;49m" + x
paulo@0 25 #x = START + c["white"][0] + ";" + c["blue"][1] + END + x + CLEAR
paulo@0 26
paulo@0 27 START = "\x1B["
paulo@0 28 BOLD = "1;"
paulo@0 29 END = "m"
paulo@0 30 CLEAR = START + "0" + END
paulo@0 31
paulo@0 32 c = {
paulo@0 33 # color : (fg, bg)
paulo@0 34 "black" : ("30", "40"),
paulo@0 35 "red" : ("31", "41"),
paulo@0 36 "green" : ("32", "42"),
paulo@0 37 "brown" : ("33", "43"),
paulo@0 38 "blue" : ("34", "44"),
paulo@0 39 "magenta" : ("35", "45"),
paulo@0 40 "cyan" : ("36", "46"),
paulo@0 41 "white" : ("37", "47"),
paulo@0 42 "default" : ("39", "49"),
paulo@0 43 }
paulo@0 44
paulo@0 45 bold = False
paulo@0 46 fg = None
paulo@0 47 bg = None
paulo@11 48 colorLine = False
paulo@0 49
paulo@11 50 (optvals, args) = getopt.getopt(sys.argv[1:], "f:b:Bl")
paulo@0 51
paulo@0 52 for (opt, val) in optvals:
paulo@0 53 if opt == '-B':
paulo@0 54 bold = True
paulo@0 55 elif opt == '-f':
paulo@0 56 fg = val
paulo@0 57 elif opt == '-b':
paulo@0 58 bg = val
paulo@11 59 elif opt == '-l':
paulo@11 60 colorLine = True
paulo@0 61
paulo@0 62 if len(args) > 0:
paulo@0 63 for line in sys.stdin:
paulo@0 64 repl = START
paulo@0 65 if bold:
paulo@0 66 repl += BOLD
paulo@0 67 if fg is not None:
paulo@0 68 repl += c[fg][0]
paulo@0 69 if bg is not None:
paulo@0 70 repl += ';' + c[bg][1]
paulo@0 71 repl += END + r'\1' + CLEAR
paulo@11 72
paulo@11 73 x = line
paulo@11 74
paulo@11 75 if colorLine == False:
paulo@11 76 x = re.sub(r'(' + args[0] + r')', repl, line)
paulo@11 77 else:
paulo@11 78 if re.search(args[0], line) != None:
paulo@11 79 last_char = line[-1]
paulo@11 80 x = repl.replace(r'\1', line[:-1])
paulo@11 81 x += last_char
paulo@0 82
paulo@0 83 sys.stdout.write(x)
paulo@0 84 sys.stdout.flush()