changeset 29949:e7cacb45c4be

formatter: introduce isplain() to replace (the inverse of) __nonzero__() (API) V2: also remove and replace __nonzero__
author Mathias De Maré <mathias.demare@gmail.com>
date Mon, 29 Aug 2016 17:19:09 +0200
parents e40343ce9c4c
children 80fef5251099
files mercurial/commands.py mercurial/formatter.py
diffstat 2 files changed, 19 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/commands.py	Tue Aug 30 15:55:07 2016 -0400
+++ b/mercurial/commands.py	Mon Aug 29 17:19:09 2016 +0200
@@ -441,12 +441,12 @@
     if linenumber and (not opts.get('changeset')) and (not opts.get('number')):
         raise error.Abort(_('at least one of -n/-c is required for -l'))
 
-    if fm:
+    if fm.isplain():
+        def makefunc(get, fmt):
+            return lambda x: fmt(get(x))
+    else:
         def makefunc(get, fmt):
             return get
-    else:
-        def makefunc(get, fmt):
-            return lambda x: fmt(get(x))
     funcmap = [(makefunc(get, fmt), sep) for op, sep, get, fmt in opmap
                if opts.get(op)]
     funcmap[0] = (funcmap[0][0], '') # no separator in front of first column
@@ -476,12 +476,12 @@
 
         for f, sep in funcmap:
             l = [f(n) for n, dummy in lines]
-            if fm:
-                formats.append(['%s' for x in l])
-            else:
+            if fm.isplain():
                 sizes = [encoding.colwidth(x) for x in l]
                 ml = max(sizes)
                 formats.append([sep + ' ' * (ml - w) + '%s' for w in sizes])
+            else:
+                formats.append(['%s' for x in l])
             pieces.append(l)
 
         for f, p, l in zip(zip(*formats), zip(*pieces), lines):
@@ -1185,7 +1185,7 @@
         fm = ui.formatter('bookmarks', opts)
         hexfn = fm.hexfunc
         marks = repo._bookmarks
-        if len(marks) == 0 and not fm:
+        if len(marks) == 0 and fm.isplain():
             ui.status(_("no bookmarks set\n"))
         for bmark, n in sorted(marks.iteritems()):
             active = repo._activebookmark
@@ -4442,10 +4442,10 @@
 
     def display(fm, fn, ctx, pstates, states):
         rev = ctx.rev()
-        if fm:
+        if fm.isplain():
+            formatuser = ui.shortuser
+        else:
             formatuser = str
-        else:
-            formatuser = ui.shortuser
         if ui.quiet:
             datefmt = '%Y-%m-%d'
         else:
@@ -5695,10 +5695,10 @@
         pathitems = sorted(ui.paths.iteritems())
 
     fm = ui.formatter('paths', opts)
-    if fm:
+    if fm.isplain():
+        hidepassword = util.hidepassword
+    else:
         hidepassword = str
-    else:
-        hidepassword = util.hidepassword
     if ui.quiet:
         namefmt = '%s\n'
     else:
--- a/mercurial/formatter.py	Tue Aug 30 15:55:07 2016 -0400
+++ b/mercurial/formatter.py	Mon Aug 29 17:19:09 2016 +0200
@@ -57,10 +57,6 @@
     def __exit__(self, exctype, excvalue, traceback):
         if exctype is None:
             self.end()
-    def __nonzero__(self):
-        '''return False if we're not doing real templating so we can
-        skip extra work'''
-        return True
     def _showitem(self):
         '''show a formatted item once all data is collected'''
         pass
@@ -96,6 +92,9 @@
     def plain(self, text, **opts):
         '''show raw text for non-templated mode'''
         pass
+    def isplain(self):
+        '''check for plain formatter usage'''
+        return False
     def nested(self, field):
         '''sub formatter to store nested data in the specified field'''
         self._item[field] = data = []
@@ -142,8 +141,6 @@
             self.hexfunc = hex
         else:
             self.hexfunc = short
-    def __nonzero__(self):
-        return False
     def startitem(self):
         pass
     def data(self, **data):
@@ -156,6 +153,8 @@
             self._ui.write(deftext % fielddata, **opts)
     def plain(self, text, **opts):
         self._ui.write(text, **opts)
+    def isplain(self):
+        return True
     def nested(self, field):
         # nested data will be directly written to ui
         return self