[PATCH 1 of 3] Add a simple progress indicator

Thomas Arendsen Hein thomas at intevation.de
Mon Jul 23 02:49:04 CDT 2007


# HG changeset patch
# User Thomas Arendsen Hein <thomas at intevation.de>
# Date 1185175619 -7200
# Node ID 01248968b792bd4955d4c133b771746b08f65f40
# Parent  b43db44cd0475291b6ffbf89b6e4dc8e410daeab
Add a simple progress indicator.

diff -r b43db44cd047 -r 01248968b792 mercurial/progress.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mercurial/progress.py	Mon Jul 23 09:26:59 2007 +0200
@@ -0,0 +1,78 @@
+# progress.py - Simple progress indicator for Mercurial
+#
+# Copyright 2007 by Intevation GmbH <intevation at intevation.de>
+# Author(s):
+# Thomas Arendsen Hein <thomas at intevation.de>
+#
+# This software may be used and distributed according to the terms
+# of the GNU General Public License, incorporated herein by reference.
+
+class noprogress(object):
+
+    """Dummy progress indicator"""
+
+    def __init__(self, msg, maxval, fd):
+        """Display progress indicator.
+
+        msg is a short string describing the task
+        maxval is the number of steps for this task
+        fd is the file-like output object, usually sys.stderr
+        """
+        pass
+
+    def update(self, value=None):
+        """Update progress indicator if needed.
+
+        value defaults to the current value + 1
+        """
+        pass
+
+    def finish(self):
+        """Finish displaying the progress indicator.
+
+        Provides a new line for following output.
+        """
+        pass
+
+    def interrupt(self):
+        """Interrupt the progress indicator.
+
+        Provides a clear line for following output.
+        """
+        pass
+
+
+class progress(noprogress):
+
+    """Simple progress indicator"""
+
+    def __init__(self, msg, maxval, fd):
+        self._msg = msg
+        self._maxval = maxval
+        self._fd = fd
+        self._value = 0
+        self._part = 0
+        self._clean = True
+        self.update(0)
+
+    def update(self, value=None):
+        if value is None:
+            value = self._value + 1
+        self._value = value
+        part = value * 10 / self._maxval
+        while part > self._part:
+            if self._clean:
+                self._clean = False
+                self._part = 0
+                self._fd.write('%s 0%%' % self._msg)
+            self._part += 1
+            self._fd.write('..%d%%' % (self._part*10))
+
+    def finish(self):
+        self.interrupt()
+
+    def interrupt(self):
+        if not self._clean:
+            self._clean = True
+            self._fd.write('\n')
+
diff -r b43db44cd047 -r 01248968b792 mercurial/ui.py
--- a/mercurial/ui.py	Sun Jul 22 16:21:49 2007 +0200
+++ b/mercurial/ui.py	Mon Jul 23 09:26:59 2007 +0200
@@ -7,7 +7,7 @@
 
 from i18n import _
 import errno, getpass, os, re, socket, sys, tempfile
-import ConfigParser, traceback, util
+import ConfigParser, traceback, util, progress
 
 def dupconfig(orig):
     new = util.configparser(orig.defaults())
@@ -27,6 +27,7 @@ class ui(object):
     def __init__(self, verbose=False, debug=False, quiet=False,
                  interactive=True, traceback=False, report_untrusted=True,
                  parentui=None):
+        self._progress = progress.noprogress(None, None, None)
         self.overlay = None
         self.buffers = []
         if parentui is None:
@@ -358,6 +359,7 @@ class ui(object):
         return "".join(self.buffers.pop())
 
     def write(self, *args):
+        self._progress.interrupt()
         if self.buffers:
             self.buffers[-1].extend([str(a) for a in args])
         else:
@@ -366,6 +368,7 @@ class ui(object):
 
     def write_err(self, *args):
         try:
+            self._progress.interrupt()
             if not sys.stdout.closed: sys.stdout.flush()
             for a in args:
                 sys.stderr.write(str(a))
@@ -386,6 +389,7 @@ class ui(object):
         return sys.stdin.readline()[:-1]
     def prompt(self, msg, pat=None, default="y"):
         if not self.interactive: return default
+        self._progress.interrupt()
         while 1:
             self.write(msg, " ")
             r = self.readline()
@@ -395,6 +399,7 @@ class ui(object):
                 self.write(_("unrecognized response\n"))
     def getpass(self, prompt=None, default=None):
         if not self.interactive: return default
+        self._progress.interrupt()
         return getpass.getpass(prompt or _('password: '))
     def status(self, *msg):
         if not self.quiet: self.write(*msg)
@@ -405,6 +410,7 @@ class ui(object):
     def debug(self, *msg):
         if self.debugflag: self.write(*msg)
     def edit(self, text, user):
+        self._progress.interrupt()
         (fd, name) = tempfile.mkstemp(prefix="hg-editor-", suffix=".txt",
                                       text=True)
         try:
@@ -436,3 +442,13 @@ class ui(object):
         if self.traceback:
             traceback.print_exc()
         return self.traceback
+
+    def progress(self, msg, maxval):
+        """Return progress indicator object"""
+        fd = sys.stderr
+        if self.quiet or maxval < 1 or not fd.isatty():
+            self._progress = progress.noprogress(msg, maxval, fd)
+        else:
+            self._progress = progress.progress(msg, maxval, fd)
+        return self._progress
+



More information about the Mercurial-devel mailing list