Mercurial > diff-colorize
annotate diff-colorize.py @ 1:44f86539d245
Refactored to allow me to more easily add new prefixes.
author | Peter Hosey |
---|---|
date | Sun, 10 Aug 2008 23:25:24 -0700 |
parents | 2df4ed64a388 |
children | 9eda9139d627 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
2 | |
3 RESET_FORMAT = '\033[0m' | |
4 COLOR_FORMAT = '\033[38;5;%um' | |
5 BEGIN_REVERSE_FORMAT = '\033[7m' | |
6 END_REVERSE_FORMAT = '\033[27m' | |
7 | |
8 INDEX_COLOR = 32 | |
9 REMOVED_COLOR = 203 | |
10 ADDED_COLOR = 2 | |
11 | |
1
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
12 class OrderedDict(dict): |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
13 def __init__(self, input=None): |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
14 if input is None: |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
15 self.keys = [] |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
16 super(OrderedDict, self).__init__() |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
17 elif isinstance(input, dict): |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
18 self.keys = list(input) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
19 super(OrderedDict, self).__init__(input) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
20 else: |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
21 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
|
22 super(OrderedDict, self).__init__(input) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
23 def __iter__(self): |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
24 return iter(self.keys) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
25 def __setitem__(self, k, v): |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
26 if k not in self: |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
27 self.keys.append(k) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
28 super(OrderedDict, self).__setitem__(k, v) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
29 def __delitem__(self, k): |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
30 super(OrderedDict, self).__delitem__(k) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
31 self.keys.remove(k) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
32 |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
33 prefixes = OrderedDict() |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
34 prefixes['---'] = ( |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
35 COLOR_FORMAT % (REMOVED_COLOR,) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
36 + BEGIN_REVERSE_FORMAT |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
37 + '---' |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
38 + END_REVERSE_FORMAT |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
39 ) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
40 prefixes['+++'] = ( |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
41 COLOR_FORMAT % (ADDED_COLOR,) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
42 + BEGIN_REVERSE_FORMAT |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
43 + '+++' |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
44 + END_REVERSE_FORMAT |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
45 ) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
46 prefixes['-'] = ( |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
47 COLOR_FORMAT % (REMOVED_COLOR,) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
48 + BEGIN_REVERSE_FORMAT |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
49 + '-' |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
50 + END_REVERSE_FORMAT |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
51 ) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
52 prefixes['+'] = ( |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
53 COLOR_FORMAT % (ADDED_COLOR,) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
54 + BEGIN_REVERSE_FORMAT |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
55 + '+' |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
56 + END_REVERSE_FORMAT |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
57 ) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
58 prefixes['Index: '] = COLOR_FORMAT % (INDEX_COLOR,) + 'Index: ' |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
59 prefixes['diff --git '] = COLOR_FORMAT % (INDEX_COLOR,) + 'diff --git ' |
0 | 60 |
61 import sys | |
62 import fileinput | |
63 | |
64 for line in fileinput.input(): | |
1
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
65 for prefix_to_test in prefixes: |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
66 if line.startswith(prefix_to_test): |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
67 sys.stdout.write(prefixes[prefix_to_test]) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
68 line = line[len(prefix_to_test):] |
0 | 69 |
70 sys.stdout.write(line) | |
71 | |
1
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
72 sys.stdout.write(RESET_FORMAT) |
44f86539d245
Refactored to allow me to more easily add new prefixes.
Peter Hosey
parents:
0
diff
changeset
|
73 |
0 | 74 print RESET_FORMAT |