-
Notifications
You must be signed in to change notification settings - Fork 4
/
rp.py
executable file
·67 lines (52 loc) · 2.36 KB
/
rp.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
from optparse import OptionParser
from subprocess import Popen, PIPE, STDOUT
parser = OptionParser("usage: cat logfile | %prog [options] searchterm1 searchterm2...")
parser.add_option("-i", action="store_true", dest="ignore_case", default=False, help="perform a case insensitive search")
parser.add_option("-k", action="store_true", dest="display_all", default=False, help="only highlight, do not filter")
(options, args) = parser.parse_args()
colors = [
('green','04;01;32'),
('yellow','04;01;33'),
('red','04;01;31'),
('blue','04;01;34'),
('purple','0;04;35'),
('magenta','04;01;35'),
('cyan','04;01;36'),
('brown','0;04;33'),
]
if len(args) == 0:
parser.print_help()
sys.exit()
op = sys.stdin.read()
if not options.display_all:
if options.ignore_case:
if sys.version_info > (3, ):
p = Popen(["egrep", "|".join(args), "--color=always", "-i"], stdout=PIPE, stdin=PIPE, stderr=STDOUT, env=os.environ.copy(),encoding="utf-8")
else:
p = Popen(["egrep", "|".join(args), "--color=always", "-i"], stdout=PIPE, stdin=PIPE, stderr=STDOUT, env=os.environ.copy())
else:
if sys.version_info > (3, ):
p = Popen(["egrep", "|".join(args), "--color=always"], stdout=PIPE, stdin=PIPE, stderr=STDOUT, env=os.environ.copy(),encoding="utf-8")
else:
p = Popen(["egrep", "|".join(args), "--color=always"], stdout=PIPE, stdin=PIPE, stderr=STDOUT, env=os.environ.copy())
op = p.communicate(input=op)[0]
for i,srch in enumerate(args):
color = colors[i%len(colors)][1]
env=os.environ.copy()
env['GREP_COLORS'] = "mt="+color
if options.ignore_case:
if sys.version_info > (3, ):
p = Popen(["egrep", srch+"|", "--color=always", "-i"], stdout=PIPE, stdin=PIPE, stderr=STDOUT, env=env,encoding="utf-8")
else:
p = Popen(["egrep", srch+"|", "--color=always", "-i"], stdout=PIPE, stdin=PIPE, stderr=STDOUT, env=env)
else:
if sys.version_info > (3, ):
p = Popen(["egrep", srch+"|", "--color=always"], stdout=PIPE, stdin=PIPE, stderr=STDOUT, env=env,encoding="utf-8")
else:
p = Popen(["egrep", srch+"|", "--color=always"], stdout=PIPE, stdin=PIPE, stderr=STDOUT, env=env)
op = p.communicate(input=op)[0]
print(op)