annotate diff-colorize.py @ 2:9eda9139d627

Added support for hunk start markers (@@…@@). For these, we set the whole line in reverse video, in the same color we use for index lines.
author Peter Hosey
date Sun, 10 Aug 2008 23:29:08 -0700
parents 44f86539d245
children 10948e4fd070
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
2
9eda9139d627 Added support for hunk start markers (@@…@@). For these, we set the whole line in reverse video, in the same color we use for index lines.
Peter Hosey
parents: 1
diff changeset
11 HUNK_START_COLOR = 32
0
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
12
1
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
13 class OrderedDict(dict):
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
14 def __init__(self, input=None):
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
15 if input is None:
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
16 self.keys = []
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
17 super(OrderedDict, self).__init__()
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
18 elif isinstance(input, dict):
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
19 self.keys = list(input)
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
20 super(OrderedDict, self).__init__(input)
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
21 else:
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
22 self.keys = [k for k, v in input]
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
23 super(OrderedDict, self).__init__(input)
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
24 def __iter__(self):
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
25 return iter(self.keys)
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
26 def __setitem__(self, k, v):
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
27 if k not in self:
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
28 self.keys.append(k)
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
29 super(OrderedDict, self).__setitem__(k, v)
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
30 def __delitem__(self, k):
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
31 super(OrderedDict, self).__delitem__(k)
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
32 self.keys.remove(k)
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
33
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
34 prefixes = OrderedDict()
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
35 prefixes['---'] = (
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
36 COLOR_FORMAT % (REMOVED_COLOR,)
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
37 + BEGIN_REVERSE_FORMAT
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
38 + '---'
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
39 + END_REVERSE_FORMAT
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
40 )
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
41 prefixes['+++'] = (
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
42 COLOR_FORMAT % (ADDED_COLOR,)
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
43 + BEGIN_REVERSE_FORMAT
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
44 + '+++'
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
45 + END_REVERSE_FORMAT
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
46 )
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
47 prefixes['-'] = (
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
48 COLOR_FORMAT % (REMOVED_COLOR,)
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
49 + BEGIN_REVERSE_FORMAT
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
50 + '-'
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
51 + END_REVERSE_FORMAT
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
52 )
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
53 prefixes['+'] = (
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
54 COLOR_FORMAT % (ADDED_COLOR,)
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
55 + BEGIN_REVERSE_FORMAT
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
56 + '+'
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
57 + END_REVERSE_FORMAT
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
58 )
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
59 prefixes['Index: '] = COLOR_FORMAT % (INDEX_COLOR,) + 'Index: '
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
60 prefixes['diff --git '] = COLOR_FORMAT % (INDEX_COLOR,) + 'diff --git '
2
9eda9139d627 Added support for hunk start markers (@@…@@). For these, we set the whole line in reverse video, in the same color we use for index lines.
Peter Hosey
parents: 1
diff changeset
61 prefixes['@@'] = (
9eda9139d627 Added support for hunk start markers (@@…@@). For these, we set the whole line in reverse video, in the same color we use for index lines.
Peter Hosey
parents: 1
diff changeset
62 COLOR_FORMAT % (HUNK_START_COLOR,)
9eda9139d627 Added support for hunk start markers (@@…@@). For these, we set the whole line in reverse video, in the same color we use for index lines.
Peter Hosey
parents: 1
diff changeset
63 + BEGIN_REVERSE_FORMAT
9eda9139d627 Added support for hunk start markers (@@…@@). For these, we set the whole line in reverse video, in the same color we use for index lines.
Peter Hosey
parents: 1
diff changeset
64 + '@@'
9eda9139d627 Added support for hunk start markers (@@…@@). For these, we set the whole line in reverse video, in the same color we use for index lines.
Peter Hosey
parents: 1
diff changeset
65 )
0
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
66
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
67 import sys
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
68 import fileinput
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
69
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
70 for line in fileinput.input():
1
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
71 for prefix_to_test in prefixes:
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
72 if line.startswith(prefix_to_test):
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
73 sys.stdout.write(prefixes[prefix_to_test])
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
74 line = line[len(prefix_to_test):]
0
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
75
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
76 sys.stdout.write(line)
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
77
1
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
78 sys.stdout.write(RESET_FORMAT)
44f86539d245 Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents: 0
diff changeset
79
0
2df4ed64a388 Initial check-in of first working version.
Peter Hosey
parents:
diff changeset
80 print RESET_FORMAT