forked from oldratlee/useful-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
colines
executable file
·47 lines (39 loc) · 1.07 KB
/
colines
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Function
# cat lines colorfully. colines means COLorful LINES.
#
# @Usage
# $ echo -n 'Hello\nWorld' | colines
# $ colines /path/to/file1
# $ colines /path/to/file1 /path/to/file2
__author__ = 'Jerry Lee'
import sys
ECHO_COLORS = (37, 31, 32, 34, 33, 35, 56)
idx = 0
def color_print(*args):
line = " ".join(args)
global idx
idx += 1
color = ECHO_COLORS[idx % len(ECHO_COLORS)]
if sys.stdout.isatty():
line_ = """\033[1;%sm%s\033[0m""" % (color, line)
print line_
else:
print(line)
if __name__ == '__main__':
if len(sys.argv) > 1:
for arg in sys.argv[1:]:
if len(sys.argv) > 2:
print('=' * 80)
print(arg)
print('=' * 80)
for line in open(arg).readlines():
color_print(line.rstrip('\r\n'))
else:
# Read from std input
while True:
line = sys.stdin.readline()
if not line:
break
color_print(line.rstrip('\r\n'))