Animated dots spinner that runs in a background thread.
| 335 | |
| 336 | |
| 337 | class _Spinner: |
| 338 | """Animated dots spinner that runs in a background thread.""" |
| 339 | |
| 340 | def __init__(self, label: str): |
| 341 | self._label = label |
| 342 | self._stop = threading.Event() |
| 343 | self._thread: threading.Thread | None = None |
| 344 | |
| 345 | def start(self) -> None: |
| 346 | sys.stdout.write(f" {self._label}") |
| 347 | sys.stdout.flush() |
| 348 | self._thread = threading.Thread(target=self._run, daemon=True) |
| 349 | self._thread.start() |
| 350 | |
| 351 | def _run(self) -> None: |
| 352 | while not self._stop.wait(timeout=1.0): |
| 353 | sys.stdout.write(".") |
| 354 | sys.stdout.flush() |
| 355 | |
| 356 | def stop(self, suffix: str = "") -> None: |
| 357 | self._stop.set() |
| 358 | if self._thread: |
| 359 | self._thread.join() |
| 360 | sys.stdout.write(f" {suffix}\n") |
| 361 | sys.stdout.flush() |
| 362 | |
| 363 | |
| 364 | def _format_usage(elapsed: float, usage) -> str: |