sh_cpl/src/cpl/console/spinner_thread.py

37 lines
799 B
Python
Raw Normal View History

2021-03-05 17:09:12 +01:00
import sys
2021-03-05 15:55:14 +01:00
import threading
import time
class SpinnerThread(threading.Thread):
2021-03-05 17:09:12 +01:00
def __init__(self):
2021-03-05 15:55:14 +01:00
threading.Thread.__init__(self)
self._is_spinning = True
@staticmethod
def _spinner():
while True:
for cursor in '|/-\\':
yield cursor
def run(self) -> None:
2021-03-05 17:09:12 +01:00
print('\t', end='')
2021-03-05 15:55:14 +01:00
spinner = self._spinner()
while self._is_spinning:
2021-03-05 17:09:12 +01:00
# self._console.write(next(spinner))
print(next(spinner), end='')
2021-03-05 15:55:14 +01:00
time.sleep(0.1)
2021-03-05 17:09:12 +01:00
# self._console.write('\b')
print('\b', end='')
2021-03-05 16:27:38 +01:00
2021-03-05 17:09:12 +01:00
sys.stdout.flush()
2021-03-05 15:55:14 +01:00
2021-03-05 17:09:12 +01:00
# self._console.write(' ')
print(' ', end='')
2021-03-05 15:55:14 +01:00
def stop_spinning(self):
self._is_spinning = False
time.sleep(0.1)