Patchwork [1,of,4] record: consolidate ui.write wrapping in a function

login
register
mail settings
Submitter Laurent Charignon
Date March 18, 2015, 2:17 a.m.
Message ID <34f7b9ef6a7a96acf292.1426645053@dev919.prn2.facebook.com>
Download mbox | patch
Permalink /patch/8134/
State Accepted
Commit a38f384f2a573e61f690b2b1b9ec7515069dc3f5
Headers show

Comments

Laurent Charignon - March 18, 2015, 2:17 a.m.
# HG changeset patch
# User Laurent Charignon <lcharignon@fb.com>
# Date 1426545033 25200
#      Mon Mar 16 15:30:33 2015 -0700
# Node ID 34f7b9ef6a7a96acf292f2721006e5e000bb4d68
# Parent  b7f936f47f2b104a60840bae571e009742126afc
record: consolidate ui.write wrapping in a function

Patch

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -21,6 +21,19 @@ 
 def parsealiases(cmd):
     return cmd.lstrip("^").split("|")
 
+def setupwrapcolorwrite(ui):
+    # wrap ui.write so diff output can be labeled/colorized
+    def wrapwrite(orig, *args, **kw):
+        label = kw.pop('label', '')
+        for chunk, l in patch.difflabel(lambda: args):
+            orig(chunk, label=label + l)
+
+    oldwrite = ui.write
+    def wrap(*args, **kwargs):
+        return wrapwrite(oldwrite, *args, **kwargs)
+    setattr(ui, 'write', wrap)
+    return oldwrite
+
 def recordfilter(ui, originalhunks):
     usecurses =  ui.configbool('experimental', 'crecord', False)
     if usecurses:
@@ -189,17 +202,7 @@ 
             except OSError:
                 pass
 
-    # wrap ui.write so diff output can be labeled/colorized
-    def wrapwrite(orig, *args, **kw):
-        label = kw.pop('label', '')
-        for chunk, l in patch.difflabel(lambda: args):
-            orig(chunk, label=label + l)
-
-    oldwrite = ui.write
-    def wrap(*args, **kwargs):
-        return wrapwrite(oldwrite, *args, **kwargs)
-    setattr(ui, 'write', wrap)
-
+    oldwrite = setupwrapcolorwrite(ui)
     try:
         return commit(ui, repo, recordfunc, pats, opts)
     finally: