comparison diff-colorize.py @ 9:cce6b860a98d

Added usage information.
author Peter Hosey
date Mon, 11 Aug 2008 00:26:26 -0700
parents 4bb2557d24cd
children 73f326ec8142
comparison
equal deleted inserted replaced
8:4bb2557d24cd 9:cce6b860a98d
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'
18
19 USAGE = """
20 Usage: diff ... | diff-colorize
21 or: diff-colorize < foo.diff
22
23 Reads unified or git-style diff data from standard input, colorizes it, and writes the result to standard output.
24
25 You can customize the color numbers used by setting these variables in your environment:
26 * DIFF_INDEX_COLOR (lines starting with "Index: " or "diff --git ")
27 * DIFF_OLD_MODE_COLOR (lines starting with "old mode"; these only appear in git-style diffs)
28 * DIFF_NEW_MODE_COLOR (lines starting with "new mode"; these only appear in git-style diffs)
29 * DIFF_REMOVED_COLOR (lines starting with "-")
30 * DIFF_ADDED_COLOR (lines starting with "+")
31 * DIFF_HUNK_START_COLOR (lines starting with "@@")
32 """.strip()
18 33
19 # Everything in the unified diff format is identified by a prefix. The prefixes are: 34 # Everything in the unified diff format is identified by a prefix. The prefixes are:
20 # 'Index: ': File marker (unified diff) 35 # 'Index: ': File marker (unified diff)
21 # 'diff --git': File marker (git-style diff) 36 # 'diff --git': File marker (git-style diff)
22 # 'old mode': File permissions mode before change 37 # 'old mode': File permissions mode before change
95 COLOR_FORMAT % (hunk_start_color,) 110 COLOR_FORMAT % (hunk_start_color,)
96 + BEGIN_REVERSE_FORMAT 111 + BEGIN_REVERSE_FORMAT
97 + '@@' 112 + '@@'
98 ) 113 )
99 114
115 if sys.stdin.isatty():
116 # 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.
117 sys.exit(USAGE)
118
100 for line in fileinput.input(): 119 for line in fileinput.input():
101 for prefix_to_test in prefixes: 120 for prefix_to_test in prefixes:
102 if line.startswith(prefix_to_test): 121 if line.startswith(prefix_to_test):
103 sys.stdout.write(prefixes[prefix_to_test]) 122 sys.stdout.write(prefixes[prefix_to_test])
104 line = line[len(prefix_to_test):] 123 line = line[len(prefix_to_test):]