From e583e54bf31d5a4764e90f75c0b72dac9d25b2fc Mon Sep 17 00:00:00 2001 From: nojhan Date: Fri, 22 Mar 2013 22:52:53 +0100 Subject: [PATCH] Read stdin as a stream, avoid blocking read/write --- colout.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/colout.py b/colout.py index e542920..ae3c8a7 100755 --- a/colout.py +++ b/colout.py @@ -193,7 +193,7 @@ def colorup( text, pattern, color = "red", style = "normal", on_groups=False): return colored_text -def colorgen( items, pattern, color = "red", style = "normal", on_groups=False): +def colorgen( stream, pattern, color = "red", style = "normal", on_groups=False): """ A generator that colors the items given in an iterable input. @@ -202,7 +202,13 @@ def colorgen( items, pattern, color = "red", style = "normal", on_groups=False): ['3.\x1b[0;31m1\x1b[0m4\x1b[0;31m1\x1b[0m59265359', '2.7\x1b[0;31m1\x1b[0m828\x1b[0;31m1\x1b[0m82846'] """ - for item in items: + while True: + try: + item = stream.readline() + except KeyboardInterrupt: + break + if not item: + break yield colorup( item, pattern, color, style, on_groups) @@ -339,7 +345,13 @@ if __name__ == "__main__": themes[name] = __import__(module) if pattern in themes.keys(): - for item in sys.stdin: + while True: + try: + item = sys.stdin.readline() + except KeyboardInterrupt: + break + if not item: + break colored = themes[pattern].theme(item) write(colored) else: