~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to progress.py

  • Committer: abentley
  • Date: 2005-04-29 14:41:03 UTC
  • Revision ID: abentley@lappy-20050429144103-b0a1a78752650702
refactored out progress.py

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import sys
 
2
class Progress(object):
 
3
    def __init__(self, units, current, total=None):
 
4
        self.units = units
 
5
        self.current = current
 
6
        self.total = total
 
7
        self.percent = None
 
8
        if self.total is not None:
 
9
            self.percent = 100.0 * current / total
 
10
 
 
11
    def __str__(self):
 
12
        if self.total is not None:
 
13
            return "%i of %i %s %.1f%%" % (self.current, self.total, self.units,
 
14
                                         self.percent)
 
15
        else:
 
16
            return "%i %s" (self.current, self.units) 
 
17
 
 
18
 
 
19
def progress_bar(progress):
 
20
    fmt = " %i of %i %s (%.1f%%)"
 
21
    f = fmt % (progress.total, progress.total, progress.units, 100.0)
 
22
    max = len(f)
 
23
    cols = 77 - max
 
24
    markers = int (float(cols) * progress.current / progress.total)
 
25
    txt = fmt % (progress.current, progress.total, progress.units,
 
26
                 progress.percent)
 
27
    sys.stdout.write("\r[%s%s]%s" % ('='*markers, ' '*(cols-markers), txt))
 
28
 
 
29
def clear_progress_bar():
 
30
    sys.stdout.write('\r%s\r' % (' '*79))
 
31