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
Provenance
- Commit
cb29f88- Tree
0308dd6- Author
Dan Sully <daniel-github@electricrain.com>- Committer
GitHub <noreply@github.com>- Parent 1
467fb8d- Parent 2
d2d80cc
3 files changed+34 added−22 removed
modified:click/_termui_impl.py+7 added-2 removed
88 unmodified lines89 self.entered = False90 self.current_item = None91 self.is_hidden = not isatty(self.file)92 self._last_line = None93+ self.short_limit = 0.594 95 def __enter__(self):96 self.entered = True97 self.render_progress()7 unmodified lines105 raise RuntimeError('You need to use progress bars in a with block.')106 self.render_progress()107 return self.generator()108 109+ def is_fast(self):110+ return time.time() - self.start <= self.short_limit111+112 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 lines227 buf.append(line)228 buf.append(' ' * (clear_width - line_len))229 line = ''.join(buf)230 # Render the line only if it changed.231+227- 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 linesmodified:tests/test_termui.py+25 added-18 removed
1 import click2 import time3 4 5+class FakeClock(object):6+ def __init__(self):7+ self.now = time.time()8+9+ def advance_time(self, seconds=1):10+ self.now += seconds11+12+ def time(self):13+ return self.now14+15+16 def test_progressbar_strip_regression(runner, monkeypatch):17+ fake_clock = FakeClock()18 label = ' padded line'19 20 @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()25 26+ monkeypatch.setattr(time, 'time', fake_clock.time)27 monkeypatch.setattr(click._termui_impl, 'isatty', lambda _: True)28 assert label in runner.invoke(cli, []).output29 30 15 unmodified lines46 raise StopIteration47 48 next = __next__49 50+ fake_clock = FakeClock()51+52 @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()57 58+ 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 None62 63 64 def test_progressbar_hidden(runner, monkeypatch):65+ fake_clock = FakeClock()66 label = 'whatever'67 68 @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()73 74+ monkeypatch.setattr(time, 'time', fake_clock.time)75 monkeypatch.setattr(click._termui_impl, 'isatty', lambda _: False)76 assert runner.invoke(cli, []).output == ''77 78 29 unmodified lines108 assert len(list(progress)) == 3109 110 111 def test_progressbar_update(runner, monkeypatch):94- class FakeClock(object):95- def __init__(self):96- self.now = time.time()97-98- def advance_time(self, seconds=1):99- self.now += seconds100-101- def time(self):102- return self.now103-112 fake_clock = FakeClock()113 114 @click.command()115 def cli():7 unmodified lines123 output = runner.invoke(cli, []).output124 125 lines = [line for line in output.split('\n') if '[' in line]126 119- 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 lines22 - 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 return370 unmodified lines