Skip to content

Commit

Permalink
Use nice spinner function from Rich instead of homebrew code.
Browse files Browse the repository at this point in the history
  • Loading branch information
ewels committed Jan 14, 2021
1 parent bb5c10d commit 9e4599a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 24 deletions.
30 changes: 7 additions & 23 deletions nf_core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import sys
import time
import yaml
from rich.live import Live
from rich.spinner import Spinner

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -269,30 +271,12 @@ def wait_cli_function(poll_func, poll_every=20):
None. Just sits in an infite loop until the function returns True.
"""
try:
is_finished = False
check_count = 0

def spinning_cursor():
spinner = Spinner("dots2", "Use ctrl+c to stop waiting and force exit.")
with Live(spinner, refresh_per_second=20) as live:
while True:
for cursor in "⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏":
yield "{} Use ctrl+c to stop waiting and force exit. ".format(cursor)

spinner = spinning_cursor()
while not is_finished:
# Write a new loading text
loading_text = next(spinner)
sys.stdout.write(loading_text)
sys.stdout.flush()
# Show the loading spinner every 0.1s
time.sleep(0.1)
# Wipe the previous loading text
sys.stdout.write("\b" * len(loading_text))
sys.stdout.flush()
# Only check every 2 seconds, but update the spinner every 0.1s
check_count += 1
if check_count > poll_every:
is_finished = poll_func()
check_count = 0
if poll_func():
break
time.sleep(2)
except KeyboardInterrupt:
raise AssertionError("Cancelled!")

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"pyyaml",
"requests",
"requests_cache",
"rich>=9",
"rich>=9.4",
"tabulate",
],
setup_requires=["twine>=1.11.0", "setuptools>=38.6."],
Expand Down

0 comments on commit 9e4599a

Please sign in to comment.