2 Progress bar (TTY version)
4 Written by Broytman, Jan 1997 - Sep 2003. Copyright (C) 1997-2003 PhiloSoft Design
13 Simple progress indicator - displays bar "graph" using standard tty
14 commands - space, backspace, etc. This method is compatible with
15 (almost) all UNIX consoles and DOS boxes.
21 This is a "bar" (width 10 chars) for 42%
23 Certainly, to use it nicely, do not write anything on screen
24 (to stdout or stderr), while using it (or use erase()/redraw() methods).
25 Erase or delete it after using.
28 left_c = '#' # Chars for "graphics"
30 space_c = ' ' # a space
31 back_c = chr(8) # a backspace
33 if os.name == 'dos' or os.name == 'nt' : # Use nice chars on DOS screen
38 def __init__(self, min, max, out = sys.stderr, width1 = 10,
39 width2 = 1+3+1): # 1 space + 3 chars for "100" + 1 for "%"
55 def display(self, current):
57 Draw current value on indicator.
58 Optimized to draw as little as possible.
61 self.current = current
63 lng = (current * self.width1) / self.max
65 if current >= self.max:
68 percent = (current * 100) / self.max
74 self.out.write(ttyProgressBar.back_c*(self.width1+self.width2))
75 self.out.write(ttyProgressBar.left_c*lng)
76 self.out.write(ttyProgressBar.right_c*(self.width1-lng) + ttyProgressBar.space_c)
77 self.percent = -1 # force to redraw percentage; the bug was fixed by William McVey
80 elif self.percent <> percent:
81 self.out.write(ttyProgressBar.back_c * (self.width2-1))
85 if self.percent <> percent:
86 self.percent = percent
87 self.out.write(str(percent).rjust(3) + '%')
98 if self.visible: # Prevent erase() to be called twice - explicitly and from __del__()
99 self.out.write(ttyProgressBar.back_c*(self.width1+self.width2))
100 self.out.write(ttyProgressBar.space_c*(self.width1+self.width2))
101 self.out.write(ttyProgressBar.back_c*(self.width1+self.width2))
107 self.lng = -1 # force to draw bar on the first call
110 # Clear space - just in case there was some cruft left
111 self.out.write(ttyProgressBar.space_c*(self.width1+self.width2))
113 self.display(self.current)