Mercurial > diff-colorize
annotate diff-colorize.py @ 28:58221373fc6f draft default tip
python3: port to Python 3
Pleasantly few changes required.
author | Augie Fackler <raf@durin42.com> |
---|---|
date | Thu, 14 Jul 2022 10:20:03 -0400 |
parents | 5f17911c4fe6 |
children |
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 |
28 | 6 import functools |
4
b8b2d1931a9e
Get our color constants from the environment, if possible.
Peter Hosey
parents:
3
diff
changeset
|
7 |
15
a7214992f904
Added a different set of default color codes and a different colorization format string. Both of these are to support 16-color mode (which is all Terminal supports).
Peter Hosey
parents:
13
diff
changeset
|
8 has_256_color = (os.environ.get('TERM', None) == 'xterm-256color') |
a7214992f904
Added a different set of default color codes and a different colorization format string. Both of these are to support 16-color mode (which is all Terminal supports).
Peter Hosey
parents:
13
diff
changeset
|
9 |
a7214992f904
Added a different set of default color codes and a different colorization format string. Both of these are to support 16-color mode (which is all Terminal supports).
Peter Hosey
parents:
13
diff
changeset
|
10 index_color = int(os.environ.get('DIFF_INDEX_COLOR', |
a7214992f904
Added a different set of default color codes and a different colorization format string. Both of these are to support 16-color mode (which is all Terminal supports).
Peter Hosey
parents:
13
diff
changeset
|
11 32 if has_256_color else 36)) |
a7214992f904
Added a different set of default color codes and a different colorization format string. Both of these are to support 16-color mode (which is all Terminal supports).
Peter Hosey
parents:
13
diff
changeset
|
12 old_mode_color = int(os.environ.get('DIFF_OLD_MODE_COLOR', |
a7214992f904
Added a different set of default color codes and a different colorization format string. Both of these are to support 16-color mode (which is all Terminal supports).
Peter Hosey
parents:
13
diff
changeset
|
13 88 if has_256_color else 31)) |
a7214992f904
Added a different set of default color codes and a different colorization format string. Both of these are to support 16-color mode (which is all Terminal supports).
Peter Hosey
parents:
13
diff
changeset
|
14 new_mode_color = int(os.environ.get('DIFF_NEW_MODE_COLOR', |
a7214992f904
Added a different set of default color codes and a different colorization format string. Both of these are to support 16-color mode (which is all Terminal supports).
Peter Hosey
parents:
13
diff
changeset
|
15 28 if has_256_color else 32)) |
a7214992f904
Added a different set of default color codes and a different colorization format string. Both of these are to support 16-color mode (which is all Terminal supports).
Peter Hosey
parents:
13
diff
changeset
|
16 removed_color = int(os.environ.get('DIFF_REMOVED_COLOR', |
a7214992f904
Added a different set of default color codes and a different colorization format string. Both of these are to support 16-color mode (which is all Terminal supports).
Peter Hosey
parents:
13
diff
changeset
|
17 160 if has_256_color else 31)) |
a7214992f904
Added a different set of default color codes and a different colorization format string. Both of these are to support 16-color mode (which is all Terminal supports).
Peter Hosey
parents:
13
diff
changeset
|
18 added_color = int(os.environ.get('DIFF_ADDED_COLOR', |
a7214992f904
Added a different set of default color codes and a different colorization format string. Both of these are to support 16-color mode (which is all Terminal supports).
Peter Hosey
parents:
13
diff
changeset
|
19 2 if has_256_color else 32)) |
a7214992f904
Added a different set of default color codes and a different colorization format string. Both of these are to support 16-color mode (which is all Terminal supports).
Peter Hosey
parents:
13
diff
changeset
|
20 hunk_start_color = int(os.environ.get('DIFF_HUNK_START_COLOR', |
a7214992f904
Added a different set of default color codes and a different colorization format string. Both of these are to support 16-color mode (which is all Terminal supports).
Peter Hosey
parents:
13
diff
changeset
|
21 32 if has_256_color else 36)) |
0 | 22 |
3
10948e4fd070
Move the color constants above the format strings, since the color constants are more editable.
Peter Hosey
parents:
2
diff
changeset
|
23 RESET_FORMAT = '\033[0m' |
15
a7214992f904
Added a different set of default color codes and a different colorization format string. Both of these are to support 16-color mode (which is all Terminal supports).
Peter Hosey
parents:
13
diff
changeset
|
24 COLOR_FORMAT_256 = '\033[38;5;%um' |
a7214992f904
Added a different set of default color codes and a different colorization format string. Both of these are to support 16-color mode (which is all Terminal supports).
Peter Hosey
parents:
13
diff
changeset
|
25 COLOR_FORMAT_16 = '\033[38;%um' |
a7214992f904
Added a different set of default color codes and a different colorization format string. Both of these are to support 16-color mode (which is all Terminal supports).
Peter Hosey
parents:
13
diff
changeset
|
26 COLOR_FORMAT = COLOR_FORMAT_256 if has_256_color else COLOR_FORMAT_16 |
3
10948e4fd070
Move the color constants above the format strings, since the color constants are more editable.
Peter Hosey
parents:
2
diff
changeset
|
27 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
|
28 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
|
29 |
9 | 30 USAGE = """ |
25 | 31 diff-colorize 1.1 by Peter Hosey |
22
948c96784f00
Added line to usage message identifying this as diff-colorize 1.0.
Peter Hosey <hg@boredzo.org>
parents:
17
diff
changeset
|
32 |
9 | 33 Usage: diff ... | diff-colorize |
34 or: diff-colorize < foo.diff | |
35 | |
36 Reads unified or git-style diff data from standard input, colorizes it, and writes the result to standard output. | |
37 | |
38 You can customize the color numbers used by setting these variables in your environment: | |
39 * DIFF_INDEX_COLOR (lines starting with "Index: " or "diff --git ") | |
40 * DIFF_OLD_MODE_COLOR (lines starting with "old mode"; these only appear in git-style diffs) | |
41 * DIFF_NEW_MODE_COLOR (lines starting with "new mode"; these only appear in git-style diffs) | |
42 * DIFF_REMOVED_COLOR (lines starting with "-") | |
43 * DIFF_ADDED_COLOR (lines starting with "+") | |
44 * DIFF_HUNK_START_COLOR (lines starting with "@@") | |
45 """.strip() | |
46 | |
18
83d58ccc70bf
Interleave adjacent runs of consecutive old and new lines into alternating old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
17
diff
changeset
|
47 def interleave(*sequences): |
83d58ccc70bf
Interleave adjacent runs of consecutive old and new lines into alternating old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
17
diff
changeset
|
48 "Generator that yields one object from each sequence in turn." |
83d58ccc70bf
Interleave adjacent runs of consecutive old and new lines into alternating old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
17
diff
changeset
|
49 |
83d58ccc70bf
Interleave adjacent runs of consecutive old and new lines into alternating old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
17
diff
changeset
|
50 def zip_pad(*iterables, **kw): |
83d58ccc70bf
Interleave adjacent runs of consecutive old and new lines into alternating old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
17
diff
changeset
|
51 "Downloaded from http://code.activestate.com/recipes/497007/" |
28 | 52 from itertools import chain |
18
83d58ccc70bf
Interleave adjacent runs of consecutive old and new lines into alternating old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
17
diff
changeset
|
53 if kw: |
83d58ccc70bf
Interleave adjacent runs of consecutive old and new lines into alternating old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
17
diff
changeset
|
54 assert len(kw) == 1 |
83d58ccc70bf
Interleave adjacent runs of consecutive old and new lines into alternating old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
17
diff
changeset
|
55 pad = kw["pad"] |
83d58ccc70bf
Interleave adjacent runs of consecutive old and new lines into alternating old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
17
diff
changeset
|
56 else: |
83d58ccc70bf
Interleave adjacent runs of consecutive old and new lines into alternating old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
17
diff
changeset
|
57 pad = None |
83d58ccc70bf
Interleave adjacent runs of consecutive old and new lines into alternating old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
17
diff
changeset
|
58 done = [len(iterables)-1] |
83d58ccc70bf
Interleave adjacent runs of consecutive old and new lines into alternating old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
17
diff
changeset
|
59 def pad_iter(): |
83d58ccc70bf
Interleave adjacent runs of consecutive old and new lines into alternating old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
17
diff
changeset
|
60 if not done[0]: |
83d58ccc70bf
Interleave adjacent runs of consecutive old and new lines into alternating old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
17
diff
changeset
|
61 return |
83d58ccc70bf
Interleave adjacent runs of consecutive old and new lines into alternating old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
17
diff
changeset
|
62 done[0] -= 1 |
83d58ccc70bf
Interleave adjacent runs of consecutive old and new lines into alternating old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
17
diff
changeset
|
63 while 1: |
83d58ccc70bf
Interleave adjacent runs of consecutive old and new lines into alternating old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
17
diff
changeset
|
64 yield pad |
83d58ccc70bf
Interleave adjacent runs of consecutive old and new lines into alternating old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
17
diff
changeset
|
65 iterables = [chain(seq, pad_iter()) for seq in iterables] |
28 | 66 return zip(*iterables) |
18
83d58ccc70bf
Interleave adjacent runs of consecutive old and new lines into alternating old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
17
diff
changeset
|
67 |
83d58ccc70bf
Interleave adjacent runs of consecutive old and new lines into alternating old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
17
diff
changeset
|
68 for objects in zip_pad(*sequences): |
83d58ccc70bf
Interleave adjacent runs of consecutive old and new lines into alternating old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
17
diff
changeset
|
69 for obj in objects: |
83d58ccc70bf
Interleave adjacent runs of consecutive old and new lines into alternating old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
17
diff
changeset
|
70 if obj is not None: |
83d58ccc70bf
Interleave adjacent runs of consecutive old and new lines into alternating old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
17
diff
changeset
|
71 yield obj |
83d58ccc70bf
Interleave adjacent runs of consecutive old and new lines into alternating old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
17
diff
changeset
|
72 |
28 | 73 @functools.total_ordering |
19
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
74 class Substring(object): |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
75 def __init__(self, a, a_start, a_stop, b, b_start, b_stop): |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
76 self.a = a |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
77 self.a_start = a_start |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
78 self.a_stop = a_stop |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
79 self.b = b |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
80 self.b_start = b_start |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
81 self.b_stop = b_stop |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
82 |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
83 def before_a_substring(self): |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
84 return self.a[:self.a_start] |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
85 def before_b_substring(self): |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
86 return self.b[:self.b_start] |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
87 def substring(self): |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
88 return ''.join(self.a[self.a_start:self.a_stop]) |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
89 a_substring = substring |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
90 b_substring = substring |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
91 def after_a_substring(self): |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
92 return self.a[self.a_stop:] |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
93 def after_b_substring(self): |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
94 return self.b[self.b_stop:] |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
95 |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
96 def __hash__(self): |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
97 return hash(self.substring()) |
28 | 98 def __lt__(self, other): |
99 return self.a_start < other.a_start | |
19
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
100 def __eq__(self, other): |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
101 return self.substring() == other.substring() |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
102 def __str__(self): |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
103 return self.substring() |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
104 def __repr__(self): |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
105 return 'Substring(%r)' % (self.substring(),) |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
106 return 'Substring(%r from %r, %r, %r, %r, %r, %r)' % ( |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
107 self.substring(), |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
108 self.a, self.a_start, self.a_stop, |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
109 self.b, self.b_start, self.b_stop, |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
110 ) |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
111 |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
112 def longest_common_substring(a, b): |
20
b4caea436f4d
Improve the highlighting of differences using a longest common subsequence algorithm.
Peter Hosey <hg@boredzo.org>
parents:
19
diff
changeset
|
113 """Returns the longest common substring between a and b, which can be any finite indexable sliceable sequences, as a Substring object. Returns None if there is no substring between the sequences. |
19
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
114 |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
115 Clarified and slightly modified (to use a special Substring object) from http://en.wikibooks.org/w/index.php?title=Algorithm_implementation/Strings/Longest_common_substring&oldid=1419225#Python |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
116 """ |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
117 a_len = len(a) |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
118 b_len = len(b) |
28 | 119 lengths = [[0] * (b_len + 1) for i in range(a_len + 1)] |
19
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
120 substrings = set() |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
121 greatest_length = current_run_length = 0 |
28 | 122 for a_idx in range(a_len): |
123 for b_idx in range(b_len): | |
19
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
124 if a[a_idx] == b[b_idx]: |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
125 current_run_length = lengths[a_idx][b_idx] + 1 |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
126 lengths[a_idx+1][b_idx+1] = current_run_length |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
127 if current_run_length > greatest_length: |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
128 greatest_length = current_run_length |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
129 substrings.clear() |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
130 if current_run_length == greatest_length: |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
131 # substrings.add(a[a_idx - current_run_length + 1:a_idx + 1]) |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
132 substrings.add(Substring(a, a_idx - current_run_length + 1, a_idx + 1, b, b_idx - current_run_length + 1, b_idx + 1)) |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
133 else: |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
134 if current_run_length > 0: |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
135 substrings.add(Substring(a, a_idx - current_run_length + 1, a_idx + 1, b, b_idx - current_run_length + 1, b_idx + 1)) |
20
b4caea436f4d
Improve the highlighting of differences using a longest common subsequence algorithm.
Peter Hosey <hg@boredzo.org>
parents:
19
diff
changeset
|
136 try: |
b4caea436f4d
Improve the highlighting of differences using a longest common subsequence algorithm.
Peter Hosey <hg@boredzo.org>
parents:
19
diff
changeset
|
137 return substrings.pop() |
b4caea436f4d
Improve the highlighting of differences using a longest common subsequence algorithm.
Peter Hosey <hg@boredzo.org>
parents:
19
diff
changeset
|
138 except KeyError: |
b4caea436f4d
Improve the highlighting of differences using a longest common subsequence algorithm.
Peter Hosey <hg@boredzo.org>
parents:
19
diff
changeset
|
139 return None |
b4caea436f4d
Improve the highlighting of differences using a longest common subsequence algorithm.
Peter Hosey <hg@boredzo.org>
parents:
19
diff
changeset
|
140 |
b4caea436f4d
Improve the highlighting of differences using a longest common subsequence algorithm.
Peter Hosey <hg@boredzo.org>
parents:
19
diff
changeset
|
141 def common_subsequence(a, b): |
b4caea436f4d
Improve the highlighting of differences using a longest common subsequence algorithm.
Peter Hosey <hg@boredzo.org>
parents:
19
diff
changeset
|
142 "Returns all common substrings between a and b, which can be any finite indexable sliceable sequences, as Substring objects. Determines this by recursively calling itself on slices of a and b before and after each longest common substring." |
b4caea436f4d
Improve the highlighting of differences using a longest common subsequence algorithm.
Peter Hosey <hg@boredzo.org>
parents:
19
diff
changeset
|
143 # Inspired by http://en.wikibooks.org/w/index.php?title=Algorithm_Implementation/Strings/Longest_common_subsequence&oldid=1912924#Python |
b4caea436f4d
Improve the highlighting of differences using a longest common subsequence algorithm.
Peter Hosey <hg@boredzo.org>
parents:
19
diff
changeset
|
144 def LCS_length_matrix(a, b): |
28 | 145 matrix = [[0] * (len(b) + 1) for i in range(len(a) + 1)] |
20
b4caea436f4d
Improve the highlighting of differences using a longest common subsequence algorithm.
Peter Hosey <hg@boredzo.org>
parents:
19
diff
changeset
|
146 for i, a_ch in enumerate(a): |
b4caea436f4d
Improve the highlighting of differences using a longest common subsequence algorithm.
Peter Hosey <hg@boredzo.org>
parents:
19
diff
changeset
|
147 for j, b_ch in enumerate(b): |
b4caea436f4d
Improve the highlighting of differences using a longest common subsequence algorithm.
Peter Hosey <hg@boredzo.org>
parents:
19
diff
changeset
|
148 if a_ch == b_ch: |
b4caea436f4d
Improve the highlighting of differences using a longest common subsequence algorithm.
Peter Hosey <hg@boredzo.org>
parents:
19
diff
changeset
|
149 matrix[i + 1][j + 1] = matrix[i][j] + 1 |
b4caea436f4d
Improve the highlighting of differences using a longest common subsequence algorithm.
Peter Hosey <hg@boredzo.org>
parents:
19
diff
changeset
|
150 else: |
b4caea436f4d
Improve the highlighting of differences using a longest common subsequence algorithm.
Peter Hosey <hg@boredzo.org>
parents:
19
diff
changeset
|
151 matrix[i + 1][j + 1] = max(matrix[i + 1][j], matrix[i][j + 1]) |
b4caea436f4d
Improve the highlighting of differences using a longest common subsequence algorithm.
Peter Hosey <hg@boredzo.org>
parents:
19
diff
changeset
|
152 return matrix |
b4caea436f4d
Improve the highlighting of differences using a longest common subsequence algorithm.
Peter Hosey <hg@boredzo.org>
parents:
19
diff
changeset
|
153 |
b4caea436f4d
Improve the highlighting of differences using a longest common subsequence algorithm.
Peter Hosey <hg@boredzo.org>
parents:
19
diff
changeset
|
154 def recursive_build_subsequence(a, b, matrix=None, i=None, j=None): |
b4caea436f4d
Improve the highlighting of differences using a longest common subsequence algorithm.
Peter Hosey <hg@boredzo.org>
parents:
19
diff
changeset
|
155 if matrix is None: |
b4caea436f4d
Improve the highlighting of differences using a longest common subsequence algorithm.
Peter Hosey <hg@boredzo.org>
parents:
19
diff
changeset
|
156 matrix = LCS_length_matrix(a, b) |
b4caea436f4d
Improve the highlighting of differences using a longest common subsequence algorithm.
Peter Hosey <hg@boredzo.org>
parents:
19
diff
changeset
|
157 if i is None: |
b4caea436f4d
Improve the highlighting of differences using a longest common subsequence algorithm.
Peter Hosey <hg@boredzo.org>
parents:
19
diff
changeset
|
158 i = len(a) |
b4caea436f4d
Improve the highlighting of differences using a longest common subsequence algorithm.
Peter Hosey <hg@boredzo.org>
parents:
19
diff
changeset
|
159 if j is None: |
b4caea436f4d
Improve the highlighting of differences using a longest common subsequence algorithm.
Peter Hosey <hg@boredzo.org>
parents:
19
diff
changeset
|
160 j = len(b) |
b4caea436f4d
Improve the highlighting of differences using a longest common subsequence algorithm.
Peter Hosey <hg@boredzo.org>
parents:
19
diff
changeset
|
161 |
b4caea436f4d
Improve the highlighting of differences using a longest common subsequence algorithm.
Peter Hosey <hg@boredzo.org>
parents:
19
diff
changeset
|
162 if i == 0 or j == 0: |
b4caea436f4d
Improve the highlighting of differences using a longest common subsequence algorithm.
Peter Hosey <hg@boredzo.org>
parents:
19
diff
changeset
|
163 return [] |
b4caea436f4d
Improve the highlighting of differences using a longest common subsequence algorithm.
Peter Hosey <hg@boredzo.org>
parents:
19
diff
changeset
|
164 elif a[i - 1] == b[j - 1]: |
b4caea436f4d
Improve the highlighting of differences using a longest common subsequence algorithm.
Peter Hosey <hg@boredzo.org>
parents:
19
diff
changeset
|
165 return recursive_build_subsequence(a, b, matrix, i - 1, j - 1) + [Substring(a, i - 1, i, b, j - 1, j)] |
b4caea436f4d
Improve the highlighting of differences using a longest common subsequence algorithm.
Peter Hosey <hg@boredzo.org>
parents:
19
diff
changeset
|
166 else: |
b4caea436f4d
Improve the highlighting of differences using a longest common subsequence algorithm.
Peter Hosey <hg@boredzo.org>
parents:
19
diff
changeset
|
167 if matrix[i][j - 1] > matrix[i - 1][j]: |
b4caea436f4d
Improve the highlighting of differences using a longest common subsequence algorithm.
Peter Hosey <hg@boredzo.org>
parents:
19
diff
changeset
|
168 return recursive_build_subsequence(a, b, matrix, i, j - 1) |
b4caea436f4d
Improve the highlighting of differences using a longest common subsequence algorithm.
Peter Hosey <hg@boredzo.org>
parents:
19
diff
changeset
|
169 else: |
b4caea436f4d
Improve the highlighting of differences using a longest common subsequence algorithm.
Peter Hosey <hg@boredzo.org>
parents:
19
diff
changeset
|
170 return recursive_build_subsequence(a, b, matrix, i - 1, j) |
b4caea436f4d
Improve the highlighting of differences using a longest common subsequence algorithm.
Peter Hosey <hg@boredzo.org>
parents:
19
diff
changeset
|
171 |
b4caea436f4d
Improve the highlighting of differences using a longest common subsequence algorithm.
Peter Hosey <hg@boredzo.org>
parents:
19
diff
changeset
|
172 return recursive_build_subsequence(a, b) |
19
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
173 |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
174 def common_and_distinct_substrings(a, b): |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
175 "Takes two strings, a and b, tokenizes them, and returns a linked list whose nodes contain runs of either common or unique tokens." |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
176 def tokenize(a): |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
177 "Each token is an identifier, a number, or a single character." |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
178 import re |
27
5f17911c4fe6
Expanded this comment to acknowledge that the same sub-expression covers both decimal and octal (beginning with 0) number literals.
Peter Hosey <hg@boredzo.org>
parents:
26
diff
changeset
|
179 # Word in identifier, word in macro name (MACRO_NAME), binary number, hex number, decimal or octal number, operator, other punctuation. |
26
3b33b1c48880
Changed the token expression to add support for sub-identifier word differencing.
Peter Hosey <hg@boredzo.org>
parents:
25
diff
changeset
|
180 token_exp = re.compile('[_A-Z]*[_a-z0-9]+:?|_??[A-Z0-9]+:?|0b[01]+|0[xX][0-9A-Fa-f]+|[0-9]+|[-+*|&^/%\[\]<=>,]|[()\\\\;`{}]') |
19
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
181 start = 0 |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
182 for match in token_exp.finditer(a): |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
183 for ch in a[start:match.start()]: |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
184 yield ch |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
185 yield match.group(0) |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
186 start = match.end() |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
187 |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
188 remainder = a[start:] |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
189 if remainder: |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
190 yield remainder |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
191 |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
192 a = list(tokenize(a)) |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
193 b = list(tokenize(b)) |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
194 |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
195 class DualPayloadLinkedListNode(object): |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
196 "This linked list gives each node two next pointers." |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
197 def __init__(self, a, b, differ=None): |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
198 self.a = a |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
199 self.b = b |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
200 self.next = None |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
201 if differ is None: |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
202 differ = (self.a != self.b) |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
203 self.differ = differ |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
204 def __iter__(self): |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
205 def walk_linked_list(x): |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
206 while x is not None: |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
207 yield x |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
208 x = x.next |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
209 return walk_linked_list(self) |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
210 def __repr__(self): |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
211 return repr([('(%r, %r)' % (x.a, x.b)) if x.differ else repr(x.a) for x in self]) |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
212 |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
213 # Linked-list nodes for common substrings will have a single Substring object in both payloads. |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
214 # Nodes for difference runs will have a string in each payload. |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
215 empty_substring = Substring(a, 0, 0, b, 0, 0) |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
216 chunks_head = DualPayloadLinkedListNode(empty_substring, empty_substring, False) |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
217 # This node is used when the input strings have no common substrings. When they do have common substrings, this node will be replaced with a real node. |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
218 chunks_head.next = DualPayloadLinkedListNode(empty_substring, empty_substring, False) |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
219 # Not chunks_head.next, since it will be replaced. |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
220 chunks_tail = chunks_head |
20
b4caea436f4d
Improve the highlighting of differences using a longest common subsequence algorithm.
Peter Hosey <hg@boredzo.org>
parents:
19
diff
changeset
|
221 for sub in sorted(common_subsequence(a, b)): |
19
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
222 last_sub = chunks_tail.a |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
223 a_dif_run = ''.join(a[last_sub.a_stop:sub.a_start]) |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
224 b_dif_run = ''.join(b[last_sub.b_stop:sub.b_start]) |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
225 if a_dif_run or b_dif_run: |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
226 chunks_tail.next = DualPayloadLinkedListNode(a_dif_run, b_dif_run, True) |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
227 chunks_tail = chunks_tail.next |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
228 chunks_tail.next = DualPayloadLinkedListNode(sub, sub, False) |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
229 chunks_tail = chunks_tail.next |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
230 else: |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
231 # Get what comes after the last substring, if anything. |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
232 last_sub = chunks_tail.a |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
233 a_dif_run = ''.join(a[last_sub.a_stop:]) |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
234 b_dif_run = ''.join(b[last_sub.b_stop:]) |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
235 if a_dif_run or b_dif_run: |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
236 chunks_tail.next = DualPayloadLinkedListNode(a_dif_run, b_dif_run, True) |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
237 |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
238 return chunks_head.next |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
239 |
7
c6337f653d9b
Added some comments documenting the OrderedDict class and our instance's contents.
Peter Hosey
parents:
6
diff
changeset
|
240 # 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
|
241 # 'Index: ': File marker (unified diff) |
c6337f653d9b
Added some comments documenting the OrderedDict class and our instance's contents.
Peter Hosey
parents:
6
diff
changeset
|
242 # '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
|
243 # '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
|
244 # '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
|
245 # '---': 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
|
246 # '+++': 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
|
247 # '-': 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
|
248 # '+': 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
|
249 # ' ': Line that hasn't changed |
c6337f653d9b
Added some comments documenting the OrderedDict class and our instance's contents.
Peter Hosey
parents:
6
diff
changeset
|
250 # '@@': 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
|
251 # |
c6337f653d9b
Added some comments documenting the OrderedDict class and our instance's contents.
Peter Hosey
parents:
6
diff
changeset
|
252 # 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
|
253 class OrderedDict(dict): |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
254 def __init__(self, input=None): |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
255 if input is None: |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
256 self.keys = [] |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
257 super(OrderedDict, self).__init__() |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
258 elif isinstance(input, dict): |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
259 self.keys = list(input) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
260 super(OrderedDict, self).__init__(input) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
261 else: |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
262 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
|
263 super(OrderedDict, self).__init__(input) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
264 def __iter__(self): |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
265 return iter(self.keys) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
266 def __setitem__(self, k, v): |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
267 if k not in self: |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
268 self.keys.append(k) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
269 super(OrderedDict, self).__setitem__(k, v) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
270 def __delitem__(self, k): |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
271 super(OrderedDict, self).__delitem__(k) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
272 self.keys.remove(k) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
273 |
7
c6337f653d9b
Added some comments documenting the OrderedDict class and our instance's contents.
Peter Hosey
parents:
6
diff
changeset
|
274 # 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
|
275 # 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
|
276 prefixes = OrderedDict() |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
277 prefixes['---'] = ( |
8
4bb2557d24cd
Now that these can come from the environment, they are variables. Lowercasing their names for this reason.
Peter Hosey
parents:
7
diff
changeset
|
278 COLOR_FORMAT % (removed_color,) |
1
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
279 + BEGIN_REVERSE_FORMAT |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
280 + '---' |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
281 + END_REVERSE_FORMAT |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
282 ) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
283 prefixes['+++'] = ( |
8
4bb2557d24cd
Now that these can come from the environment, they are variables. Lowercasing their names for this reason.
Peter Hosey
parents:
7
diff
changeset
|
284 COLOR_FORMAT % (added_color,) |
1
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
285 + BEGIN_REVERSE_FORMAT |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
286 + '+++' |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
287 + END_REVERSE_FORMAT |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
288 ) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
289 prefixes['-'] = ( |
8
4bb2557d24cd
Now that these can come from the environment, they are variables. Lowercasing their names for this reason.
Peter Hosey
parents:
7
diff
changeset
|
290 COLOR_FORMAT % (removed_color,) |
1
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
291 + BEGIN_REVERSE_FORMAT |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
292 + '-' |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
293 + END_REVERSE_FORMAT |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
294 ) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
295 prefixes['+'] = ( |
8
4bb2557d24cd
Now that these can come from the environment, they are variables. Lowercasing their names for this reason.
Peter Hosey
parents:
7
diff
changeset
|
296 COLOR_FORMAT % (added_color,) |
1
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
297 + BEGIN_REVERSE_FORMAT |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
298 + '+' |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
299 + END_REVERSE_FORMAT |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
300 ) |
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
|
301 prefixes['old mode'] = ( # Git-style diffs only |
8
4bb2557d24cd
Now that these can come from the environment, they are variables. Lowercasing their names for this reason.
Peter Hosey
parents:
7
diff
changeset
|
302 COLOR_FORMAT % (old_mode_color,) |
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
|
303 + 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
|
304 + '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
|
305 + 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
|
306 ) |
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
|
307 prefixes['new mode'] = ( # Git-style diffs only |
8
4bb2557d24cd
Now that these can come from the environment, they are variables. Lowercasing their names for this reason.
Peter Hosey
parents:
7
diff
changeset
|
308 COLOR_FORMAT % (new_mode_color,) |
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
|
309 + 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
|
310 + '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
|
311 + 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
|
312 ) |
8
4bb2557d24cd
Now that these can come from the environment, they are variables. Lowercasing their names for this reason.
Peter Hosey
parents:
7
diff
changeset
|
313 prefixes['Index: '] = COLOR_FORMAT % (index_color,) + 'Index: ' |
4bb2557d24cd
Now that these can come from the environment, they are variables. Lowercasing their names for this reason.
Peter Hosey
parents:
7
diff
changeset
|
314 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
|
315 prefixes['@@'] = ( |
8
4bb2557d24cd
Now that these can come from the environment, they are variables. Lowercasing their names for this reason.
Peter Hosey
parents:
7
diff
changeset
|
316 COLOR_FORMAT % (hunk_start_color,) |
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
|
317 + 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
|
318 + '@@' |
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
|
319 ) |
0 | 320 |
17
54a209909531
Make the file importable for debugging purposes by wrapping the main-program-only bits in an if __name__ == "__main__" block.
Peter Hosey <hg@boredzo.org>
parents:
15
diff
changeset
|
321 if __name__ == "__main__": |
54a209909531
Make the file importable for debugging purposes by wrapping the main-program-only bits in an if __name__ == "__main__" block.
Peter Hosey <hg@boredzo.org>
parents:
15
diff
changeset
|
322 if sys.stdin.isatty(): |
54a209909531
Make the file importable for debugging purposes by wrapping the main-program-only bits in an if __name__ == "__main__" block.
Peter Hosey <hg@boredzo.org>
parents:
15
diff
changeset
|
323 # Standard input is a TTY, meaning that the user ran 'diff-colorize' at the shell prompt, without redirecting anything into it. Print usage info and exit. |
54a209909531
Make the file importable for debugging purposes by wrapping the main-program-only bits in an if __name__ == "__main__" block.
Peter Hosey <hg@boredzo.org>
parents:
15
diff
changeset
|
324 sys.exit(USAGE) |
9 | 325 |
18
83d58ccc70bf
Interleave adjacent runs of consecutive old and new lines into alternating old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
17
diff
changeset
|
326 # Buffers to support interleaving old and new lines that were contiguous runs. |
83d58ccc70bf
Interleave adjacent runs of consecutive old and new lines into alternating old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
17
diff
changeset
|
327 buffer_old = [] # '-' lines |
83d58ccc70bf
Interleave adjacent runs of consecutive old and new lines into alternating old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
17
diff
changeset
|
328 buffer_new = [] # '+' lines |
83d58ccc70bf
Interleave adjacent runs of consecutive old and new lines into alternating old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
17
diff
changeset
|
329 |
21
929a488c4960
Prevent lines that are completely different from being completely highlighted as different.
Peter Hosey <hg@boredzo.org>
parents:
20
diff
changeset
|
330 from string import whitespace |
929a488c4960
Prevent lines that are completely different from being completely highlighted as different.
Peter Hosey <hg@boredzo.org>
parents:
20
diff
changeset
|
331 |
19
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
332 def flush_buffers(buffer_old, buffer_new): |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
333 "Flush the buffers, interleaving the lines and highlighting differences between them." |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
334 def print_single_line(buffered_line): |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
335 prefix = '-' if buffered_line.startswith('-') else '+' |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
336 buffered_line = buffered_line[len(prefix):] |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
337 |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
338 sys.stdout.write(prefixes[prefix]) |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
339 sys.stdout.write(buffered_line) |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
340 sys.stdout.write(RESET_FORMAT) |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
341 |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
342 last_line_if_old = None |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
343 for buffered_line in interleave(buffer_old, buffer_new): |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
344 if buffered_line.startswith('-'): |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
345 if last_line_if_old is not None: |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
346 print_single_line(last_line_if_old) |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
347 last_line_if_old = buffered_line |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
348 else: |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
349 if last_line_if_old is None: |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
350 # No old line immediately preceding this, so just print it. |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
351 print_single_line(buffered_line) |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
352 else: |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
353 old_line = last_line_if_old |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
354 new_line = buffered_line |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
355 |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
356 old_line_output = [prefixes['-']] |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
357 new_line_output = [prefixes['+']] |
21
929a488c4960
Prevent lines that are completely different from being completely highlighted as different.
Peter Hosey <hg@boredzo.org>
parents:
20
diff
changeset
|
358 |
929a488c4960
Prevent lines that are completely different from being completely highlighted as different.
Peter Hosey <hg@boredzo.org>
parents:
20
diff
changeset
|
359 differenced_lines = common_and_distinct_substrings(old_line[1:], new_line[1:]) |
929a488c4960
Prevent lines that are completely different from being completely highlighted as different.
Peter Hosey <hg@boredzo.org>
parents:
20
diff
changeset
|
360 lines_have_any_non_whitespace_part_in_common = False |
929a488c4960
Prevent lines that are completely different from being completely highlighted as different.
Peter Hosey <hg@boredzo.org>
parents:
20
diff
changeset
|
361 for node in differenced_lines: |
929a488c4960
Prevent lines that are completely different from being completely highlighted as different.
Peter Hosey <hg@boredzo.org>
parents:
20
diff
changeset
|
362 if not node.differ: |
929a488c4960
Prevent lines that are completely different from being completely highlighted as different.
Peter Hosey <hg@boredzo.org>
parents:
20
diff
changeset
|
363 if str(node.a) not in whitespace: |
929a488c4960
Prevent lines that are completely different from being completely highlighted as different.
Peter Hosey <hg@boredzo.org>
parents:
20
diff
changeset
|
364 lines_have_any_non_whitespace_part_in_common = True |
929a488c4960
Prevent lines that are completely different from being completely highlighted as different.
Peter Hosey <hg@boredzo.org>
parents:
20
diff
changeset
|
365 break |
929a488c4960
Prevent lines that are completely different from being completely highlighted as different.
Peter Hosey <hg@boredzo.org>
parents:
20
diff
changeset
|
366 |
929a488c4960
Prevent lines that are completely different from being completely highlighted as different.
Peter Hosey <hg@boredzo.org>
parents:
20
diff
changeset
|
367 for node in differenced_lines: |
929a488c4960
Prevent lines that are completely different from being completely highlighted as different.
Peter Hosey <hg@boredzo.org>
parents:
20
diff
changeset
|
368 if lines_have_any_non_whitespace_part_in_common and node.differ: |
19
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
369 old_line_output.append(BEGIN_REVERSE_FORMAT) |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
370 old_line_output.append(str(node.a)) |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
371 old_line_output.append(END_REVERSE_FORMAT) |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
372 |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
373 new_line_output.append(BEGIN_REVERSE_FORMAT) |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
374 new_line_output.append(str(node.b)) |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
375 new_line_output.append(END_REVERSE_FORMAT) |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
376 else: |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
377 old_line_output.append(str(node.a)) |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
378 new_line_output.append(str(node.b)) |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
379 |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
380 last_line_if_old = None |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
381 sys.stdout.writelines(''.join(old_line_output)) |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
382 sys.stdout.writelines(''.join(new_line_output)) |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
383 else: |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
384 if last_line_if_old is not None: |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
385 print_single_line(last_line_if_old) |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
386 |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
387 del buffer_old[:] |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
388 del buffer_new[:] |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
389 |
17
54a209909531
Make the file importable for debugging purposes by wrapping the main-program-only bits in an if __name__ == "__main__" block.
Peter Hosey <hg@boredzo.org>
parents:
15
diff
changeset
|
390 for line in fileinput.input(): |
18
83d58ccc70bf
Interleave adjacent runs of consecutive old and new lines into alternating old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
17
diff
changeset
|
391 if line.startswith('-') and not line.startswith('---'): |
83d58ccc70bf
Interleave adjacent runs of consecutive old and new lines into alternating old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
17
diff
changeset
|
392 buffer_old.append(line) |
83d58ccc70bf
Interleave adjacent runs of consecutive old and new lines into alternating old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
17
diff
changeset
|
393 continue |
83d58ccc70bf
Interleave adjacent runs of consecutive old and new lines into alternating old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
17
diff
changeset
|
394 elif line.startswith('+') and not line.startswith('+++'): |
83d58ccc70bf
Interleave adjacent runs of consecutive old and new lines into alternating old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
17
diff
changeset
|
395 buffer_new.append(line) |
83d58ccc70bf
Interleave adjacent runs of consecutive old and new lines into alternating old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
17
diff
changeset
|
396 continue |
83d58ccc70bf
Interleave adjacent runs of consecutive old and new lines into alternating old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
17
diff
changeset
|
397 else: |
19
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
398 flush_buffers(buffer_old, buffer_new) |
18
83d58ccc70bf
Interleave adjacent runs of consecutive old and new lines into alternating old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
17
diff
changeset
|
399 |
17
54a209909531
Make the file importable for debugging purposes by wrapping the main-program-only bits in an if __name__ == "__main__" block.
Peter Hosey <hg@boredzo.org>
parents:
15
diff
changeset
|
400 for prefix_to_test in prefixes: |
54a209909531
Make the file importable for debugging purposes by wrapping the main-program-only bits in an if __name__ == "__main__" block.
Peter Hosey <hg@boredzo.org>
parents:
15
diff
changeset
|
401 if line.startswith(prefix_to_test): |
54a209909531
Make the file importable for debugging purposes by wrapping the main-program-only bits in an if __name__ == "__main__" block.
Peter Hosey <hg@boredzo.org>
parents:
15
diff
changeset
|
402 sys.stdout.write(prefixes[prefix_to_test]) |
54a209909531
Make the file importable for debugging purposes by wrapping the main-program-only bits in an if __name__ == "__main__" block.
Peter Hosey <hg@boredzo.org>
parents:
15
diff
changeset
|
403 line = line[len(prefix_to_test):] |
0 | 404 |
17
54a209909531
Make the file importable for debugging purposes by wrapping the main-program-only bits in an if __name__ == "__main__" block.
Peter Hosey <hg@boredzo.org>
parents:
15
diff
changeset
|
405 sys.stdout.write(line) |
0 | 406 |
17
54a209909531
Make the file importable for debugging purposes by wrapping the main-program-only bits in an if __name__ == "__main__" block.
Peter Hosey <hg@boredzo.org>
parents:
15
diff
changeset
|
407 sys.stdout.write(RESET_FORMAT) |
19
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
408 else: |
b709258a2fc2
Added highlighting differences between consecutive old and new lines.
Peter Hosey <hg@boredzo.org>
parents:
18
diff
changeset
|
409 flush_buffers(buffer_old, buffer_new) |