Re: [PATCH 1 of 2] formatter: introduce isplain() to replace (the inverse of) __nonzero__()

2016-09-20 Thread Yuya Nishihara
On Mon, 19 Sep 2016 09:14:41 -0500, Mathias De Maré wrote:
> # HG changeset patch
> # User Mathias De Maré 
> # Date 1472483949 -7200
> #  Mon Aug 29 17:19:09 2016 +0200
> # Node ID 084ca55ce77a5fe77bc93db26346cc71211a0070
> # Parent  285a8c3e53f2183438f0cdbc238e4ab851d0d110
> formatter: introduce isplain() to replace (the inverse of) __nonzero__()

You have one more "if fm:". Fixed as follows and queued this, thanks. Also
flagged as (API).

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -4442,10 +4442,10 @@ def grep(ui, repo, pattern, *pats, **opt
 
 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:
diff --git a/mercurial/formatter.py b/mercurial/formatter.py
--- a/mercurial/formatter.py
+++ b/mercurial/formatter.py
@@ -141,8 +141,6 @@ class plainformatter(baseformatter):
 self.hexfunc = hex
 else:
 self.hexfunc = short
-def __nonzero__(self):
-return False
 def startitem(self):
 pass
 def data(self, **data):
___
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel


[PATCH 1 of 2] formatter: introduce isplain() to replace (the inverse of) __nonzero__()

2016-09-19 Thread Mathias De Maré
# HG changeset patch
# User Mathias De Maré 
# Date 1472483949 -7200
#  Mon Aug 29 17:19:09 2016 +0200
# Node ID 084ca55ce77a5fe77bc93db26346cc71211a0070
# Parent  285a8c3e53f2183438f0cdbc238e4ab851d0d110
formatter: introduce isplain() to replace (the inverse of) __nonzero__()

V2: also remove and replace __nonzero__

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -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
@@ -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:
diff --git a/mercurial/formatter.py b/mercurial/formatter.py
--- a/mercurial/formatter.py
+++ b/mercurial/formatter.py
@@ -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 = []
@@ -156,6 +155,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
___
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel