comparison 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
comparison
equal deleted inserted replaced
6:d58b4e2e12d4 7:c6337f653d9b
14 RESET_FORMAT = '\033[0m' 14 RESET_FORMAT = '\033[0m'
15 COLOR_FORMAT = '\033[38;5;%um' 15 COLOR_FORMAT = '\033[38;5;%um'
16 BEGIN_REVERSE_FORMAT = '\033[7m' 16 BEGIN_REVERSE_FORMAT = '\033[7m'
17 END_REVERSE_FORMAT = '\033[27m' 17 END_REVERSE_FORMAT = '\033[27m'
18 18
19 # Everything in the unified diff format is identified by a prefix. The prefixes are:
20 # 'Index: ': File marker (unified diff)
21 # 'diff --git': File marker (git-style diff)
22 # 'old mode': File permissions mode before change
23 # 'new mode': File permissions mode after change
24 # '---': Defining '-' (giving the name and modification date of the file before change)
25 # '+++': Defining '+' (giving the name and modification date of the file after change)
26 # '-': Line before change (i.e., removed)
27 # '+': Line after change (i.e., added)
28 # ' ': Line that hasn't changed
29 # '@@': Hunk start (@@ -start,length +start, length @@)
30 #
31 # We need to look for these prefixes in order, in order to handle '---'/'+++' before '-'/'+'. Hence the OrderedDict.
19 class OrderedDict(dict): 32 class OrderedDict(dict):
20 def __init__(self, input=None): 33 def __init__(self, input=None):
21 if input is None: 34 if input is None:
22 self.keys = [] 35 self.keys = []
23 super(OrderedDict, self).__init__() 36 super(OrderedDict, self).__init__()
35 super(OrderedDict, self).__setitem__(k, v) 48 super(OrderedDict, self).__setitem__(k, v)
36 def __delitem__(self, k): 49 def __delitem__(self, k):
37 super(OrderedDict, self).__delitem__(k) 50 super(OrderedDict, self).__delitem__(k)
38 self.keys.remove(k) 51 self.keys.remove(k)
39 52
53 # Each value includes not only the terminal-config characters, but also the key, somewhere within it (possibly between two terminal-config strings).
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.
40 prefixes = OrderedDict() 55 prefixes = OrderedDict()
41 prefixes['---'] = ( 56 prefixes['---'] = (
42 COLOR_FORMAT % (REMOVED_COLOR,) 57 COLOR_FORMAT % (REMOVED_COLOR,)
43 + BEGIN_REVERSE_FORMAT 58 + BEGIN_REVERSE_FORMAT
44 + '---' 59 + '---'