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