[PATCH 6 of 6 DRAFT] progress: Add time remaining h:mm for long tasks

timeless timeless at gmail.com
Tue Oct 19 11:32:23 CDT 2010


# HG changeset patch
# User timeless <timeless at gmail.com>
# Date 1287504568 -10800
# Node ID 9d7ced63526c5eca169eac1b66246879409a7fe9
# Parent  42e0ca8daec0b609bbdb09c85880b2f2a86e7e40
progress: Add time remaining h:mm for long tasks

diff --git a/hgext/progress.py b/hgext/progress.py
--- a/hgext/progress.py
+++ b/hgext/progress.py
@@ -46,6 +46,7 @@ num characters, or ``+<num>`` for the fi
 import sys
 import time
 
+from math import log10
 from mercurial import util
 
 def spacejoin(*args):
@@ -64,6 +65,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,7 +76,7 @@ class progbar(object):
             'progress', 'format',
             default=['topic', 'bar', 'number'])
 
-    def show(self, topic, pos, item, unit, total):
+    def show(self, topic, pos, item, unit, total, now):
         if not shouldprint(self.ui):
             return
         tuple = (topic, pos, item, unit, total)
@@ -128,6 +130,16 @@ 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)):
+                        minutes = (elapsed * (total - pos) / pos) // 60
+                        if minutes:
+                            hours = minutes // 60
+                            minutes -= hours * 60
+                            progwidth -= int(log10(hours+1)) + 4
+                            tail = spacejoin(tail, "%d:%02d" % (hours, minutes))
                 amt = pos * progwidth // total
                 bar = '=' * (amt - 1)
                 if amt > 0:
@@ -171,6 +183,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 +200,13 @@ 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])
+            self.show(curtopic, *self.topicstates[curtopic], now=now)
 
 def uisetup(ui):
     class progressui(ui.__class__):


More information about the Mercurial-devel mailing list