[PATCH 7 of 8] progress: Add estimated time remaining for long tasks

Augie Fackler durin42 at gmail.com
Thu Dec 9 17:41:07 CST 2010


# HG changeset patch
# User timeless <timeless at gmail.com>
# Date 1288093318 -10800
# Node ID a1c27febbb6cdab52e44a6b96098cfe800055665
# Parent  fc30ad3b4f8d6d465be150e411dc24fb07ac4807
progress: Add estimated time remaining for long tasks

Output looks roughly like this:
updating [======================>                       ] 1547/4842 0m13s

output will either show h:m or m:s

diff --git a/hgext/progress.py b/hgext/progress.py
--- a/hgext/progress.py
+++ b/hgext/progress.py
@@ -46,6 +46,7 @@
 import sys
 import time
 
+from mercurial.i18n import _
 from mercurial import util
 
 def spacejoin(*args):
@@ -64,6 +65,8 @@
         self.topics = []
         self.topicstates = {}
         self.pendingtopics = {}
+        self.starttimes = {}
+        self.startvals = {}
         self.printed = False
         self.lastprint = time.time() + float(self.ui.config(
             'progress', 'delay', default=3))
@@ -74,10 +77,10 @@
             '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 +131,26 @@
                 used += len(tail) + 1
             progwidth = termwidth - used - 3
             if total and pos <= total:
+                initial = self.startvals[topic]
+                target = total - initial
+                delta = pos - initial
+                if delta > 0:
+                    elapsed = now - self.starttimes[topic]
+                    if elapsed > float(
+                        self.ui.config('progress', 'estimate', default=2)):
+                        seconds = (elapsed * (target - delta)) // delta + 1
+                        minutes = seconds // 60
+                        if minutes < 10:
+                            seconds -= minutes * 60
+                            remaining = _("%dm%02ds") % (minutes, seconds)
+                        else:
+                            # we're going to ignore seconds in this case
+                            minutes += 1
+                            hours = minutes // 60
+                            minutes -= hours * 60
+                            remaining = _("%dh%02dm") % (hours, minutes)
+                        progwidth -= len(remaining) + 1
+                        tail = spacejoin(tail, remaining)
                 amt = pos * progwidth // total
                 bar = '=' * (amt - 1)
                 if amt > 0:
@@ -171,6 +194,8 @@
         now = time.time()
         if pos is None:
             self.pendingtopics.pop(topic, None)
+            self.starttimes.pop(topic, None)
+            self.startvals.pop(topic, None)
             if self.topics and self.topics[0] == topic and self.printed:
                 self.complete()
                 self.resetstate()
@@ -187,12 +212,15 @@
                     return
             if topic not in self.topics:
                 self.pendingtopics[topic] = 0
+                self.starttimes[topic] = now
+                self.startvals[topic] = pos
                 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