Patchwork [3,of,3] progress: specify updatebar() function by constructor argument

login
register
mail settings
Submitter Yuya Nishihara
Date Jan. 13, 2019, 6:43 a.m.
Message ID <ed29a1a69f505fb8b35e.1547361791@mimosa>
Download mbox | patch
Permalink /patch/37703/
State Accepted
Headers show

Comments

Yuya Nishihara - Jan. 13, 2019, 6:43 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1547357805 -32400
#      Sun Jan 13 14:36:45 2019 +0900
# Node ID ed29a1a69f505fb8b35e8a36c220a50c99baa4cd
# Parent  e13ab96098d053c755988b4e94e5c99b203f70e8
progress: specify updatebar() function by constructor argument

This makes it easy for ui extensions to intercept progress messages. It also
seems slightly nicer in that scmutil.progress doesn't touch ui internals.
via Mercurial-devel - Jan. 13, 2019, 7:44 a.m.
On Sat, Jan 12, 2019 at 10:48 PM Yuya Nishihara <yuya@tcha.org> wrote:

> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1547357805 -32400
> #      Sun Jan 13 14:36:45 2019 +0900
> # Node ID ed29a1a69f505fb8b35e8a36c220a50c99baa4cd
> # Parent  e13ab96098d053c755988b4e94e5c99b203f70e8
> progress: specify updatebar() function by constructor argument


Queued, thanks.

Patch

diff --git a/mercurial/scmutil.py b/mercurial/scmutil.py
--- a/mercurial/scmutil.py
+++ b/mercurial/scmutil.py
@@ -1415,28 +1415,13 @@  def wlocksub(repo, cmd, *args, **kwargs)
                     **kwargs)
 
 class progress(object):
-    def __init__(self, ui, topic, unit="", total=None):
+    def __init__(self, ui, updatebar, topic, unit="", total=None):
         self.ui = ui
         self.pos = 0
         self.topic = topic
         self.unit = unit
         self.total = total
         self.debug = ui.configbool('progress', 'debug')
-        if getattr(ui._fmsgerr, 'structured', False):
-            # channel for machine-readable output with metadata, just send
-            # raw information
-            # TODO: consider porting some useful information (e.g. estimated
-            # time) from progbar. we might want to support update delay to
-            # reduce the cost of transferring progress messages.
-            def updatebar(topic, pos, item, unit, total):
-                ui._fmsgerr.write(None, type=b'progress', topic=topic,
-                                  pos=pos, item=item, unit=unit,
-                                  total=total)
-        elif ui._progbar is not None:
-            updatebar = ui._progbar.progress
-        else:
-            def updatebar(topic, pos, item, unit, total):
-                pass
         self._updatebar = updatebar
 
     def __enter__(self):
diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -1698,8 +1698,22 @@  class ui(object):
             progress.complete()
 
     def makeprogress(self, topic, unit="", total=None):
-        '''exists only so low-level modules won't need to import scmutil'''
-        return scmutil.progress(self, topic, unit, total)
+        """Create a progress helper for the specified topic"""
+        if getattr(self._fmsgerr, 'structured', False):
+            # channel for machine-readable output with metadata, just send
+            # raw information
+            # TODO: consider porting some useful information (e.g. estimated
+            # time) from progbar. we might want to support update delay to
+            # reduce the cost of transferring progress messages.
+            def updatebar(topic, pos, item, unit, total):
+                self._fmsgerr.write(None, type=b'progress', topic=topic,
+                                    pos=pos, item=item, unit=unit, total=total)
+        elif self._progbar is not None:
+            updatebar = self._progbar.progress
+        else:
+            def updatebar(topic, pos, item, unit, total):
+                pass
+        return scmutil.progress(self, updatebar, topic, unit, total)
 
     def getlogger(self, name):
         """Returns a logger of the given name; or None if not registered"""