Improved spinner

This commit is contained in:
Sven Heidemann 2021-03-10 20:34:00 +01:00
parent b377710931
commit ae4709b0d4
2 changed files with 17 additions and 7 deletions

View File

@ -265,7 +265,7 @@ class Console:
cls.write_line(message)
cls.set_hold_back(True)
spinner = SpinnerThread(spinner_foreground_color, spinner_background_color)
spinner = SpinnerThread(len(message), spinner_foreground_color, spinner_background_color)
spinner.start()
return_value = call(*args)
spinner.stop_spinning()

View File

@ -1,3 +1,4 @@
import os
import sys
import threading
import time
@ -10,13 +11,15 @@ from cpl.console.foreground_color import ForegroundColor
class SpinnerThread(threading.Thread):
def __init__(self, foreground_color: ForegroundColor, background_color: BackgroundColor):
def __init__(self, msg_len: int, foreground_color: ForegroundColor, background_color: BackgroundColor):
threading.Thread.__init__(self)
self._is_spinning = True
self._msg_len = msg_len
self._foreground_color = foreground_color
self._background_color = background_color
self._is_spinning = True
@staticmethod
def _spinner():
while True:
@ -34,15 +37,22 @@ class SpinnerThread(threading.Thread):
return color_args
def run(self) -> None:
print('\t', end='')
rows, columns = os.popen('stty size', 'r').read().split()
end_msg = 'done'
columns = int(columns) - self._msg_len - len(end_msg)
print(f'{"" : >{columns}}', end='')
spinner = self._spinner()
while self._is_spinning:
print(colored(next(spinner), *self._get_color_args()), end='')
print(colored(f'{next(spinner): >{len(end_msg)}}', *self._get_color_args()), end='')
time.sleep(0.1)
print('\b', end='')
back = ''
for i in range(0, len(end_msg)):
back += '\b'
print(back, end='')
sys.stdout.flush()
print(colored('done', *self._get_color_args()), end='')
print(colored(end_msg, *self._get_color_args()), end='')
def stop_spinning(self):
self._is_spinning = False