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)
spinner = SpinnerThread(len(message), spinner_foreground_color, spinner_background_color)
spinner.start()
return_value = call(*args, **kwargs)
return_value = None
try:
return_value = call(*args, **kwargs)
except KeyboardInterrupt:
spinner.exit()
exit()
spinner.stop_spinning()
cls.set_hold_back(False)

View File

@ -25,6 +25,7 @@ class SpinnerThread(threading.Thread):
self._background_color = background_color
self._is_spinning = True
self._exit = False
@staticmethod
def _spinner():
@ -57,14 +58,20 @@ class SpinnerThread(threading.Thread):
"""
rows, columns = os.popen('stty size', 'r').read().split()
end_msg = 'done'
columns = int(columns) - self._msg_len - len(end_msg)
if columns > 0:
print(f'{"" : >{columns}}', end='')
end_msg_pos = int(columns) - self._msg_len - len(end_msg)
if end_msg_pos > 0:
print(f'{"" : >{end_msg_pos}}', end='')
else:
print('', end='')
first = True
spinner = self._spinner()
while self._is_spinning:
print(colored(f'{next(spinner): >{len(end_msg)}}', *self._get_color_args()), end='')
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='')
time.sleep(0.1)
back = ''
for i in range(0, len(end_msg)):
@ -73,7 +80,8 @@ class SpinnerThread(threading.Thread):
print(back, end='')
sys.stdout.flush()
print(colored(end_msg, *self._get_color_args()), end='')
if not self._exit:
print(colored(end_msg, *self._get_color_args()), end='')
def stop_spinning(self):
"""
@ -82,3 +90,12 @@ class SpinnerThread(threading.Thread):
"""
self._is_spinning = False
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():
time.sleep(3)
time.sleep(2)
if __name__ == '__main__':
Console.write_line('Hello World\n')
Console.spinner('Test:', test_spinner, spinner_foreground_color=ForegroundColorEnum.cyan, text_foreground_color='green')
opts = [
'Option 1',
'Option 2',
'Option 3',
'Option 4'
]
selected = Console.select(
'>',
'Select item:',
opts,
header_foreground_color=ForegroundColorEnum.blue,
option_foreground_color=ForegroundColorEnum.green,
cursor_foreground_color=ForegroundColorEnum.red
)
Console.write_line(f'You selected: {selected}\n')
Console.spinner('Test:', test_spinner, spinner_foreground_color=ForegroundColorEnum.cyan,
text_foreground_color='green')
# opts = [
# 'Option 1',
# 'Option 2',
# 'Option 3',
# 'Option 4'
# ]
# selected = Console.select(
# '>',
# 'Select item:',
# opts,
# header_foreground_color=ForegroundColorEnum.blue,
# option_foreground_color=ForegroundColorEnum.green,
# cursor_foreground_color=ForegroundColorEnum.red
# )
# Console.write_line(f'You selected: {selected}')
Console.write_line()