blob: d1db2a1c51726de14d0d5c50f810336831265288 [file] [log] [blame]
Alex41485522019-04-12 17:26:18 -05001import sys
2
3
4class Progress(object):
Alexd9fd85e2019-05-16 16:58:24 -05005 _strsize = 0
6 _note_size = 0
7
Alex41485522019-04-12 17:26:18 -05008 def __init__(self, max_index, bar_size=21):
9 self.total = max_index
10 # bar size in symbols
11 self.bar_size = bar_size
12
13 def write_progress(self, index, note=''):
Alex3ebc5632019-04-18 16:47:18 -050014 # calc index and percent values
Alexd9fd85e2019-05-16 16:58:24 -050015 _suffix = ''
16 new_size = len(note)
17 if self._note_size > new_size:
18 _suffix = ' '*(self._note_size - new_size)
Alex41485522019-04-12 17:26:18 -050019 _percent = (100 * index) / self.total
20 _index = (self.bar_size * index) / self.total
21 # clear the line
22 sys.stdout.write('\r')
23 # print new progress
24 _format = "[{:"+str(self.bar_size-1)+"}] {}/{} ({}%) {}"
Alexd9fd85e2019-05-16 16:58:24 -050025 _progress_string = _format.format(
Alex41485522019-04-12 17:26:18 -050026 '='*_index,
27 index,
28 self.total,
29 _percent,
Alexd9fd85e2019-05-16 16:58:24 -050030 note + _suffix
31 )
32 sys.stdout.write(_progress_string)
33 # Save new note size and whole string size
34 self._strsize = len(_progress_string)
35 self._note_size = new_size
Alex41485522019-04-12 17:26:18 -050036 sys.stdout.flush()
Alex3ebc5632019-04-18 16:47:18 -050037
Alexd9fd85e2019-05-16 16:58:24 -050038 def clearline(self):
39 sys.stdout.write('\r')
40 sys.stdout.write(' '*self._strsize)
41 sys.stdout.write('\r')
42
43 def end(self):
44 self._note_size = 0
45 self._strsize = 0
Alex41485522019-04-12 17:26:18 -050046 sys.stdout.write('\n')