1
*** added file 'bzrlib/progress.py'
5
+# Copyright (C) 2005 Aaron Bentley
6
+# <aaron.bentley@utoronto.ca>
8
+# This program is free software; you can redistribute it and/or modify
9
+# it under the terms of the GNU General Public License as published by
10
+# the Free Software Foundation; either version 2 of the License, or
11
+# (at your option) any later version.
13
+# This program is distributed in the hope that it will be useful,
14
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+# GNU General Public License for more details.
18
+# You should have received a copy of the GNU General Public License
19
+# along with this program; if not, write to the Free Software
20
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
+class Progress(object):
25
+ def __init__(self, units, current, total=None):
27
+ self.current = current
30
+ if self.total is not None:
31
+ self.percent = 100.0 * current / total
34
+ if self.total is not None:
35
+ return "%i of %i %s %.1f%%" % (self.current, self.total, self.units,
38
+ return "%i %s" (self.current, self.units)
41
+def progress_bar(progress):
42
+ fmt = " %i of %i %s (%.1f%%)"
43
+ f = fmt % (progress.total, progress.total, progress.units, 100.0)
46
+ markers = int (float(cols) * progress.current / progress.total)
47
+ txt = fmt % (progress.current, progress.total, progress.units,
49
+ sys.stderr.write("\r[%s%s]%s" % ('='*markers, ' '*(cols-markers), txt))
51
+def clear_progress_bar():
52
+ sys.stderr.write('\r%s\r' % (' '*79))
54
+def spinner_str(progress, show_text=False):
56
+ Produces the string for a textual "spinner" progress indicator
57
+ :param progress: an object represinting current progress
58
+ :param show_text: If true, show progress text as well
59
+ :return: The spinner string
61
+ >>> spinner_str(Progress("baloons", 0))
63
+ >>> spinner_str(Progress("baloons", 5))
65
+ >>> spinner_str(Progress("baloons", 6), show_text=True)
68
+ positions = ('|', '/', '-', '\\')
69
+ text = positions[progress.current % 4]
71
+ text+=" %i %s" % (progress.current, progress.units)
74
+def spinner(progress, show_text=False, output=sys.stderr):
76
+ Update a spinner progress indicator on an output
77
+ :param progress: The progress to display
78
+ :param show_text: If true, show text as well as spinner
79
+ :param output: The output to write to
81
+ >>> spinner(Progress("baloons", 6), show_text=True, output=sys.stdout)
84
+ output.write('\r%s' % spinner_str(progress, show_text))
88
+ result = doctest.testmod()
91
+ print "All tests passed"
93
+ print "No tests to run"
94
+if __name__ == "__main__":