diff --git a/usr/lib/python3.8/spoc/cli.py b/usr/lib/python3.8/spoc/cli.py index 0400f8c..cddf642 100644 --- a/usr/lib/python3.8/spoc/cli.py +++ b/usr/lib/python3.8/spoc/cli.py @@ -7,6 +7,33 @@ from math import floor SIZE_PREFIXES = ('', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y') +class ActionItem: + def __init__(self, text, action, show_progress=True): + self.text = text + self.action = action + self.show_progress = show_progress + self.units_total = 1 + self.units_done = 0 + + def run(self): + if self.show_progress: + with ThreadPoolExecutor() as executor: + future = executor.submit(self.action, self) + while not future.done(): + time.sleep(0.2) + self.print_progress() + # Get the result of the future and let it raise exception, if there was any + data = future.result() + self.print_progress('\n') + else: + self.print_progress() + self.action() + self.units_done = 1 + self.print_progress('\n') + + def print_progress(self, end='\r'): + print(f'\x1b[K{self.text} ({self.units_done}/{self.units_total}) [{floor(self.units_done/self.units_total*100)} %]', end=end) + class ActionQueue: def __init__(self): self.queue = [] @@ -39,33 +66,6 @@ class ActionQueue: item.text = f'[{index}/{queue_length}] {item.text}' item.run() -class ActionItem: - def __init__(self, text, action, has_progress=True): - self.text = text - self.action = action - self.has_progress = has_progress - self.units_total = 1 - self.units_done = 0 - - def run(self): - if self.has_progress: - with ThreadPoolExecutor() as executor: - future = executor.submit(self.action, self) - while not future.done(): - time.sleep(0.2) - self.print_progress() - # Get the result of the future and let it raise exception, if there was any - data = future.result() - self.print_progress('\n') - else: - self.print_progress() - self.action() - self.units_done = 1 - self.print_progress('\n') - - def print_progress(self, end='\r'): - print(f'\x1b[K{self.text} ({self.units_done}/{self.units_total}) [{floor(self.units_done/self.units_total*100)} %]', end=end) - def readable_size(bytes): i = 0 while bytes > 1024: