Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame^] | 1 | from time import sleep |
| 2 | import sys |
| 3 | |
| 4 | |
| 5 | class Progress(object): |
| 6 | def __init__(self, max_index, bar_size=21): |
| 7 | self.total = max_index |
| 8 | # bar size in symbols |
| 9 | self.bar_size = bar_size |
| 10 | |
| 11 | def write_progress(self, index, note=''): |
| 12 | #calc index and percent values |
| 13 | _percent = (100 * index) / self.total |
| 14 | _index = (self.bar_size * index) / self.total |
| 15 | # clear the line |
| 16 | sys.stdout.write('\r') |
| 17 | # print new progress |
| 18 | _format = "[{:"+str(self.bar_size-1)+"}] {}/{} ({}%) {}" |
| 19 | sys.stdout.write(_format.format( |
| 20 | '='*_index, |
| 21 | index, |
| 22 | self.total, |
| 23 | _percent, |
| 24 | note |
| 25 | )) |
| 26 | sys.stdout.flush() |
| 27 | |
| 28 | @staticmethod |
| 29 | def newline(): |
| 30 | sys.stdout.write('\n') |