diff --git a/AUTHORS b/AUTHORS index 8896c96..cdbc3d1 100644 --- a/AUTHORS +++ b/AUTHORS @@ -18,3 +18,4 @@ Patches and Suggestions - Miguel Araujo - takluyver - kracekumar +- Alejandro Gómez diff --git a/clint/textui/progress.py b/clint/textui/progress.py index 3a94d1b..d0f93c7 100644 --- a/clint/textui/progress.py +++ b/clint/textui/progress.py @@ -15,10 +15,12 @@ import sys STREAM = sys.stderr BAR_TEMPLATE = '%s[%s%s] %i/%i\r' +MILL_TEMPLATE = '%s %s %i/%i\r' DOTS_CHAR = '.' BAR_FILLED_CHAR = '#' BAR_EMPTY_CHAR = ' ' +MILL_CHARS = ['|', '/', '-', '\\'] def bar(it, label='', width=32, hide=False, empty_char=BAR_EMPTY_CHAR, filled_char=BAR_FILLED_CHAR): """Progress iterator. Wrap your iterables with it.""" @@ -65,3 +67,32 @@ def dots(it, label='', hide=False): STREAM.write('\n') STREAM.flush() + +def mill(it, label='', hide=False,): + """Progress iterator. Prints a mill while iterating over the items.""" + + def _mill_char(_i): + if _i == 100: + return ' ' + else: + return MILL_CHARS[_i % len(MILL_CHARS)] + + def _show(_i): + if not hide: + STREAM.write(MILL_TEMPLATE % ( + label, _mill_char(_i), _i, count)) + STREAM.flush() + + count = len(it) + + if count: + _show(0) + + for i, item in enumerate(it): + + yield item + _show(i+1) + + if not hide: + STREAM.write('\n') + STREAM.flush() diff --git a/examples/progressbar.py b/examples/progressbar.py index 7643d52..951552e 100755 --- a/examples/progressbar.py +++ b/examples/progressbar.py @@ -17,4 +17,6 @@ if __name__ == '__main__': for i in progress.dots(range(100)): sleep(random() * 0.2) - \ No newline at end of file + + for i in progress.mill(range(100)): + sleep(random() * 0.2)