Skip to content

Instantly share code, notes, and snippets.

@pdokas
Forked from rharmes/svn-color.py
Created March 28, 2012 18:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pdokas/2229236 to your computer and use it in GitHub Desktop.
Save pdokas/2229236 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: phalkunz@gmail.com
Date: May 23, 2009
Modified: June 15, 2009
Additional modifications:
Author: Phil Christensen (http://bubblehouse.org)
Contact: phil@bubblehouse.org
Date: February 22, 2010
Author: Phil Dokas
Contact: phil@jetless.org
Date: April 16, 2012
Author: Ross Harmes
Contact: ross@rossharmes.net
Date: October 11, 2011
"""
import os, sys, re, subprocess
tabsize = 4
colorizedSubcommands = (
'status', 'stat', 'st',
'add',
'remove', 'delete', 'del', 'rm',
'diff', 'di',
'up',
'commit', 'ci',
'merge',
)
colorOrder = [
"svn: Commit failed",
"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 = {
"svn: Commit failed" : "30;41", # black on red
"Index:" : "34", # blue
"Updated" : "37", # grey
"At" : "37", # grey
"Committed" : "36", # cyan
"Sending" : "32", # 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