2021.4 #19

Merged
edraft merged 237 commits from 2021.4 into master 2021-04-01 10:13:33 +02:00
3 changed files with 50 additions and 23 deletions
Showing only changes of commit 121b9b84a0 - Show all commits

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 = 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,13 +58,19 @@ 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:
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 = ''
@ -73,6 +80,7 @@ class SpinnerThread(threading.Thread):
print(back, end='')
sys.stdout.flush()
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()