-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquickcolorize
executable file
·46 lines (38 loc) · 1.03 KB
/
quickcolorize
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python
from QuickColorize import colorize
# a quick command-line program for colorizing stdin or a list of files
# Example:
# % quickcolorize term1 term2 term3 < somefilename
# Use -- to separate terms to highlight from filenames:
# % quickcolorize term1 term2 term3 -- file1 file2 file3
# Author: David McClosky
# Homepage: http://zorglish.org
# Code: http://github.com/dmcc
import sys
filenames = []
stdin_mode = True
mapping = {}
for arg in sys.argv[1:]:
if arg == '--':
stdin_mode = False
continue
if stdin_mode:
mapping[arg] = colorize(arg)
else:
filenames.append(arg)
def file_iter():
if stdin_mode:
yield sys.stdin
else:
for filename in filenames:
f = open(filename)
yield f
del f
for input_file in file_iter():
for line in input_file:
for old, new in mapping.items():
line = line.replace(old, new)
try:
sys.stdout.write(line)
except OSError:
raise SystemExit