Posted By

philchristensen on 02/22/10


Tagged

svn Bash python color


Versions (?)

Color-Coded `svn status` (v3)


 / Published in: Python
 

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

Then there was: 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

Requires Python 2.4 or greater.

  1. #!/usr/bin/env python
  2.  
  3. """
  4. Author: Saophalkun Ponlu (http://phalkunz.com)
  5. Contact: [email protected]
  6. Date: May 23, 2009
  7. Modified: June 15, 2009
  8.  
  9. Additional modifications:
  10. Author: Phil Christensen (http://bubblehouse.org)
  11. Contact: [email protected]
  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
Posted By: joelittlejohn on December 22, 2010

I'm getting some strange characters showing in my terminal when running svn status. Is this something to do with using the wrong white-space character?

Example:

D file1 A + file2 M file3

They appear to be 0001 0002 (whatever that is)

Posted By: joelittlejohn on December 22, 2010

When I turn this line:

return ''.join(("\001\033[", statusColors[color], "m", line, "\033[m\002"))

into this:

return ''.join(("\033[", statusColors[color], "m", line, "\033[m"))

The script works fine :)

Linux 4GBL08518 2.6.32-26-generic #48-Ubuntu SMP Wed Nov 24 09:00:03 UTC 2010 i686 GNU/Linux (Ubuntu 10.04.1 LTS)

GNU bash, version 4.1.5(1)-release (i486-pc-linux-gnu)

You need to login to post a comment.