From a95e803268f298a4b7c7f9b64693b4f5507d9763 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20G=C3=B3mez?= Date: Sat, 14 Jan 2012 19:01:23 +0100 Subject: [PATCH] Add a mill progress indicator to `progress.py` I've created a progress indicator that outputs a "mill" and added it to the `progress.py` file. Very simple stuff but its more compact than the other progress bars and it can be useful when using long labels. --- AUTHORS | 1 + clint/textui/progress.py | 31 +++++++++++++++++++++++++++++++ examples/progressbar.py | 4 +++- 3 files changed, 35 insertions(+), 1 deletion(-) 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)