record: consolidate ui.write wrapping in a function
authorLaurent Charignon <lcharignon@fb.com>
Mon, 16 Mar 2015 15:30:33 -0700
changeset 24356 a38f384f2a57
parent 24355 ca4b89683078
child 24357 2da601ab3125
record: consolidate ui.write wrapping in a function
mercurial/cmdutil.py
--- a/mercurial/cmdutil.py	Tue Mar 17 18:20:24 2015 +0900
+++ b/mercurial/cmdutil.py	Mon Mar 16 15:30:33 2015 -0700
@@ -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: