Created
October 6, 2011 20:03
-
-
Save pdokas/1268492 to your computer and use it in GitHub Desktop.
Colorize SVN
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 1) Alias this: alias svn=/Users/foo/dev/svn-color.py | |
# 2) Paste the script below into the above file | |
# 3) chmod 777 <the file> | |
# 4) Delete this quote block so the env line is line 1 | |
# | |
#!/usr/bin/env python | |
""" | |
Author: Saophalkun Ponlu (http://phalkunz.com) | |
Contact: [email protected] | |
Date: May 23, 2009 | |
Modified: June 15, 2009 | |
Additional modifications: | |
Author: Phil Christensen (http://bubblehouse.org) | |
Contact: [email protected] | |
Date: February 22, 2010 | |
""" | |
import os, sys, re, subprocess | |
tabsize = 4 | |
colorizedSubcommands = ( | |
'status', 'stat', 'st', | |
'add', | |
'remove', 'delete', 'del', 'rm', | |
'diff', 'di', | |
'update', 'up', | |
) | |
statusColors = { | |
"M" : "31", # red | |
"\?" : "37", # grey | |
"A" : "32", # green | |
"G" : "32;41", # green on green | |
"X" : "33", # yellow | |
"U" : "36", # cyan | |
"C" : "30;41", # black on red | |
"-" : "31", # red | |
"D" : "31;1", # bold red | |
"\+" : "32", # green | |
} | |
def colorize(line): | |
for color in statusColors: | |
if re.match(color, line): | |
return ''.join(("\001\033[", statusColors[color], "m", line, "\033[m\002")) | |
else: | |
return line | |
def escape(s): | |
s = s.replace('$', r'\$') | |
s = s.replace('"', r'\"') | |
s = s.replace('`', r'\`') | |
return s | |
passthru = lambda x: x | |
quoted = lambda x: '"%s"' % escape(x) | |
if __name__ == "__main__": | |
cmd = ' '.join(['svn']+[(passthru, quoted)[' ' in arg](arg) for arg in sys.argv[1:]]) | |
output = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) | |
cancelled = False | |
for line in output.stdout: | |
line = line.expandtabs(tabsize) | |
if(sys.argv[1] in colorizedSubcommands): | |
line = colorize(line) | |
try: | |
sys.stdout.write(line) | |
except: | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment