Added support for manually advancing progressbars.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 6cd13a4 · Browse files · 5 files·+43 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+43 added−7 removed
- other:+43 added−7 removed
Provenance
- Commit
90d7ee9- Tree
c01dd31- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
6cd13a4
5 files changed+43 added−7 removed
modified:click/_termui_impl.py+7 added-4 removed
209 unmodified lines210 echo(line, file=self.file, nl=False, color=self.color)211 self.file.write(' ' * (clear_width - line_len))212 self.file.flush()213 214- def make_step(self):215- self.pos += 1214+ def make_step(self, n_steps):215+ self.pos += n_steps216 if self.length_known and self.pos >= self.length:217 self.finished = True218 219 if (time.time() - self.last_eta) < 1.0:3 unmodified lines223 self.avg = self.avg[-6:] + [-(self.start - time.time()) / (self.pos)]224 225 self.eta_known = self.length_known226 227+ def update(self, n_steps):228+ self.make_step(n_steps)229+ self.render_progress()230+231 def finish(self):232 self.eta_known = 0233 self.current_item = None234 self.finished = True8 unmodified lines243 self.finish()244 self.render_progress()245 raise StopIteration()246 else:243- self.make_step()244- self.render_progress()247+ self.update(1)248 return rv249 250 if not PY2:251 __next__ = next261 unmodified linesmodified:click/termui.py+13 added-3 removed
1 unmodified line2 import sys3 import struct4 5 from ._compat import raw_input, text_type, string_types, \6- colorama, isatty, strip_ansi, get_winterm_size, \7- DEFAULT_COLUMNS, WIN6+ isatty, strip_ansi, get_winterm_size, DEFAULT_COLUMNS, WIN7 from .utils import echo8 from .exceptions import Abort, UsageError9 from .types import convert_type10 219 unmodified lines230 with progressbar(items) as bar:231 for item in bar:232 do_something_with(item)233 234+ Alternatively, if no iterable is specified, one can manually update the235+ progress bar through the `update()` method instead of directly236+ iterating over the progress bar. The update method accepts the number237+ of steps to increment the bar with::238+239+ with progressbar(length=chunks.total_bytes) as bar:240+ for chunk in chunks:241+ process_chunk(chunk)242+ bar.update(chunks.bytes)243+244 .. versionadded:: 2.0245 246 .. versionadded:: 4.0238- Added the `color` parameter.247+ Added the `color` parameter. Added a `update` method to the248+ progressbar object.249 250 :param iterable: an iterable to iterate over. If not provided the length251 is required.252 :param length: the number of items to iterate over. By default the276 unmodified linesmodified:docs/utils.rst+11 added
364 unmodified lines365 label='Modifying user accounts',366 length=number_of_users) as bar:367 for user in bar:368 modify_the_user(user)369+370+Sometimes, one may need to iterate over an external iterator, and advance the371+progress bar iregularly. To do so, you need to speficy the length (and no372+iterable), and use the update method on the context return value instead of373+iterating directly over it::374+375+ with click.progressbar(length=total_size,376+ label='Unzipping archive') as bar:377+ for archive in zip_file:378+ archive.extract()379+ bar.update(archive.size)modified:examples/termui/termui.py+11 added
1 # coding: utf-82 import click3+import math4 import time5 import random6 7 try:67 unmodified lines75 fill_char=click.style('#', fg='magenta')) as bar:76 for item in bar:77 process_slowly(item)78 79+ # 'Non-linear progress bar'80+ steps = [math.exp( x * 1. / 20) - 1 for x in range(20)]81+ count = int(sum(steps))82+ with click.progressbar(length=count, show_percent=False,83+ label='Slowing progress bar',84+ fill_char=click.style(u'█', fg='green')) as bar:85+ for item in steps:86+ time.sleep(item)87+ bar.update(item)88 89+90 @cli.command()91 @click.argument('url')92 def open(url):93 """Opens a file or URL In the default application."""63 unmodified linesmodified:CHANGES+1 added
26 unmodified lines27 - Fix bug in ``clear()`` on Windows when colorama is installed.28 - Reject ``nargs=-1`` for options properly. Options cannot be variadic.29 - Fixed an issue with bash completion not working properly for commands with30 non ASCII characters or dashes.31+- Added a way to manually update the progressbar.32 33 Version 3.334 -----------35 176 unmodified lines