Skip to content

Instantly share code, notes, and snippets.

@rharmes
Forked from pdokas/svn-color.py
Created October 11, 2011 19:45
Show Gist options
  • Save rharmes/1279186 to your computer and use it in GitHub Desktop.
Save rharmes/1279186 to your computer and use it in GitHub Desktop.
Colorize SVN
#
# 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
Author: Phil Dokas
Contact: [email protected]
Date: October 7, 2011
Author: Ross Harmes
Contact: [email protected]
Date: October 11, 2011
"""
import os, sys, re, subprocess
tabsize = 4
colorizedSubcommands = (
'status', 'stat', 'st',
'add',
'remove', 'delete', 'del', 'rm',
'diff', 'di',
'up',
'commit', 'co',
)
colorOrder = [
"Index:",
"Updated",
"At",
"Committed",
"Sending",
"Transmitting",
"@@",
"========",
"M",
"\?",
"A",
"X",
"G",
"C",
"U",
"-",
"D",
"\+",
]
#
# Colors Text Background
#
# black: 30 40
# red: 31 41
# green: 32 42
# yellow: 33 43
# blue: 34 44
# magenta: 35 45
# cyan: 36 46
# grey: 37 47
statusColors = {
"Index:" : "34", # blue
"Updated" : "37", # grey
"At" : "37", # grey
"Committed" : "37", # grey
"Sending" : "30;42", # black on green
"Transmitting" : "37", # grey
"@@" : "37", # grey
"========" : "37", # grey
"M" : "35", # magenta
"\?" : "37", # grey
"A" : "32", # green
"X" : "33", # yellow
"G" : "30;42", # black on green
"C" : "30;41", # black on red
"U" : "36", # cyan
"-" : "31", # red
"D" : "31", # red
"\+" : "32", # green
}
def colorize(line):
for i, color in enumerate(colorOrder):
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