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 |
|
12 prefixes_to_invert = ['---', '+++', '-', '+'] |
|
13 |
|
14 import sys |
|
15 import fileinput |
|
16 |
|
17 for line in fileinput.input(): |
|
18 if line.startswith('Index: ') or line.startswith('diff --git '): |
|
19 color = INDEX_COLOR |
|
20 elif line.startswith('-'): |
|
21 color = REMOVED_COLOR |
|
22 elif line.startswith('+'): |
|
23 color = ADDED_COLOR |
|
24 else: |
|
25 color = None |
|
26 |
|
27 if color is not None: |
|
28 sys.stdout.write(COLOR_FORMAT % (color,)) |
|
29 for prefix in prefixes_to_invert: |
|
30 if line.startswith(prefix): |
|
31 sys.stdout.write(BEGIN_REVERSE_FORMAT) |
|
32 sys.stdout.write(prefix) |
|
33 sys.stdout.write(END_REVERSE_FORMAT) |
|
34 line = line[len(prefix):] |
|
35 break |
|
36 else: |
|
37 sys.stdout.write(RESET_FORMAT) |
|
38 |
|
39 sys.stdout.write(line) |
|
40 |
|
41 print RESET_FORMAT |