annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
1 #!/usr/bin/env python
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
2
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
3 RESET_FORMAT = '\033[0m'
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
4 COLOR_FORMAT = '\033[38;5;%um'
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
5 BEGIN_REVERSE_FORMAT = '\033[7m'
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
6 END_REVERSE_FORMAT = '\033[27m'
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
7
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
8 INDEX_COLOR = 32
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
9 REMOVED_COLOR = 203
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
10 ADDED_COLOR = 2
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
11
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
12 prefixes_to_invert = ['---', '+++', '-', '+']
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
13
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
14 import sys
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
15 import fileinput
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
16
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
17 for line in fileinput.input():
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
18 if line.startswith('Index: ') or line.startswith('diff --git '):
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
19 color = INDEX_COLOR
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
20 elif line.startswith('-'):
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
21 color = REMOVED_COLOR
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
22 elif line.startswith('+'):
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
23 color = ADDED_COLOR
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
24 else:
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
25 color = None
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
26
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
27 if color is not None:
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
28 sys.stdout.write(COLOR_FORMAT % (color,))
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
29 for prefix in prefixes_to_invert:
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
30 if line.startswith(prefix):
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
31 sys.stdout.write(BEGIN_REVERSE_FORMAT)
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
32 sys.stdout.write(prefix)
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
33 sys.stdout.write(END_REVERSE_FORMAT)
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
34 line = line[len(prefix):]
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
35 break
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
36 else:
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
37 sys.stdout.write(RESET_FORMAT)
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
38
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
39 sys.stdout.write(line)
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
40
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
41 print RESET_FORMAT