Mercurial > diff-colorize
annotate diff-colorize.py @ 7:c6337f653d9b
Added some comments documenting the OrderedDict class and our instance's contents.
author | Peter Hosey |
---|---|
date | Mon, 11 Aug 2008 00:16:09 -0700 |
parents | d58b4e2e12d4 |
children | 4bb2557d24cd |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
2 | |
6
d58b4e2e12d4
Moved the imports of sys and fileinput, so that all the imports are together.
Peter Hosey
parents:
5
diff
changeset
|
3 import sys |
4
b8b2d1931a9e
Get our color constants from the environment, if possible.
Peter Hosey
parents:
3
diff
changeset
|
4 import os |
6
d58b4e2e12d4
Moved the imports of sys and fileinput, so that all the imports are together.
Peter Hosey
parents:
5
diff
changeset
|
5 import fileinput |
4
b8b2d1931a9e
Get our color constants from the environment, if possible.
Peter Hosey
parents:
3
diff
changeset
|
6 |
b8b2d1931a9e
Get our color constants from the environment, if possible.
Peter Hosey
parents:
3
diff
changeset
|
7 INDEX_COLOR = int(os.environ.get('DIFF_INDEX_COLOR', 32)) |
5
fa7cd4c2716b
Added prefixes “old mode” and “new mode”, found in Git-style diffs, along with new color constants for them. We treat these the same way we treat “---” and “+++”.
Peter Hosey
parents:
4
diff
changeset
|
8 OLD_MODE_COLOR = int(os.environ.get('DIFF_OLD_MODE_COLOR', 124)) |
fa7cd4c2716b
Added prefixes “old mode” and “new mode”, found in Git-style diffs, along with new color constants for them. We treat these the same way we treat “---” and “+++”.
Peter Hosey
parents:
4
diff
changeset
|
9 NEW_MODE_COLOR = int(os.environ.get('DIFF_NEW_MODE_COLOR', 28)) |
4
b8b2d1931a9e
Get our color constants from the environment, if possible.
Peter Hosey
parents:
3
diff
changeset
|
10 REMOVED_COLOR = int(os.environ.get('DIFF_REMOVED_COLOR', 203)) |
b8b2d1931a9e
Get our color constants from the environment, if possible.
Peter Hosey
parents:
3
diff
changeset
|
11 ADDED_COLOR = int(os.environ.get('DIFF_ADDED_COLOR', 2)) |
b8b2d1931a9e
Get our color constants from the environment, if possible.
Peter Hosey
parents:
3
diff
changeset
|
12 HUNK_START_COLOR = int(os.environ.get('DIFF_HUNK_START_COLOR', 32)) |
0 | 13 |
3
10948e4fd070
Move the color constants above the format strings, since the color constants are more editable.
Peter Hosey
parents:
2
diff
changeset
|
14 RESET_FORMAT = '\033[0m' |
10948e4fd070
Move the color constants above the format strings, since the color constants are more editable.
Peter Hosey
parents:
2
diff
changeset
|
15 COLOR_FORMAT = '\033[38;5;%um' |
10948e4fd070
Move the color constants above the format strings, since the color constants are more editable.
Peter Hosey
parents:
2
diff
changeset
|
16 BEGIN_REVERSE_FORMAT = '\033[7m' |
10948e4fd070
Move the color constants above the format strings, since the color constants are more editable.
Peter Hosey
parents:
2
diff
changeset
|
17 END_REVERSE_FORMAT = '\033[27m' |
10948e4fd070
Move the color constants above the format strings, since the color constants are more editable.
Peter Hosey
parents:
2
diff
changeset
|
18 |
7
c6337f653d9b
Added some comments documenting the OrderedDict class and our instance's contents.
Peter Hosey
parents:
6
diff
changeset
|
19 # Everything in the unified diff format is identified by a prefix. The prefixes are: |
c6337f653d9b
Added some comments documenting the OrderedDict class and our instance's contents.
Peter Hosey
parents:
6
diff
changeset
|
20 # 'Index: ': File marker (unified diff) |
c6337f653d9b
Added some comments documenting the OrderedDict class and our instance's contents.
Peter Hosey
parents:
6
diff
changeset
|
21 # 'diff --git': File marker (git-style diff) |
c6337f653d9b
Added some comments documenting the OrderedDict class and our instance's contents.
Peter Hosey
parents:
6
diff
changeset
|
22 # 'old mode': File permissions mode before change |
c6337f653d9b
Added some comments documenting the OrderedDict class and our instance's contents.
Peter Hosey
parents:
6
diff
changeset
|
23 # 'new mode': File permissions mode after change |
c6337f653d9b
Added some comments documenting the OrderedDict class and our instance's contents.
Peter Hosey
parents:
6
diff
changeset
|
24 # '---': Defining '-' (giving the name and modification date of the file before change) |
c6337f653d9b
Added some comments documenting the OrderedDict class and our instance's contents.
Peter Hosey
parents:
6
diff
changeset
|
25 # '+++': Defining '+' (giving the name and modification date of the file after change) |
c6337f653d9b
Added some comments documenting the OrderedDict class and our instance's contents.
Peter Hosey
parents:
6
diff
changeset
|
26 # '-': Line before change (i.e., removed) |
c6337f653d9b
Added some comments documenting the OrderedDict class and our instance's contents.
Peter Hosey
parents:
6
diff
changeset
|
27 # '+': Line after change (i.e., added) |
c6337f653d9b
Added some comments documenting the OrderedDict class and our instance's contents.
Peter Hosey
parents:
6
diff
changeset
|
28 # ' ': Line that hasn't changed |
c6337f653d9b
Added some comments documenting the OrderedDict class and our instance's contents.
Peter Hosey
parents:
6
diff
changeset
|
29 # '@@': Hunk start (@@ -start,length +start, length @@) |
c6337f653d9b
Added some comments documenting the OrderedDict class and our instance's contents.
Peter Hosey
parents:
6
diff
changeset
|
30 # |
c6337f653d9b
Added some comments documenting the OrderedDict class and our instance's contents.
Peter Hosey
parents:
6
diff
changeset
|
31 # We need to look for these prefixes in order, in order to handle '---'/'+++' before '-'/'+'. Hence the OrderedDict. |
1
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
32 class OrderedDict(dict): |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
33 def __init__(self, input=None): |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
34 if input is None: |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
35 self.keys = [] |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
36 super(OrderedDict, self).__init__() |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
37 elif isinstance(input, dict): |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
38 self.keys = list(input) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
39 super(OrderedDict, self).__init__(input) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
40 else: |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
41 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
|
42 super(OrderedDict, self).__init__(input) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
43 def __iter__(self): |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
44 return iter(self.keys) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
45 def __setitem__(self, k, v): |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
46 if k not in self: |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
47 self.keys.append(k) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
48 super(OrderedDict, self).__setitem__(k, v) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
49 def __delitem__(self, k): |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
50 super(OrderedDict, self).__delitem__(k) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
51 self.keys.remove(k) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
52 |
7
c6337f653d9b
Added some comments documenting the OrderedDict class and our instance's contents.
Peter Hosey
parents:
6
diff
changeset
|
53 # Each value includes not only the terminal-config characters, but also the key, somewhere within it (possibly between two terminal-config strings). |
c6337f653d9b
Added some comments documenting the OrderedDict class and our instance's contents.
Peter Hosey
parents:
6
diff
changeset
|
54 # Theoretically, you could replace the key with some other string or leave it out entirely, if you wanted to, but I wouldn't recommend it. |
1
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
55 prefixes = OrderedDict() |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
56 prefixes['---'] = ( |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
57 COLOR_FORMAT % (REMOVED_COLOR,) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
58 + BEGIN_REVERSE_FORMAT |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
59 + '---' |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
60 + END_REVERSE_FORMAT |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
61 ) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
62 prefixes['+++'] = ( |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
63 COLOR_FORMAT % (ADDED_COLOR,) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
64 + BEGIN_REVERSE_FORMAT |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
65 + '+++' |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
66 + END_REVERSE_FORMAT |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
67 ) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
68 prefixes['-'] = ( |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
69 COLOR_FORMAT % (REMOVED_COLOR,) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
70 + BEGIN_REVERSE_FORMAT |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
71 + '-' |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
72 + END_REVERSE_FORMAT |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
73 ) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
74 prefixes['+'] = ( |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
75 COLOR_FORMAT % (ADDED_COLOR,) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
76 + BEGIN_REVERSE_FORMAT |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
77 + '+' |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
78 + END_REVERSE_FORMAT |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
79 ) |
5
fa7cd4c2716b
Added prefixes “old mode” and “new mode”, found in Git-style diffs, along with new color constants for them. We treat these the same way we treat “---” and “+++”.
Peter Hosey
parents:
4
diff
changeset
|
80 prefixes['old mode'] = ( # Git-style diffs only |
fa7cd4c2716b
Added prefixes “old mode” and “new mode”, found in Git-style diffs, along with new color constants for them. We treat these the same way we treat “---” and “+++”.
Peter Hosey
parents:
4
diff
changeset
|
81 COLOR_FORMAT % (OLD_MODE_COLOR,) |
fa7cd4c2716b
Added prefixes “old mode” and “new mode”, found in Git-style diffs, along with new color constants for them. We treat these the same way we treat “---” and “+++”.
Peter Hosey
parents:
4
diff
changeset
|
82 + BEGIN_REVERSE_FORMAT |
fa7cd4c2716b
Added prefixes “old mode” and “new mode”, found in Git-style diffs, along with new color constants for them. We treat these the same way we treat “---” and “+++”.
Peter Hosey
parents:
4
diff
changeset
|
83 + 'old mode' |
fa7cd4c2716b
Added prefixes “old mode” and “new mode”, found in Git-style diffs, along with new color constants for them. We treat these the same way we treat “---” and “+++”.
Peter Hosey
parents:
4
diff
changeset
|
84 + END_REVERSE_FORMAT |
fa7cd4c2716b
Added prefixes “old mode” and “new mode”, found in Git-style diffs, along with new color constants for them. We treat these the same way we treat “---” and “+++”.
Peter Hosey
parents:
4
diff
changeset
|
85 ) |
fa7cd4c2716b
Added prefixes “old mode” and “new mode”, found in Git-style diffs, along with new color constants for them. We treat these the same way we treat “---” and “+++”.
Peter Hosey
parents:
4
diff
changeset
|
86 prefixes['new mode'] = ( # Git-style diffs only |
fa7cd4c2716b
Added prefixes “old mode” and “new mode”, found in Git-style diffs, along with new color constants for them. We treat these the same way we treat “---” and “+++”.
Peter Hosey
parents:
4
diff
changeset
|
87 COLOR_FORMAT % (NEW_MODE_COLOR,) |
fa7cd4c2716b
Added prefixes “old mode” and “new mode”, found in Git-style diffs, along with new color constants for them. We treat these the same way we treat “---” and “+++”.
Peter Hosey
parents:
4
diff
changeset
|
88 + BEGIN_REVERSE_FORMAT |
fa7cd4c2716b
Added prefixes “old mode” and “new mode”, found in Git-style diffs, along with new color constants for them. We treat these the same way we treat “---” and “+++”.
Peter Hosey
parents:
4
diff
changeset
|
89 + 'new mode' |
fa7cd4c2716b
Added prefixes “old mode” and “new mode”, found in Git-style diffs, along with new color constants for them. We treat these the same way we treat “---” and “+++”.
Peter Hosey
parents:
4
diff
changeset
|
90 + END_REVERSE_FORMAT |
fa7cd4c2716b
Added prefixes “old mode” and “new mode”, found in Git-style diffs, along with new color constants for them. We treat these the same way we treat “---” and “+++”.
Peter Hosey
parents:
4
diff
changeset
|
91 ) |
1
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
92 prefixes['Index: '] = COLOR_FORMAT % (INDEX_COLOR,) + 'Index: ' |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
93 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
|
94 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
|
95 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
|
96 + 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
|
97 + '@@' |
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
|
98 ) |
0 | 99 |
100 for line in fileinput.input(): | |
1
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
101 for prefix_to_test in prefixes: |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
102 if line.startswith(prefix_to_test): |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
103 sys.stdout.write(prefixes[prefix_to_test]) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
104 line = line[len(prefix_to_test):] |
0 | 105 |
106 sys.stdout.write(line) | |
107 | |
1
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
108 sys.stdout.write(RESET_FORMAT) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
109 |
0 | 110 print RESET_FORMAT |