[PATCH] progress: Add time remaining for long tasks

timeless timeless at gmail.com
Tue Oct 19 17:33:26 CDT 2010


# HG changeset patch
# User timeless <timeless at gmail.com>
# Date 1287526613 -10800
# Node ID 2b78730aa7e275e86a6852ac992d4559546a185c
# Parent  42e0ca8daec0b609bbdb09c85880b2f2a86e7e40
progress: Add time remaining for long tasks

diff --git a/hgext/progress.py b/hgext/progress.py
--- a/hgext/progress.py
+++ b/hgext/progress.py
@@ -64,6 +64,7 @@ class progbar(object):
         self.topics = []
         self.topicstates = {}
         self.pendingtopics = {}
+        self.starttimes = {}
         self.printed = False
         self.lastprint = time.time() + float(self.ui.config(
             'progress', 'delay', default=3))
@@ -74,10 +75,10 @@ class progbar(object):
             'progress', 'format',
             default=['topic', 'bar', 'number'])
 
-    def show(self, topic, pos, item, unit, total):
+    def show(self, now, tuple):
         if not shouldprint(self.ui):
             return
-        tuple = (topic, pos, item, unit, total)
+        (topic, pos, item, unit, total) = tuple
         if self.printed == tuple:
             # don't double-print progress information, otherwise
             # possible in the case of unwinding the topic stack
@@ -128,6 +129,23 @@ class progbar(object):
                 used += len(tail) + 1
             progwidth = termwidth - used - 3
             if total and pos <= total:
+                if pos > 0:
+                    elapsed = now - self.starttimes[topic]
+                    if elapsed > float(
+                        self.ui.config('progress', 'estimate', default=10)):
+                        seconds = elapsed * (total - pos) / pos
+                        minutes = seconds // 60
+                        if minutes < 10:
+                            seconds -= minutes * 60
+                            remaining = "%dm%02d" % (minutes, seconds)
+                        else:
+                            # we're going to ignore seconds in this case
+                            minutes += 1
+                            hours = minutes // 60
+                            minutes -= hours * 60
+                            remaining = "%dh%02d" % (hours, minutes)
+                        progwidth -= len(remaining) + 1
+                        tail = spacejoin(tail, remaining)
                 amt = pos * progwidth // total
                 bar = '=' * (amt - 1)
                 if amt > 0:
@@ -171,6 +189,7 @@ class progbar(object):
         now = time.time()
         if pos is None:
             self.pendingtopics.pop(topic, None)
+            self.starttimes.pop(topic, None)
             if self.topics and self.topics[0] == topic and self.printed:
                 self.complete()
                 self.resetstate()
@@ -187,12 +206,14 @@ class progbar(object):
                     return
             if topic not in self.topics:
                 self.pendingtopics[topic] = 0
+                self.starttimes[topic] = now
                 self.topics.append(topic)
             self.topicstates[topic] = pos, item, unit, total
         if now - self.lastprint >= self.refresh and self.topics:
             self.lastprint = now
             curtopic = self.topics[-1]
-            self.show(curtopic, *self.topicstates[curtopic])
+            tuple = (curtopic,) + self.topicstates[curtopic]
+            self.show(now, tuple)
 
 def uisetup(ui):
     class progressui(ui.__class__):


More information about the Mercurial-devel mailing list