Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 95 lines (84 sloc) 2.325 kB
3d17406 @mythmon Initial Commit
authored
1 #!/usr/bin/env python
3fe6248 @mythmon Comment and clean up progress.py a bit.
authored
2 # progress.py
3d17406 @mythmon Initial Commit
authored
3
3fe6248 @mythmon Comment and clean up progress.py a bit.
authored
4 """A class for drawing a progress bar to the console in various styles."""
5
6 import os
7 import time
8 import sys
9 import math
3d17406 @mythmon Initial Commit
authored
10
11 class ProgressBar:
12
13 def __init__(self, maxValue, style="percent"):
14 self.style = style
15 self.current = 0
16 self.maxValue = maxValue
3fe6248 @mythmon Comment and clean up progress.py a bit.
authored
17 # Get the width of the current console
3d17406 @mythmon Initial Commit
authored
18 _,self.width = os.popen('stty size', 'r').read().split()
3fe6248 @mythmon Comment and clean up progress.py a bit.
authored
19 # It is returned as a string. Fix that.
3d17406 @mythmon Initial Commit
authored
20 self.width = int(self.width)
21 self.firstDraw = True
22 if style=="percent":
3fe6248 @mythmon Comment and clean up progress.py a bit.
authored
23 # [****----] 50%
24 # Leave space for the sides (2), the brackets(2), the percent (3)
25 # and the space between the brackets and percent (1).
26 self.width -= 8
3d17406 @mythmon Initial Commit
authored
27 if style=="plain":
3fe6248 @mythmon Comment and clean up progress.py a bit.
authored
28 # [******------]
29 # Leave space for the sides (2) and the brackets (2).
30 self.width -= 4
3d17406 @mythmon Initial Commit
authored
31 if style=="numbers":
3fe6248 @mythmon Comment and clean up progress.py a bit.
authored
32 # [***---] 25/50
33 # The biggest number is xxx/xxx, so numOfDigits*2+1 for the numbers
34 # then 2 more for spaces on either side of the bar, 2 for the
35 # brackets.
36 numDigits = math.ceil(math.log10(maxValue))
37 self.width -= int(numDigits * 2 + 5)
3d17406 @mythmon Initial Commit
authored
38
3fe6248 @mythmon Comment and clean up progress.py a bit.
authored
39 """Update the value of the progress bar, and by default redraw it."""
3d17406 @mythmon Initial Commit
authored
40 def update(self,value,redraw=True):
41 self.current = value
42 if redraw:
43 self.draw()
44
45 templates = {
46 'percent': ' [{0}{1}]{2:>3}% ',
3fe6248 @mythmon Comment and clean up progress.py a bit.
authored
47 'numbers': ' [{0}{1}]{3}/{4} ',
48 'plain' : ' [{0}{1}] '
49 }
3d17406 @mythmon Initial Commit
authored
50
3fe6248 @mythmon Comment and clean up progress.py a bit.
authored
51 """Draw the progress bar to the screen in the specifed style."""
3d17406 @mythmon Initial Commit
authored
52 def draw(self):
53 percent = self.current / (self.maxValue * 1.0)
54 if percent > 1:
55 percent = 1
56 stars = int(self.width * percent)
57 blanks = self.width - stars
3fe6248 @mythmon Comment and clean up progress.py a bit.
authored
58
59 bar = ProgressBar.templates[self.style].format('*'*stars, ' '*blanks,
60 int(percent*100), self.current, self.maxValue)
61
62 # Reset the cursor to the beginning of the line
3d17406 @mythmon Initial Commit
authored
63 sys.stdout.write("\r")
3fe6248 @mythmon Comment and clean up progress.py a bit.
authored
64 # Write over the old progress bar
65 sys.stdout.write(bar)
66 # Refresh the console
3d17406 @mythmon Initial Commit
authored
67 sys.stdout.flush()
68
3fe6248 @mythmon Comment and clean up progress.py a bit.
authored
69 """Some testing/demo code."""
3d17406 @mythmon Initial Commit
authored
70 if __name__ == "__main__":
3fe6248 @mythmon Comment and clean up progress.py a bit.
authored
71 bar = ProgressBar(50,style="plain")
72 n = 0
73 while n <= 50:
74 bar.update(n)
75 bar.draw()
76 n += 1
77 time.sleep(0.05)
78 print
79 bar = ProgressBar(50,style="percent")
80 n = 0
81 while n <= 50:
82 bar.update(n)
83 bar.draw()
84 n += 1
85 time.sleep(0.05)
86 print
87 bar = ProgressBar(50,style="numbers")
3d17406 @mythmon Initial Commit
authored
88 n = 0
3fe6248 @mythmon Comment and clean up progress.py a bit.
authored
89 while n <= 50:
3d17406 @mythmon Initial Commit
authored
90 bar.update(n)
91 bar.draw()
92 n += 1
3fe6248 @mythmon Comment and clean up progress.py a bit.
authored
93 time.sleep(0.05)
3d17406 @mythmon Initial Commit
authored
94 print
Something went wrong with that request. Please try again.