formatter: add condwrite method
authorMatt Mackall <mpm@selenic.com>
Sat, 03 Nov 2012 14:37:50 -0500
changeset 17909 3326fd05eb1f
parent 17908 42f8ee0e04ac
child 17910 c8709ff57ff2
formatter: add condwrite method This makes handling conditional output tidier
mercurial/formatter.py
--- a/mercurial/formatter.py	Wed Oct 24 23:09:31 2012 +0200
+++ b/mercurial/formatter.py	Sat Nov 03 14:37:50 2012 -0500
@@ -31,6 +31,10 @@
         '''do default text output while assigning data to item'''
         for k, v in zip(fields.split(), fielddata):
             self._item[k] = v
+    def condwrite(self, cond, fields, deftext, *fielddata, **opts):
+        '''do conditional write (primarily for plain formatter)'''
+        for k, v in zip(fields.split(), fielddata):
+            self._item[k] = v
     def plain(self, text, **opts):
         '''show raw text for non-templated mode'''
         pass
@@ -51,6 +55,10 @@
         pass
     def write(self, fields, deftext, *fielddata, **opts):
         self._ui.write(deftext % fielddata, **opts)
+    def condwrite(self, cond, fields, deftext, *fielddata, **opts):
+        '''do conditional write'''
+        if cond:
+            self._ui.write(deftext % fielddata, **opts)
     def plain(self, text, **opts):
         self._ui.write(text, **opts)
     def end(self):