Skip to content

Commit

Permalink
Merge pull request #16 from sublee/master
Browse files Browse the repository at this point in the history
fixed issue #13, #14, #15
  • Loading branch information
nicoulaj committed Feb 6, 2016
2 parents 8de2bee + baeb1ad commit c00b69f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
41 changes: 29 additions & 12 deletions rainbow
Expand Up @@ -310,12 +310,22 @@ def apply_filters(line):
for (pattern,pattern_filter) in list(patterns.items()):

# Apply filter for each match.
for match in pattern_filter['regex'].finditer(line):
line = line.replace(match.group(),pattern_filter['pattern_start'] + match.group() + pattern_filter['pattern_end'])
line = pattern_filter['regex'].sub(pattern_filter['pattern_start'] + r'\g<0>' + pattern_filter['pattern_end'], line)

return line.rstrip()


def reset_all():
"""
Reset color, background color, intensity.
"""

sys.stdout.write(chr(27)+'[39m')
sys.stdout.write(chr(27)+'[49m')
sys.stdout.write(chr(27)+'[22m')
sys.stdout.flush()


def main():
"""
Rainbow main program.
Expand Down Expand Up @@ -416,24 +426,31 @@ def main():
else:
os.close(pipe_write)
pipe_fd_handle = os.fdopen(pipe_read)
while True:
try:
line = pipe_fd_handle.readline()
if line:
print(apply_filters(line[:-1]))
else:
break
except KeyboardInterrupt:
pass
try:
while True:
try:
line = pipe_fd_handle.readline()
if line:
print(apply_filters(line[:-1]))
else:
break
except KeyboardInterrupt:
pass
finally:
reset_all()
os._exit(os.EX_OK)

else:
logger.info("No arguments given, using STDIN as input.")
# Support both Python 2 and 3.
input_ = raw_input if sys.version_info[0] < 3 else input
try:
while True:
print(apply_filters(input()))
print(apply_filters(input_()))
except (EOFError, KeyboardInterrupt):
return 0
finally:
reset_all()

# Raise an error for any other exception.
except Exception as exc:
Expand Down
7 changes: 5 additions & 2 deletions setup.py
@@ -1,6 +1,7 @@
#!/usr/bin/env python
import os, glob
from distutils.core import setup
import glob
import os

setup(
name = "rainbow",
Expand All @@ -24,12 +25,14 @@
"Intended Audience :: System Administrators",
"Natural Language :: English",
"Programming Language :: Python",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 3",
"License :: OSI Approved :: GNU General Public License (GPL)",
],
scripts=['rainbow'],
data_files=[
('/usr/share/rainbow/configs', glob.glob('configs/*')),
('/etc/bash_completion.d', ['completion/bash/rainbow']),
('/usr/share/zsh/site-functions', ['completion/zsh/_rainbow'])
]
],
)

0 comments on commit c00b69f

Please sign in to comment.