Skip to content
Myr

Added support for current item display to progressbar.

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 b24e004 · Browse files · 3 files+30 added−15 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+30 added−15 removed

  • other:+30 added−15 removed

3 files+30 added−15 removed

modified:click/_termui_impl.py+12 added-7 removed
48 unmodified lines
49class ProgressBar(object):5051 def __init__(self, iterable, length=None, fill_char='#', empty_char=' ',52 bar_template='%(bar)s', info_sep=' ', show_eta=True,53 show_percent=None, show_pos=None, label=None, file=None,54 width=30):53 show_percent=None, show_pos=False, item_show_func=None,54 label=None, file=None, width=30):55 self.fill_char = fill_char56 self.empty_char = empty_char57 self.bar_template = bar_template58 self.info_sep = info_sep59 self.show_eta = show_eta60 self.show_percent = show_percent61 self.show_pos = show_pos62 self.item_show_func = item_show_func63 self.label = label or ''64 if file is None:65 file = _default_text_stdout()66 self.file = file
14 unmodified lines
81 self.eta_known = False82 self.finished = False83 self.max_width = None84 self.entered = False85 self.current_item = None8687 try:88 self.is_hidden = not self.file.isatty()89 except Exception:
51 unmodified lines
141 return ('% 4d%%' % int(self.pct * 100))[1:]142143 def format_progress_line(self):144 show_percent = self.show_percent143 show_pos = self.show_pos145146 info_bits = []147 if self.length_known:148 bar_length = int(self.pct * self.width)149 bar = self.fill_char * bar_length150 bar += self.empty_char * (self.width - bar_length)151 if show_percent is None:151 show_percent = not show_pos152 show_percent = not self.show_pos153 else:154 if self.finished:155 bar = self.fill_char * self.width156 else:
1 unmodified line
158 if self.time_per_iteration != 0:159 bar[int((math.cos(self.pos * self.time_per_iteration)160 / 2.0 + 0.5) * self.width)] = self.fill_char161 bar = ''.join(bar)161 if show_pos is None:162 show_pos = True162164 if show_pos:163 if self.show_pos:164 info_bits.append(self.format_pos())165 if show_percent:166 info_bits.append(self.format_pct())167 if self.show_eta and self.eta_known and not self.finished:168 info_bits.append(self.format_eta())169 if self.item_show_func is not None:170 item_info = self.item_show_func(self.current_item)171 if item_info is not None:172 info_bits.append(item_info)173174 return (self.bar_template % {175 'label': self.label,176 'bar': bar,
33 unmodified lines
210 self.eta_known = self.length_known211212 def finish(self):213 self.eta_known = 0214 self.current_item = None215 self.finished = True216217 def next(self):218 if self.is_hidden:219 return next(self.iter)220 try:221 rv = next(self.iter)222 self.current_item = rv223 except StopIteration:224 self.finish()225 self.render_progress()226 raise
65 unmodified lines
modified:click/termui.py+12 added-7 removed
178 unmodified lines
179 return pager(text + '\n')180181182def progressbar(iterable=None, length=None, label=None, show_eta=True,183 show_percent=None, show_pos=None, fill_char='#',184 empty_char='-', bar_template='%(label)s [%(bar)s] %(info)s',183 show_percent=None, show_pos=False,184 item_show_func=None, fill_char='#', empty_char='-',185 bar_template='%(label)s [%(bar)s] %(info)s',186 info_sep=' ', width=36, file=None):187 """This function creates an iterable context manager that can be used188 to iterate over something while showing a progress bar. It will189 either iterate over the `iterable` or `length` items (that are counted
34 unmodified lines
224 :param show_percent: enables or disables the percentage display. The225 default is `True` if the iterable has a length or226 `False` if not.227 :param show_pos: enables or disables the absolute position display. The227 deafult is `False` if the iterable has a length or228 `True` if not.228 deafult is `False`.229 :param item_show_func: a function called with the current item which230 can return a string to show the current item231 next to the progress bar. Note that the current232 item can be `None`!233 :param fill_char: the character to use to show the filled part of the234 progress bar.235 :param empty_char: the character to use to show the non filled part of236 the progress bar.
8 unmodified lines
245 """246 from ._termui_impl import ProgressBar247 return ProgressBar(iterable=iterable, length=length, show_eta=show_eta,248 show_percent=show_percent, show_pos=show_pos,245 fill_char=fill_char, empty_char=empty_char,246 bar_template=bar_template, info_sep=info_sep,247 file=file, label=label, width=width)249 item_show_func=item_show_func, fill_char=fill_char,250 empty_char=empty_char, bar_template=bar_template,251 info_sep=info_sep, file=file, label=label,252 width=width)
modified:examples/termui/termui.py+6 added-1 removed
53 unmodified lines
54 fill_char=Fore.GREEN + '#' + Fore.RESET) as bar:55 for item in bar:56 process_slowly(item)5758 def show_item(item):59 if item is not None:60 return 'Item #%d' % item6162 with click.progressbar(filter(items), label='Committing transaction',59 fill_char=Fore.YELLOW + '#' + Fore.RESET) as bar:63 fill_char=Fore.YELLOW + '#' + Fore.RESET,64 item_show_func=show_item) as bar:65 for item in bar:66 process_slowly(item)6768 with click.progressbar(length=count, label='Counting',
5 unmodified lines