Console spinner bugfixes for linux terminal

This commit is contained in:
Sven Heidemann 2021-03-15 20:33:20 +01:00
parent 26ec121079
commit 121b9b84a0
3 changed files with 50 additions and 23 deletions

View File

@ -424,7 +424,14 @@ class Console:
cls.set_hold_back(True) cls.set_hold_back(True)
spinner = SpinnerThread(len(message), spinner_foreground_color, spinner_background_color) spinner = SpinnerThread(len(message), spinner_foreground_color, spinner_background_color)
spinner.start() spinner.start()
return_value = None
try:
return_value = call(*args, **kwargs) return_value = call(*args, **kwargs)
except KeyboardInterrupt:
spinner.exit()
exit()
spinner.stop_spinning() spinner.stop_spinning()
cls.set_hold_back(False) cls.set_hold_back(False)

View File

@ -25,6 +25,7 @@ class SpinnerThread(threading.Thread):
self._background_color = background_color self._background_color = background_color
self._is_spinning = True self._is_spinning = True
self._exit = False
@staticmethod @staticmethod
def _spinner(): def _spinner():
@ -57,13 +58,19 @@ class SpinnerThread(threading.Thread):
""" """
rows, columns = os.popen('stty size', 'r').read().split() rows, columns = os.popen('stty size', 'r').read().split()
end_msg = 'done' end_msg = 'done'
columns = int(columns) - self._msg_len - len(end_msg) end_msg_pos = int(columns) - self._msg_len - len(end_msg)
if columns > 0: if end_msg_pos > 0:
print(f'{"" : >{columns}}', end='') print(f'{"" : >{end_msg_pos}}', end='')
else: else:
print('', end='') print('', end='')
first = True
spinner = self._spinner() spinner = self._spinner()
while self._is_spinning: while self._is_spinning:
if first:
first = False
print(colored(f'{next(spinner): >{len(end_msg) - 1}}', *self._get_color_args()), end='')
else:
print(colored(f'{next(spinner): >{len(end_msg)}}', *self._get_color_args()), end='') print(colored(f'{next(spinner): >{len(end_msg)}}', *self._get_color_args()), end='')
time.sleep(0.1) time.sleep(0.1)
back = '' back = ''
@ -73,6 +80,7 @@ class SpinnerThread(threading.Thread):
print(back, end='') print(back, end='')
sys.stdout.flush() sys.stdout.flush()
if not self._exit:
print(colored(end_msg, *self._get_color_args()), end='') print(colored(end_msg, *self._get_color_args()), end='')
def stop_spinning(self): def stop_spinning(self):
@ -82,3 +90,12 @@ class SpinnerThread(threading.Thread):
""" """
self._is_spinning = False self._is_spinning = False
time.sleep(0.1) time.sleep(0.1)
def exit(self):
"""
Stops the spinner
:return:
"""
self._is_spinning = False
self._exit = True
time.sleep(0.1)

View File

@ -3,24 +3,27 @@ from cpl.console import Console, ForegroundColorEnum
def test_spinner(): def test_spinner():
time.sleep(3) time.sleep(2)
if __name__ == '__main__': if __name__ == '__main__':
Console.write_line('Hello World\n') Console.write_line('Hello World\n')
Console.spinner('Test:', test_spinner, spinner_foreground_color=ForegroundColorEnum.cyan, text_foreground_color='green') Console.spinner('Test:', test_spinner, spinner_foreground_color=ForegroundColorEnum.cyan,
opts = [ text_foreground_color='green')
'Option 1', # opts = [
'Option 2', # 'Option 1',
'Option 3', # 'Option 2',
'Option 4' # 'Option 3',
] # 'Option 4'
selected = Console.select( # ]
'>', # selected = Console.select(
'Select item:', # '>',
opts, # 'Select item:',
header_foreground_color=ForegroundColorEnum.blue, # opts,
option_foreground_color=ForegroundColorEnum.green, # header_foreground_color=ForegroundColorEnum.blue,
cursor_foreground_color=ForegroundColorEnum.red # option_foreground_color=ForegroundColorEnum.green,
) # cursor_foreground_color=ForegroundColorEnum.red
Console.write_line(f'You selected: {selected}\n') # )
# Console.write_line(f'You selected: {selected}')
Console.write_line()