Color-Coded `svn status` (v3)


/ Published in: Python
Save to your folder(s)

First there was: [http://snipplr.com/view/15246/color-coded-svn-status](http://snipplr.com/view/15246/color-coded-svn-status)

Then there was: [http://snipplr.com/view/16540/color-coded-svn-status-v2](http://snipplr.com/view/16540/color-coded-svn-status-v2)

A few days ago, I found a handy script online that colorized the output of SVN status. It worked pretty well, but needed a little polish and a couple of tweaks to make it use more common Python idioms. As I continued to use it and fix bugs and inefficiencies, I ended up replacing nearly every line in the original, but it was still a great starting point.

Additional changes include ANSI word-wrapping, a configurable tab expansion feature (for better code alignment), the 'colorizedSubcommands' sequence so that only applicable commands get colorized, use of proper `subprocess` module calls so that piping through `less` will work (for example, try `svn-color diff | less -r` to see colorized diff output).

To use, stick it somewhere, make executable (`chmod 755`), and then add this to your .profile:

alias svn=/usr/local/bin/svn-color.py

I hope you find my modifications useful. You can modify the colors used by looking up the ANSI color codes for your preferred color scheme and editing the 'statusColors' dictionary. Here's a useful reference for ANSI color values:

[http://www.ibm.com/developerworks/linux/library/l-tip-prompt/colortable.gif](http://www.ibm.com/developerworks/linux/library/l-tip-prompt/colortable.gif)

Requires Python 2.4 or greater.


Copy this code and paste it in your HTML
  1. #!/usr/bin/env python
  2.  
  3. """
  4. Author: Saophalkun Ponlu (http://phalkunz.com)
  5. Date: May 23, 2009
  6. Modified: June 15, 2009
  7.  
  8. Additional modifications:
  9. Author: Phil Christensen (http://bubblehouse.org)
  10. Date: February 22, 2010
  11. """
  12.  
  13. import os, sys, re, subprocess
  14.  
  15. tabsize = 4
  16.  
  17. colorizedSubcommands = (
  18. 'status',
  19. 'add',
  20. 'remove',
  21. 'diff',
  22. )
  23.  
  24. statusColors = {
  25. "M" : "31", # red
  26. "\?" : "37", # grey
  27. "A" : "32", # green
  28. "X" : "33", # yellow
  29. "C" : "30;41", # black on red
  30. "-" : "31", # red
  31. "D" : "31;1", # bold red
  32. "\+" : "32", # green
  33. }
  34.  
  35. def colorize(line):
  36. for color in statusColors:
  37. if re.match(color, line):
  38. return ''.join(("\001\033[", statusColors[color], "m", line, "\033[m\002"))
  39. else:
  40. return line
  41.  
  42. def escape(s):
  43. s = s.replace('$', r'\$')
  44. s = s.replace('"', r'\"')
  45. s = s.replace('`', r'\`')
  46. return s
  47.  
  48. passthru = lambda x: x
  49. quoted = lambda x: '"%s"' % escape(x)
  50.  
  51. if __name__ == "__main__":
  52. cmd = ' '.join(['svn']+[(passthru, quoted)[' ' in arg](arg) for arg in sys.argv[1:]])
  53. output = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
  54. cancelled = False
  55. for line in output.stdout:
  56. line = line.expandtabs(tabsize)
  57. if(sys.argv[1] in colorizedSubcommands):
  58. line = colorize(line)
  59. try:
  60. sys.stdout.write(line)
  61. except:
  62. sys.exit(1)

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.