miniarchiver/progress_meter.py

37 lines
1.2 KiB
Python
Raw Permalink Normal View History

2017-07-04 23:21:38 +00:00
import sys
class ProgressBar:
def __init__(self, num_elements):
self.num_elements = num_elements
self.current_element = 0
def show(self, msg=''):
total_blocks = 10
blocks_done = ((self.current_element * total_blocks)
// self.num_elements)
blocks_to_go = total_blocks - blocks_done
2017-07-04 23:26:29 +00:00
print('\r' # Go to the start of the line
'\x1b[K' # Clear line
'\x1b[0m' # Restart the "style"
'\x1b[?7l' # Disable line wrapping
'|' # Put the first "|"
2017-07-04 23:21:38 +00:00
+ blocks_done * '' # Completed blocks
+ blocks_to_go * ' ' # Uncompleted blocks
+ '\x1b[7m|\x1b[0m' # End the bar
+ ' '
2017-07-04 23:26:29 +00:00
+ msg # Add message
+ '\x1b[?7h' # Enable line wrapping
+ '\r', # Go back to the start
2017-07-04 23:21:38 +00:00
end='')
def next_iter(self, msg=''):
self.current_element += 1
self.show(msg)
2021-12-17 08:58:19 +00:00
def clean(self):
print('\r' # Go to the start of the line
'\x1b[K' # Clear line
'\x1b[0m', # Restart the "style"
end='')