Skip to content
Myr

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

5 files+43 added−7 removed

modified:click/_termui_impl.py+7 added-4 removed
209 unmodified lines
210 echo(line, file=self.file, nl=False, color=self.color)211 self.file.write(' ' * (clear_width - line_len))212 self.file.flush()213214 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 = True218219 if (time.time() - self.last_eta) < 1.0:
3 unmodified lines
223 self.avg = self.avg[-6:] + [-(self.start - time.time()) / (self.pos)]224225 self.eta_known = self.length_known226227 def update(self, n_steps):228 self.make_step(n_steps)229 self.render_progress()230231 def finish(self):232 self.eta_known = 0233 self.current_item = None234 self.finished = True
8 unmodified lines
243 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 rv249250 if not PY2:251 __next__ = next
261 unmodified lines
modified:click/termui.py+13 added-3 removed
1 unmodified line
2import sys3import struct45from ._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, WIN7from .utils import echo8from .exceptions import Abort, UsageError9from .types import convert_type10
219 unmodified lines
230 with progressbar(items) as bar:231 for item in bar:232 do_something_with(item)233234 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::238239 with progressbar(length=chunks.total_bytes) as bar:240 for chunk in chunks:241 process_chunk(chunk)242 bar.update(chunks.bytes)243244 .. versionadded:: 2.0245246 .. versionadded:: 4.0238 Added the `color` parameter.247 Added the `color` parameter. Added a `update` method to the248 progressbar object.249250 :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 the
276 unmodified lines
modified:docs/utils.rst+11 added
364 unmodified lines
365 label='Modifying user accounts',366 length=number_of_users) as bar:367 for user in bar:368 modify_the_user(user)369370Sometimes, one may need to iterate over an external iterator, and advance the371progress bar iregularly. To do so, you need to speficy the length (and no372iterable), and use the update method on the context return value instead of373iterating directly over it::374375 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-82import click3import math4import time5import random67try:
67 unmodified lines
75 fill_char=click.style('#', fg='magenta')) as bar:76 for item in bar:77 process_slowly(item)7879 # '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)888990@cli.command()91@click.argument('url')92def open(url):93 """Opens a file or URL In the default application."""
63 unmodified lines
modified:CHANGES+1 added
26 unmodified lines
27- 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.3233Version 3.334-----------35
176 unmodified lines