From 490d09a31415d3fd1b16f650188bfd8e701ae8e8 Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Mon, 19 Sep 2011 08:56:47 -0700 Subject: Support units in progress messages This allows our progress meter to be used for bytes transferred, by setting the units to KB or MB to let the user know the size. Change-Id: Ie8653d4a40d79439026c18bd51915845b2c5bba9 Signed-off-by: Shawn O. Pearce --- progress.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'progress.py') diff --git a/progress.py b/progress.py index 2ace701..d948654 100644 --- a/progress.py +++ b/progress.py @@ -21,13 +21,14 @@ from trace import IsTrace _NOT_TTY = not os.isatty(2) class Progress(object): - def __init__(self, title, total=0): + def __init__(self, title, total=0, units=''): self._title = title self._total = total self._done = 0 self._lastp = -1 self._start = time() self._show = False + self._units = units def update(self, inc=1): self._done += inc @@ -51,11 +52,11 @@ class Progress(object): if self._lastp != p: self._lastp = p - sys.stderr.write('\r%s: %3d%% (%d/%d) ' % ( + sys.stderr.write('\r%s: %3d%% (%d%s/%d%s) ' % ( self._title, p, - self._done, - self._total)) + self._done, self._units, + self._total, self._units)) sys.stderr.flush() def end(self): @@ -69,9 +70,9 @@ class Progress(object): sys.stderr.flush() else: p = (100 * self._done) / self._total - sys.stderr.write('\r%s: %3d%% (%d/%d), done. \n' % ( + sys.stderr.write('\r%s: %3d%% (%d%s/%d%s), done. \n' % ( self._title, p, - self._done, - self._total)) + self._done, self._units, + self._total, self._units)) sys.stderr.flush() -- cgit v1.2.3