[PATCH 1 of 2] formatter: introduce isplain() to replace (the inverse of) __nonzero__()
Mathias De Maré
mathias.demare at gmail.com
Mon Sep 19 14:14:41 UTC 2016
# HG changeset patch
# User Mathias De Maré <mathias.demare at gmail.com>
# 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
More information about the Mercurial-devel
mailing list