Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix output in corner cases #41

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions click_spinner/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ def __init__(self, beep=False, disable=False, force=False, stream=sys.stdout):
self.stream = stream
self.stop_running = None
self.spin_thread = None
self.tty_output = self.stream.isatty() or self.force

def start(self):
if self.disable:
return
if self.stream.isatty() or self.force:
if self.tty_output:
self.stop_running = threading.Event()
self.spin_thread = threading.Thread(target=self.init_spin)
self.spin_thread.start()
Expand All @@ -43,8 +44,11 @@ def __exit__(self, exc_type, exc_val, exc_tb):
if self.disable:
return False
self.stop()
if self.beep:
self.stream.write('\7')
if self.tty_output:
if self.beep:
self.stream.write('\7')
self.stream.flush()
self.stream.write(' \b')
self.stream.flush()
return False

Expand All @@ -64,6 +68,8 @@ def spinner(beep=False, disable=False, force=False, stream=sys.stdout):
Hide spinner.
force : bool
Force creation of spinner even when stdout is redirected.
stream : IO
Stream to write the spinner to.

Example
-------
Expand Down
28 changes: 28 additions & 0 deletions tests/test_spinner.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,34 @@ def cli():
result = runner.invoke(cli, [])
assert result.exception is None

def test_spinner_redirect_with_beep():
@click.command()
def cli():
stdout_io = StringIO()
with click_spinner.Spinner(beep=True, stream=stdout_io):
time.sleep(1) # allow time for a few spins
stdout_str = stdout_io.getvalue()
assert len(stdout_str) == 0

runner = CliRunner()
result = runner.invoke(cli, [])
assert result.exception is None


def test_spinner_erase():
@click.command()
def cli():
stdout_io = StringIO()
stdout_io.isatty = lambda: True
with click_spinner.Spinner(stream=stdout_io):
time.sleep(1) # allow time for a few spins
stdout_str = stdout_io.getvalue()
assert stdout_str[-3:] == '\b \b'

runner = CliRunner()
result = runner.invoke(cli, [])
assert result.exception is None


def test_spinner_redirect_force():
@click.command()
Expand Down