jsvn: reset on every new color; add colordiff() utility function

This commit is contained in:
Josh Holtrop 2012-02-20 14:30:29 -05:00
parent 69f8e9944a
commit edc09ec740

31
jsvn
View File

@ -76,16 +76,39 @@ COLORS = {
# Utility Functions # # Utility Functions #
########################################################################### ###########################################################################
def ansi_color(out, fg=None, bg=None, bold=False): def ansi_color(out, fg=None, bg=None, bold=False):
if bold is not None: bc = 1 if bold else 0
out.write('\033[1m')
if fg is not None: if fg is not None:
out.write('\033[%dm' % (30 + COLORS[fg])) out.write('\033[%d;%dm' % (bc, 30 + COLORS[fg]))
if bg is not None: if bg is not None:
out.write('\033[%dm' % (40 + COLORS[bg])) out.write('\033[%d;%dm' % (bc, 40 + COLORS[bg]))
def ansi_reset(out): def ansi_reset(out):
out.write('\033[0m') out.write('\033[0m')
def colordiff(out, line):
if re.search(r'^-', line):
ansi_color(out, 'red')
out.write(line)
out.write('\n')
ansi_reset(out)
return
elif re.search(r'^\+', line):
ansi_color(out, 'green')
out.write(line)
out.write('\n')
ansi_reset(out)
return
m = re.match(r'(@@.*@@)(.*)', line)
if m is not None:
ansi_color(out, 'cyan')
out.write(m.group(1))
out.write(m.group(2))
out.write('\n')
ansi_reset(out)
return
out.write(line)
out.write('\n')
def findInPath(cmd): def findInPath(cmd):
path_entries = os.environ['PATH'].split(os.pathsep) path_entries = os.environ['PATH'].split(os.pathsep)
for p in path_entries: for p in path_entries: