Skip to content

Commit

Permalink
Merge pull request #863 from georgeyk/progressbar-to-stderr
Browse files Browse the repository at this point in the history
ProgressBar should use stderr by default
  • Loading branch information
Dan Sully authored May 14, 2018
2 parents 19d5273 + 2a49ba9 commit da4a508
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 31 deletions.
59 changes: 28 additions & 31 deletions click/_termui_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,43 +192,40 @@ def format_progress_line(self):

def render_progress(self):
from .termui import get_terminal_size
nl = False

if self.is_hidden:
buf = [self.label]
nl = True
else:
buf = []
# Update width in case the terminal has been resized
if self.autowidth:
old_width = self.width
self.width = 0
clutter_length = term_len(self.format_progress_line())
new_width = max(0, get_terminal_size()[0] - clutter_length)
if new_width < old_width:
buf.append(BEFORE_BAR)
buf.append(' ' * self.max_width)
self.max_width = new_width
self.width = new_width

clear_width = self.width
if self.max_width is not None:
clear_width = self.max_width

buf.append(BEFORE_BAR)
line = self.format_progress_line()
line_len = term_len(line)
if self.max_width is None or self.max_width < line_len:
self.max_width = line_len
buf.append(line)

buf.append(' ' * (clear_width - line_len))
line = ''.join(buf)
return

buf = []
# Update width in case the terminal has been resized
if self.autowidth:
old_width = self.width
self.width = 0
clutter_length = term_len(self.format_progress_line())
new_width = max(0, get_terminal_size()[0] - clutter_length)
if new_width < old_width:
buf.append(BEFORE_BAR)
buf.append(' ' * self.max_width)
self.max_width = new_width
self.width = new_width

clear_width = self.width
if self.max_width is not None:
clear_width = self.max_width

buf.append(BEFORE_BAR)
line = self.format_progress_line()
line_len = term_len(line)
if self.max_width is None or self.max_width < line_len:
self.max_width = line_len

buf.append(line)
buf.append(' ' * (clear_width - line_len))
line = ''.join(buf)
# Render the line only if it changed.
if line != self._last_line:
self._last_line = line
echo(line, file=self.file, color=self.color, nl=nl)
echo(line, file=self.file, color=self.color, nl=True)
self.file.flush()

def make_step(self, n_steps):
Expand Down
13 changes: 13 additions & 0 deletions tests/test_termui.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ def cli():
assert result.exception is None


def test_progressbar_hidden(runner, monkeypatch):
label = 'whatever'

@click.command()
def cli():
with click.progressbar(tuple(range(10)), label=label) as progress:
for thing in progress:
pass

monkeypatch.setattr(click._termui_impl, 'isatty', lambda _: False)
assert runner.invoke(cli, []).output == ''


def test_choices_list_in_prompt(runner, monkeypatch):
@click.command()
@click.option('-g', type=click.Choice(['none', 'day', 'week', 'month']),
Expand Down

0 comments on commit da4a508

Please sign in to comment.