~abentley/bzrtools/bzrtools.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import sys
class Progress(object):
    def __init__(self, units, current, total=None):
        self.units = units
        self.current = current
        self.total = total
        self.percent = None
        if self.total is not None:
            self.percent = 100.0 * current / total

    def __str__(self):
        if self.total is not None:
            return "%i of %i %s %.1f%%" % (self.current, self.total, self.units,
                                         self.percent)
        else:
            return "%i %s" (self.current, self.units) 


def progress_bar(progress):
    fmt = " %i of %i %s (%.1f%%)"
    f = fmt % (progress.total, progress.total, progress.units, 100.0)
    max = len(f)
    cols = 77 - max
    markers = int (float(cols) * progress.current / progress.total)
    txt = fmt % (progress.current, progress.total, progress.units,
                 progress.percent)
    sys.stdout.write("\r[%s%s]%s" % ('='*markers, ' '*(cols-markers), txt))

def clear_progress_bar():
    sys.stdout.write('\r%s\r' % (' '*79))