1 """Progress bar (TTY version)"""
9 Simple progress indicator - displays bar "graph" using standard tty
10 commands - space, backspace, etc. This method is compatible with
11 (almost) all UNIX consoles and DOS boxes.
17 This is a "bar" (width 10 chars) for 42%
19 Certainly, to use it nicely, do not write anything on screen
20 (to stdout or stderr), while using it (or use erase()/redraw() methods).
21 Erase or delete it after using.
24 left_c = '#' # Chars for "graphics"
26 space_c = ' ' # a space
27 back_c = chr(8) # a backspace
29 if os.name == 'dos' or os.name == 'nt' : # Use nice chars on DOS screen
34 def __init__(self, min, max, out = sys.stderr, width1 = 10,
35 width2 = 1+3+1): # 1 space + 3 chars for "100" + 1 for "%"
51 def display(self, current):
53 Draw current value on indicator.
54 Optimized to draw as little as possible.
57 self.current = current
59 lng = (current * self.width1) // self.max
61 if current >= self.max:
64 percent = (current * 100) // self.max
70 self.out.write(ttyProgressBar.back_c*(self.width1+self.width2))
71 self.out.write(ttyProgressBar.left_c*lng)
72 self.out.write(ttyProgressBar.right_c*(self.width1-lng) + ttyProgressBar.space_c)
73 self.percent = -1 # force to redraw percentage; the bug was fixed by William McVey
76 elif self.percent != percent:
77 self.out.write(ttyProgressBar.back_c * (self.width2-1))
81 if self.percent != percent:
82 self.percent = percent
83 self.out.write(str(percent).rjust(3) + '%')
94 if self.visible: # Prevent erase() to be called twice - explicitly and from __del__()
95 self.out.write(ttyProgressBar.back_c*(self.width1+self.width2))
96 self.out.write(ttyProgressBar.space_c*(self.width1+self.width2))
97 self.out.write(ttyProgressBar.back_c*(self.width1+self.width2))
103 self.lng = -1 # force to draw bar on the first call
106 # Clear space - just in case there was some cruft left
107 self.out.write(ttyProgressBar.space_c*(self.width1+self.width2))
109 self.display(self.current)