Skip to content
Myr

Merge pull request #487 from con-f-use/master

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

Show progressbar only if total execution time is noticable

authored by Dan Sully · committed by GitHub · parents 467fb8d, d2d80cc · Browse files · 3 files+34 added−22 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+34 added−22 removed

  • tests:+25 added−18 removed
  • other:+9 added−4 removed

3 files+34 added−22 removed

modified:click/_termui_impl.py+7 added-2 removed
88 unmodified lines
89 self.entered = False90 self.current_item = None91 self.is_hidden = not isatty(self.file)92 self._last_line = None93 self.short_limit = 0.59495 def __enter__(self):96 self.entered = True97 self.render_progress()
7 unmodified lines
105 raise RuntimeError('You need to use progress bars in a with block.')106 self.render_progress()107 return self.generator()108109 def is_fast(self):110 return time.time() - self.start <= self.short_limit111112 def render_finish(self):109 if self.is_hidden:113 if self.is_hidden or self.is_fast():114 return115 self.file.write(AFTER_BAR)116 self.file.flush()117
109 unmodified lines
227 buf.append(line)228 buf.append(' ' * (clear_width - line_len))229 line = ''.join(buf)230 # Render the line only if it changed.231227 if line != self._last_line:232 if line != self._last_line and not self.is_fast():233 self._last_line = line234 echo(line, file=self.file, color=self.color, nl=True)235 self.file.flush()236
330 unmodified lines
modified:tests/test_termui.py+25 added-18 removed
1import click2import time345class FakeClock(object):6 def __init__(self):7 self.now = time.time()89 def advance_time(self, seconds=1):10 self.now += seconds1112 def time(self):13 return self.now141516def test_progressbar_strip_regression(runner, monkeypatch):17 fake_clock = FakeClock()18 label = ' padded line'1920 @click.command()21 def cli():22 with click.progressbar(tuple(range(10)), label=label) as progress:23 for thing in progress:12 pass24 fake_clock.advance_time()2526 monkeypatch.setattr(time, 'time', fake_clock.time)27 monkeypatch.setattr(click._termui_impl, 'isatty', lambda _: True)28 assert label in runner.invoke(cli, []).output2930
15 unmodified lines
46 raise StopIteration4748 next = __next__4950 fake_clock = FakeClock()5152 @click.command()53 def cli():54 with click.progressbar(Hinted(10), label='test') as progress:55 for thing in progress:41 pass56 fake_clock.advance_time()5758 monkeypatch.setattr(time, 'time', fake_clock.time)59 monkeypatch.setattr(click._termui_impl, 'isatty', lambda _: True)60 result = runner.invoke(cli, [])61 assert result.exception is None626364def test_progressbar_hidden(runner, monkeypatch):65 fake_clock = FakeClock()66 label = 'whatever'6768 @click.command()69 def cli():70 with click.progressbar(tuple(range(10)), label=label) as progress:71 for thing in progress:55 pass72 fake_clock.advance_time()7374 monkeypatch.setattr(time, 'time', fake_clock.time)75 monkeypatch.setattr(click._termui_impl, 'isatty', lambda _: False)76 assert runner.invoke(cli, []).output == ''7778
29 unmodified lines
108 assert len(list(progress)) == 3109110111def test_progressbar_update(runner, monkeypatch):94 class FakeClock(object):95 def __init__(self):96 self.now = time.time()9798 def advance_time(self, seconds=1):99 self.now += seconds100101 def time(self):102 return self.now103112 fake_clock = FakeClock()113114 @click.command()115 def cli():
7 unmodified lines
123 output = runner.invoke(cli, []).output124125 lines = [line for line in output.split('\n') if '[' in line]126119 assert ' 0%' in lines[0]120 assert ' 25% 00:00:03' in lines[1]121 assert ' 50% 00:00:02' in lines[2]122 assert ' 75% 00:00:01' in lines[3]123 assert '100% ' in lines[4]127 assert ' 25% 00:00:03' in lines[0]128 assert ' 50% 00:00:02' in lines[1]129 assert ' 75% 00:00:01' in lines[2]130 assert '100% ' in lines[3]
modified:CHANGES+2 added-2 removed
21 unmodified lines
22- Usage errors now hint at the `--help` option.23- ``launch`` now works properly under Cygwin. See #650.24- `CliRunner.invoke` now may receive `args` as a string representing25 a Unix shell command. See #664.26- Fix bug that caused bashcompletion to give improper completions on 26- Fix bug that caused bashcompletion to give improper completions on27 chained commands. See #774.28- Add support for bright colors.29- 't' and 'f' are now converted to True and False. 29- 't' and 'f' are now converted to True and False.30- Fix bug that caused bashcompletion to give improper completions on31 chained commands when a required option/argument was being completed.32 See #790.33- Allow autocompletion function to determine whether or not to return
370 unmodified lines