Created
January 12, 2011 12:24
-
-
Save shawnchin/776093 to your computer and use it in GitHub Desktop.
(P.O.C) script to colorise ls output based on svn status
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
#!/usr/bin/env python | |
""" | |
Author: Shawn Chin (http://shawnchin.github.com) | |
Description: colorise output of `ls` based on svn status | |
Note: | |
This is a proof-of-concept implementation and only performs | |
a basic `ls` call in the current working directory. | |
""" | |
import re | |
from subprocess import Popen, PIPE | |
colormap = { | |
"M" : "31", # red | |
"?" : "37;41", # grey | |
"A" : "32", # green | |
"X" : "33", # yellow | |
"C" : "30;41", # black on red | |
"-" : "31", # red | |
"D" : "31;1", # bold red | |
"+" : "32", # green | |
} | |
re_svnout = re.compile(r'(.)\s+(.+)$') | |
file_status = {} | |
def colorise(line, key): | |
if key in colormap.keys(): | |
return "\001\033[%sm%s\033[m\002" % (colormap[key], line) | |
else: | |
return line | |
def get_svn_status(): | |
cmd = "svn status" | |
output = Popen(cmd, shell=True, stdout=PIPE) | |
for line in output.stdout: | |
match = re_svnout.match(line) | |
if match: | |
status, f = match.group(1), match.group(2) | |
# if sub directory has changes, mark it as modified | |
if "/" in f: | |
f = f.split("/")[0] | |
status = "M" | |
file_status[f] = status | |
if __name__ == "__main__": | |
get_svn_status() | |
for L in Popen("ls", shell=True, stdout=PIPE).stdout: | |
line = L.strip() | |
status = file_status.get(line, False) | |
print colorise(line, status) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment