comparison diff-colorize.py @ 0:2df4ed64a388

Initial check-in of first working version.
author Peter Hosey
date Sun, 10 Aug 2008 22:57:59 -0700
parents
children 44f86539d245
comparison
equal deleted inserted replaced
-1:000000000000 0:2df4ed64a388
1 #!/usr/bin/env python
2
3 RESET_FORMAT = '\033[0m'
4 COLOR_FORMAT = '\033[38;5;%um'
5 BEGIN_REVERSE_FORMAT = '\033[7m'
6 END_REVERSE_FORMAT = '\033[27m'
7
8 INDEX_COLOR = 32
9 REMOVED_COLOR = 203
10 ADDED_COLOR = 2
11
12 prefixes_to_invert = ['---', '+++', '-', '+']
13
14 import sys
15 import fileinput
16
17 for line in fileinput.input():
18 if line.startswith('Index: ') or line.startswith('diff --git '):
19 color = INDEX_COLOR
20 elif line.startswith('-'):
21 color = REMOVED_COLOR
22 elif line.startswith('+'):
23 color = ADDED_COLOR
24 else:
25 color = None
26
27 if color is not None:
28 sys.stdout.write(COLOR_FORMAT % (color,))
29 for prefix in prefixes_to_invert:
30 if line.startswith(prefix):
31 sys.stdout.write(BEGIN_REVERSE_FORMAT)
32 sys.stdout.write(prefix)
33 sys.stdout.write(END_REVERSE_FORMAT)
34 line = line[len(prefix):]
35 break
36 else:
37 sys.stdout.write(RESET_FORMAT)
38
39 sys.stdout.write(line)
40
41 print RESET_FORMAT