~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to progress.py

  • Committer: Aaron Bentley
  • Date: 2007-06-11 05:08:34 UTC
  • Revision ID: aaron.bentley@utoronto.ca-20070611050834-wcbta2pfitcuopku
fix long-line detection

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
17
 
18
18
import sys
 
19
import datetime
 
20
from bzrlib.progress import ProgressBar
19
21
 
20
22
class Progress(object):
21
23
    def __init__(self, units, current, total=None):
22
24
        self.units = units
23
25
        self.current = current
24
26
        self.total = total
25
 
        self.percent = None
26
 
        if self.total is not None:
27
 
            self.percent = 100.0 * current / total
 
27
 
 
28
    def _get_percent(self):
 
29
        if self.total is None:
 
30
            return None
 
31
        return 100.0 * self.current / self.total
 
32
 
 
33
    percent = property(_get_percent)
28
34
 
29
35
    def __str__(self):
30
36
        if self.total is not None:
31
37
            return "%i of %i %s %.1f%%" % (self.current, self.total, self.units,
32
38
                                         self.percent)
33
39
        else:
34
 
            return "%i %s" (self.current, self.units) 
35
 
 
36
 
 
37
 
def progress_bar(progress):
38
 
    fmt = " %i of %i %s (%.1f%%)"
39
 
    f = fmt % (progress.total, progress.total, progress.units, 100.0)
40
 
    max = len(f)
41
 
    cols = 77 - max
42
 
    markers = int (float(cols) * progress.current / progress.total)
43
 
    txt = fmt % (progress.current, progress.total, progress.units,
44
 
                 progress.percent)
45
 
    sys.stdout.write("\r[%s%s]%s" % ('='*markers, ' '*(cols-markers), txt))
 
40
            return "%i %s" (self.current, self.units)
 
41
 
 
42
def show_progress(pi, prog):
 
43
    pi.update(prog.units, prog.current, prog.total)
46
44
 
47
45
def clear_progress_bar():
48
 
    sys.stdout.write('\r%s\r' % (' '*79))
49
 
 
 
46
    sys.stderr.write('\r%s\r' % (' '*79))