501
501
+# arch-tag: d1541a25-eac5-4de9-a476-08a7cecd5683
503
*** added file 'bzrlib/progress.py'
505
+++ bzrlib/progress.py
507
+# Copyright (C) 2005 Aaron Bentley
508
+# <aaron.bentley@utoronto.ca>
510
+# This program is free software; you can redistribute it and/or modify
511
+# it under the terms of the GNU General Public License as published by
512
+# the Free Software Foundation; either version 2 of the License, or
513
+# (at your option) any later version.
515
+# This program is distributed in the hope that it will be useful,
516
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
517
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
518
+# GNU General Public License for more details.
520
+# You should have received a copy of the GNU General Public License
521
+# along with this program; if not, write to the Free Software
522
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
526
+class Progress(object):
527
+ def __init__(self, units, current, total=None):
529
+ self.current = current
531
+ self.percent = None
532
+ if self.total is not None:
533
+ self.percent = 100.0 * current / total
536
+ if self.total is not None:
537
+ return "%i of %i %s %.1f%%" % (self.current, self.total, self.units,
540
+ return "%i %s" (self.current, self.units)
543
+def progress_bar(progress):
544
+ fmt = " %i of %i %s (%.1f%%)"
545
+ f = fmt % (progress.total, progress.total, progress.units, 100.0)
548
+ markers = int (float(cols) * progress.current / progress.total)
549
+ txt = fmt % (progress.current, progress.total, progress.units,
551
+ sys.stderr.write("\r[%s%s]%s" % ('='*markers, ' '*(cols-markers), txt))
553
+def clear_progress_bar():
554
+ sys.stderr.write('\r%s\r' % (' '*79))
556
+def spinner_str(progress, show_text=False):
558
+ Produces the string for a textual "spinner" progress indicator
559
+ :param progress: an object represinting current progress
560
+ :param show_text: If true, show progress text as well
561
+ :return: The spinner string
563
+ >>> spinner_str(Progress("baloons", 0))
565
+ >>> spinner_str(Progress("baloons", 5))
567
+ >>> spinner_str(Progress("baloons", 6), show_text=True)
570
+ positions = ('|', '/', '-', '\\')
571
+ text = positions[progress.current % 4]
573
+ text+=" %i %s" % (progress.current, progress.units)
576
+def spinner(progress, show_text=False, output=sys.stderr):
578
+ Update a spinner progress indicator on an output
579
+ :param progress: The progress to display
580
+ :param show_text: If true, show text as well as spinner
581
+ :param output: The output to write to
583
+ >>> spinner(Progress("baloons", 6), show_text=True, output=sys.stdout)
586
+ output.write('\r%s' % spinner_str(progress, show_text))
590
+ result = doctest.testmod()
593
+ print "All tests passed"
595
+ print "No tests to run"
596
+if __name__ == "__main__":
599
503
*** added directory 'testdata'
600
504
*** added file 'testdata/diff'