[PATCH 1 of 4] Add a simple progress indicator

Stefano Tortarolo stefano at inventati.org
Wed Apr 30 08:04:30 CDT 2008


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

diff -r 626cb86a6523 -r a360beb5ab20 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 626cb86a6523 -r a360beb5ab20 mercurial/ui.py
--- a/mercurial/ui.py	Thu Apr 24 17:16:02 2008 +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())
@@ -29,6 +29,7 @@
     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:
@@ -377,6 +378,7 @@
         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:
@@ -385,6 +387,7 @@
 
     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))
@@ -424,6 +427,7 @@
         If not interactive -- the default is returned
         """
         if not self.interactive: return default
+        self._progress.interrupt()
         while True:
             try:
                 r = self._readline(msg + ' ')
@@ -438,6 +442,7 @@
 
     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)
@@ -448,6 +453,7 @@
     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:
@@ -478,6 +484,15 @@
             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
+
     def geteditor(self):
         '''return editor to use'''
         return (os.environ.get("HGEDITOR") or


More information about the Mercurial-devel mailing list