comparison diff-colorize.py @ 8:4bb2557d24cd

Now that these can come from the environment, they are variables. Lowercasing their names for this reason.
author Peter Hosey
date Mon, 11 Aug 2008 00:21:13 -0700
parents c6337f653d9b
children cce6b860a98d
comparison
equal deleted inserted replaced
7:c6337f653d9b 8:4bb2557d24cd
2 2
3 import sys 3 import sys
4 import os 4 import os
5 import fileinput 5 import fileinput
6 6
7 INDEX_COLOR = int(os.environ.get('DIFF_INDEX_COLOR', 32)) 7 index_color = int(os.environ.get('DIFF_INDEX_COLOR', 32))
8 OLD_MODE_COLOR = int(os.environ.get('DIFF_OLD_MODE_COLOR', 124)) 8 old_mode_color = int(os.environ.get('DIFF_OLD_MODE_COLOR', 124))
9 NEW_MODE_COLOR = int(os.environ.get('DIFF_NEW_MODE_COLOR', 28)) 9 new_mode_color = int(os.environ.get('DIFF_NEW_MODE_COLOR', 28))
10 REMOVED_COLOR = int(os.environ.get('DIFF_REMOVED_COLOR', 203)) 10 removed_color = int(os.environ.get('DIFF_REMOVED_COLOR', 203))
11 ADDED_COLOR = int(os.environ.get('DIFF_ADDED_COLOR', 2)) 11 added_color = int(os.environ.get('DIFF_ADDED_COLOR', 2))
12 HUNK_START_COLOR = int(os.environ.get('DIFF_HUNK_START_COLOR', 32)) 12 hunk_start_color = int(os.environ.get('DIFF_HUNK_START_COLOR', 32))
13 13
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'
52 52
53 # Each value includes not only the terminal-config characters, but also the key, somewhere within it (possibly between two terminal-config strings). 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. 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.
55 prefixes = OrderedDict() 55 prefixes = OrderedDict()
56 prefixes['---'] = ( 56 prefixes['---'] = (
57 COLOR_FORMAT % (REMOVED_COLOR,) 57 COLOR_FORMAT % (removed_color,)
58 + BEGIN_REVERSE_FORMAT 58 + BEGIN_REVERSE_FORMAT
59 + '---' 59 + '---'
60 + END_REVERSE_FORMAT 60 + END_REVERSE_FORMAT
61 ) 61 )
62 prefixes['+++'] = ( 62 prefixes['+++'] = (
63 COLOR_FORMAT % (ADDED_COLOR,) 63 COLOR_FORMAT % (added_color,)
64 + BEGIN_REVERSE_FORMAT 64 + BEGIN_REVERSE_FORMAT
65 + '+++' 65 + '+++'
66 + END_REVERSE_FORMAT 66 + END_REVERSE_FORMAT
67 ) 67 )
68 prefixes['-'] = ( 68 prefixes['-'] = (
69 COLOR_FORMAT % (REMOVED_COLOR,) 69 COLOR_FORMAT % (removed_color,)
70 + BEGIN_REVERSE_FORMAT 70 + BEGIN_REVERSE_FORMAT
71 + '-' 71 + '-'
72 + END_REVERSE_FORMAT 72 + END_REVERSE_FORMAT
73 ) 73 )
74 prefixes['+'] = ( 74 prefixes['+'] = (
75 COLOR_FORMAT % (ADDED_COLOR,) 75 COLOR_FORMAT % (added_color,)
76 + BEGIN_REVERSE_FORMAT 76 + BEGIN_REVERSE_FORMAT
77 + '+' 77 + '+'
78 + END_REVERSE_FORMAT 78 + END_REVERSE_FORMAT
79 ) 79 )
80 prefixes['old mode'] = ( # Git-style diffs only 80 prefixes['old mode'] = ( # Git-style diffs only
81 COLOR_FORMAT % (OLD_MODE_COLOR,) 81 COLOR_FORMAT % (old_mode_color,)
82 + BEGIN_REVERSE_FORMAT 82 + BEGIN_REVERSE_FORMAT
83 + 'old mode' 83 + 'old mode'
84 + END_REVERSE_FORMAT 84 + END_REVERSE_FORMAT
85 ) 85 )
86 prefixes['new mode'] = ( # Git-style diffs only 86 prefixes['new mode'] = ( # Git-style diffs only
87 COLOR_FORMAT % (NEW_MODE_COLOR,) 87 COLOR_FORMAT % (new_mode_color,)
88 + BEGIN_REVERSE_FORMAT 88 + BEGIN_REVERSE_FORMAT
89 + 'new mode' 89 + 'new mode'
90 + END_REVERSE_FORMAT 90 + END_REVERSE_FORMAT
91 ) 91 )
92 prefixes['Index: '] = COLOR_FORMAT % (INDEX_COLOR,) + 'Index: ' 92 prefixes['Index: '] = COLOR_FORMAT % (index_color,) + 'Index: '
93 prefixes['diff --git '] = COLOR_FORMAT % (INDEX_COLOR,) + 'diff --git ' 93 prefixes['diff --git '] = COLOR_FORMAT % (index_color,) + 'diff --git '
94 prefixes['@@'] = ( 94 prefixes['@@'] = (
95 COLOR_FORMAT % (HUNK_START_COLOR,) 95 COLOR_FORMAT % (hunk_start_color,)
96 + BEGIN_REVERSE_FORMAT 96 + BEGIN_REVERSE_FORMAT
97 + '@@' 97 + '@@'
98 ) 98 )
99 99
100 for line in fileinput.input(): 100 for line in fileinput.input():