Coverage for cfg_checker/helpers/console_utils.py : 27%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import sys
4class Progress(object):
5 _strsize = 0
6 _note_size = 0
8 def __init__(self, max_index, bar_size=21):
9 self.total = max_index
10 # bar size in symbols
11 self.bar_size = bar_size
13 def write_progress(self, index, note=''):
14 # calc index and percent values
15 _suffix = ''
16 new_size = len(note)
17 if self._note_size > new_size:
18 _suffix = ' '*(self._note_size - new_size)
19 _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)+"}] {}/{} ({}%) {}"
25 _progress_string = _format.format(
26 '='*_index,
27 index,
28 self.total,
29 _percent,
30 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
36 sys.stdout.flush()
38 def clearline(self):
39 sys.stdout.write('\r')
40 sys.stdout.write(' '*self._strsize)
41 sys.stdout.write('\r')
42 sys.stdout.flush()
44 def end(self):
45 self._note_size = 0
46 self._strsize = 0
47 sys.stdout.write('\n')