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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.