Commits

Anonymous committed fb29a5b

Trying gb using a coroutine.

Comments (0)

Files changed (1)

  • +29 -11
    gb
 
 import sys
 
-try:
-   pat = sys.argv[1]
-except IndexError:
-   pat = None
+def coroutine(func):
+    def start(*args, **kwargs):
+        cr = func(*args, **kwargs)
+        cr.next()
+        return cr
+    return start
 
-green = True
-for line in iter(sys.stdin.readline, ""):
-    if (pat and pat in line) or (not pat and green):
-        print "\x1b[7m%s\x1b[0m" % line.rstrip()
-    else:
-        print line,
-    green = not green
+@coroutine
+def greenbar(pat=None):
+    green = True
+    try:
+        while True:
+            line = (yield)
+            if (pat and pat in line) or (not pat and green):
+                print "\x1b[7m%s\x1b[0m" % line
+            else:
+                print line,
+            green = not green
+    except GeneratorExit:
+        pass
+
+if __name__ == '__main__':
+    try:
+       pat = sys.argv[1]
+    except IndexError:
+       pat = None
+
+    gb = greenbar(pat)
+    for line in iter(sys.stdin.readline, ""):
+        gb.send(line[:-1])