-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspinner.py
More file actions
38 lines (32 loc) · 998 Bytes
/
spinner.py
File metadata and controls
38 lines (32 loc) · 998 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import itertools
import threading
import time
import click
class Spinner:
def __init__(self, text="Processing..."):
self.spinner = itertools.cycle(["|", "/", "-", "\\"])
self.text = click.style(text, fg="yellow")
self.stop_running = False
def start(self):
"""
Start the spinner in a separate thread.
"""
self.stop_running = False
threading.Thread(target=self._spin, daemon=True).start()
def _spin(self):
"""
Spin until stopped.
"""
while not self.stop_running:
print(
f"\r{self.text} {click.style(next(self.spinner), fg='yellow')}", end="", flush=True
)
time.sleep(0.1)
print("\r", end="", flush=True)
def stop(self):
"""
Stop the spinner and clear the line.
"""
self.stop_running = True
time.sleep(0.1)
print("\r" + " " * (len(self.text) + 1) + "\r", end="", flush=True)