[PATCH] formatter: make grep command use formatter

Matt Mackall mpm at selenic.com
Wed Mar 9 15:57:25 EST 2016


On Wed, 2016-03-09 at 15:16 +0000, Kostia Balytskyi wrote:
> On 3/9/16, 12:19 AM, "Matt Mackall" <mpm at selenic.com> wrote:
> 
> 
> > On Tue, 2016-03-08 at 18:34 -0500, timeless wrote:
> > > So... I love the idea. Problems:
> > > 
> > > 1. this breaks color.
> > 
> > I'd consider this blocking.
> > 
> > > 2. reviewing the generictemplating plan [1]
> > 
> > But I wouldn't consider this blocking.
> > 
> > > +    tmpl = ["{label('grep.filename', filename)}", "{label('grep.rev',
> > > rev)}"]
> > > +    if opts.get('line_number'):
> > > +        tmpl.append("{label('grep.linenumber', linenum)}")
> > 
> > You're making this too complicated.
> > 
> > The formatter is designed to replace plain old ui.write() from a
> > programmer's
> > perspective, to keep code simple. If you're doing significant rewriting to
> > go
> > from the ui.write() model to the formatter, something is very wrong.
> > 
> > So don't build a template, just make some linear calls:
> > 
> >  fm.startitem()
> >  fm.write("user", "user: %s", user, label="foo.user")
> >  fm.condwrite(showrev, "rev", " rev: %d", rev, label="foo.rev")
> >  fm.plain("\n")
> > 
> > When no -T is provided, this falls through to the aforementioned plain
> > ui.write(), and thus ends up much faster than the templater.
> Hm. What do I do when I have a list, like bookmarks? I could implement
> something like ``fm.writelist("bookmarks", bookmarks)`` I guess, but I'm not
> sure
> whether it's the right approach/desired.

If you need to do something fm.write/condwrite can't do well because %-notation
isn't up to the task, do this:

  fm.data("x", x) # give the formatter access to the "native" value
  xmadepretty = ...       # make it pretty for default display
  fm.plain(xmadepretty)   # bypass the formatter for no -T

Indeed, I recommend putting them right next to each other for clarity. I suppose
we could make a more compact version of this pattern, something like:

  xmadepretty = ...
  fm.write("x", "", x, plain=xmadepretty)

..that could even be one line if <...> isn't too complex.

Your suggestion about writelist is interesting, but consider that the ways we
might want to format a list or dict might vary wildly from command to command.
-- 
Mathematics is the supreme nostalgia of our time.



More information about the Mercurial-devel mailing list