Commit Graph

36 Commits

Author SHA1 Message Date
Yuya Nishihara
0a5e04d63d formatter: add overview of API and example as doctest 2016-10-22 15:02:11 +09:00
Mathias De Maré
ba1cc0ec1c formatter: introduce isplain() to replace (the inverse of) __nonzero__() (API)
V2: also remove and replace __nonzero__
2016-08-29 17:19:09 +02:00
Yuya Nishihara
e42d0fb100 formatter: add context manager interface for convenience
And port "hg files" to test it.

As you can see, extra indent is necessary to port to this API. I don't think
we should switch every fm.formatter() call to "with" statement.
2016-08-29 00:00:05 +09:00
Yuya Nishihara
a0741d6e1d formatter: add fm.nested(field) to either write or build sub items
We sometimes need to build nested items by formatter, but there was no
convenient way other than building and putting them manually by fm.data():

  exts = []
  for n, v in extensions:
      fm.plain('%s %s\n' % (n, v))
      exts.append({'name': n, 'ver': v})
  fm.data(extensions=exts)

This should work for simple cases, but doing this would make it harder to
change the underlying data type for better templating support.

So this patch provides fm.nested(field), which returns new nested formatter
(or self if items aren't structured and just written to ui.) A nested formatter
stores items which will later be rendered by the parent formatter.

  fn = fm.nested('extensions')
  for n, v in extensions:
      fn.startitem()
      fn.write('name ver', '%s %s\n', n, v)
  fn.end()

Nested items are directly exported to a template for now:

  {extensions % "{name} {ver}\n"}

There's no {extensions} nor {join(extensions, sep)} yet. I have a plan for
them by extending fm.nested() API, but I want to revisit it after trying
out this API in the real world.
2016-03-13 19:59:39 +09:00
Yuya Nishihara
2c363af9f2 formatter: factor out format*() functions to separate classes
New converter classes will be reused by a nested formatter. See the next
patch for details.

This change is also good in that the default values are defined uniquely
by the baseformatter.
2016-08-15 13:51:14 +09:00
Yuya Nishihara
d586657c81 formatter: add function to convert dict to appropriate format
This will be used to process key-value pairs by formatter. The default
field names and format are derived from the {extras} template keyword.

Tests will be added later.
2016-08-15 12:58:33 +09:00
Yuya Nishihara
bcdd7c502f formatter: add function to convert date tuple to appropriate format 2016-07-31 17:07:29 +09:00
Yuya Nishihara
f449ec5977 formatter: add function to convert list to appropriate format (issue5217)
Before, it wasn't possible for formatter to handle array structure other
than date tuple. We've discussed that at the last sprint, which ended we
would probably want to allow only templatable data structure, i.e. a list
of dicts:

  data(tags=[{'tag': a}, {'tag': b}, ...])

Unfortunately, it turned out not working well with template functions:

  "{ifcontains(a, tags, ...)}"
    ^^^^^^^^^^^^^^^^^^
    "a in tags", where tags should be a plain list/set of tags

So the formatter must at least know if the type [{}] was constructed from
a plain list or was actually a list of dicts.

This patch introduces new explicit interface to convert an array structure
to an appropriate data type for the current formatter, which can be used
as follows:

  fm.write('tags', _('tags: %s\n'), fm.formatlist(tags, name='tag'))

No separate fm.data() call should be necessary.
2016-07-10 21:03:06 +09:00
Pulkit Goyal
5f52c722cf py3: conditionalize cPickle import by adding in util
The cPickle is renamed to _pickle in python3 and this C extension is available
 in pickle which was not included in earlier versions. So imports are conditionalized
 to import cPickle in py2 and pickle in py3. Moreover the use of pickle in py2 is
 switched to cPickle as the C extension is faster. The hack is added in util.py and
the modules import util.pickle
2016-06-04 14:38:00 +05:30
Yuya Nishihara
ec53346d72 templater: load and expand aliases by template engine (API) (issue4842)
Now template aliases are fully supported in log and formatter templates.

As I said before, aliases are not expanded in map files. This avoids possible
corruption of our stock styles and web templates. This behavior is undocumented
since no map file nor [templates] section are documented at all. Later on,
we might want to add [aliases] section to map files if it appears to be useful.
2016-03-27 20:59:36 +09:00
Yuya Nishihara
84a8ba9511 templater: factor out function that creates templater from string template
This function will host loading of template aliases. It is not defined at
templater, but at formatter, since formatter is the module handling ui stuff
in front of templater.
2016-04-10 17:23:09 +09:00
Yuya Nishihara
3f981af86b templater: separate function to create templater from map file (API)
New frommapfile() function will make it clear when template aliases will be
loaded. They should be applied to command arguments and templates in hgrc,
but not to map files. Otherwise, our stock styles and web templates
(i.e map-file templates) could be modified unintentionally.

Future patches will add "aliases" argument to __init__(), but not to
frommapfile().
2016-04-03 23:26:48 +09:00
Yuya Nishihara
634500f64e templater: relax unquotestring() to fall back to bare string
This is convenient for our use case where quotes are optional except in
a map file.
2016-03-26 18:12:12 +09:00
Kostia Balytskyi
e31d7d20ae formatter: make labels work with templated output
To describe the bug this fix is addressing, one can do
   ``$ hg status -T "{label('red', path)}\n" --color=debug``
and observe that the label is not applied before my fix and applied with it.
2016-03-08 04:08:33 -08:00
Pierre-Yves David
30913031d4 error: get Abort from 'error' instead of 'util'
The home of 'Abort' is 'error' not 'util' however, a lot of code seems to be
confused about that and gives all the credit to 'util' instead of the
hardworking 'error'. In a spirit of equity, we break the cycle of injustice and
give back to 'error' the respect it deserves. And screw that 'util' poser.

For great justice.
2015-10-08 12:55:45 -07:00
Yuya Nishihara
ca46de8232 formatter: use dict.update() to set arguments passed to write functions
This isn't important, but update() is better than loop in general.
2015-09-23 21:54:47 +09:00
Yuya Nishihara
78102f8d39 formatter: verify number of arguments passed to write functions
zip() takes the shortest length, which can be a source of bug.
2015-09-23 21:51:48 +09:00
Gregory Szorc
9142160136 formatter: use absolute_import 2015-08-08 19:17:40 -07:00
Matt Mackall
0e22b7683e formatter: mark developer options 2015-06-25 17:49:11 -05:00
Matt Mackall
88eb59a3ff formatter: add template support
This lets all the non-log commands that use the formatter use
templates. There are still some things that don't work, for instance:

- color (needs "repo" in map)
- shortest (needs "ctx" in map)
2015-06-10 14:33:38 -05:00
Matt Mackall
110d98d512 formatter: add a method to build a full templater from a -T option 2015-06-10 14:30:18 -05:00
Matt Mackall
8b7c4f2d3a formatter: move most of template option helper to formatter
We want to share this function between formatter and cmdutils. It
doesn't belong in templater because it imports knowledge of ui layers
that shouldn't be there. We'd prefer cmdutil to layer on the formatter
rather than vice-versa. Since the formatter is the handler for -T
options for all non-log commands, let's move the helper there. We
leave the bits specific to the old --style option behind.
2015-06-10 14:29:13 -05:00
Yuya Nishihara
410cff16c8 formatter: convert None to json null
It will be used by "annotate" command to represent the workingctx revision.
2014-09-17 22:34:34 +09:00
Yuya Nishihara
f7d73883e0 formatter: add general way to switch hex/short functions
This seems a bit awkward, but it can avoid duplicates in annotate, tags,
branches and bookmarks.

I guess fm.hexfunc can eventually be removed (or redesigned) when it gets
template backend.
2014-10-03 22:20:02 +09:00
Yuya Nishihara
f8be8539d4 formatter: convert booleans to json
It will be used in branches output.
2014-10-02 23:20:59 +09:00
Yuya Nishihara
9678578b10 formatter: convert float value to json
It will be used to encode ctx.date().
2014-09-17 22:21:01 +09:00
Yuya Nishihara
a0e25a7bf4 formatter: have jsonformatter accept tuple as value
This is necessary for "annotate" to encode ctx.date() in the same manner
as jsonchangeset printer.

It doesn't support list object because keeping mutable object in _item could
be a source of hidden bugs.  Also, I can't think of the use case.
2014-09-17 21:30:22 +09:00
Yuya Nishihara
c8c3f2cd83 formatter: extract function that encode values to json string
This is the stub for tuple support, which will be used to encode ctx.date()
in the same manner as jsonchangeset printer.
2014-09-17 21:15:43 +09:00
Yuya Nishihara
e91eea1470 formatter: correct bool testing which should be __nonzero__ in Python 2 2014-09-17 00:31:03 +09:00
Matt Mackall
8a32bb8140 formatter: add pickle format
This gives convenient Python2 output. Python 3 users will need encoding=bytes.
2014-09-12 18:36:38 -05:00
Matt Mackall
949498b27d formatter: add json formatter 2014-09-12 18:29:29 -05:00
Matt Mackall
be3bc4f866 formatter: make debug style match Python syntax 2014-09-12 19:06:11 -05:00
Matt Mackall
8d7146d630 formatter: add condwrite method
This makes handling conditional output tidier
2012-11-03 14:37:50 -05:00
David M. Carr
9e830e5477 formatter: improve implementation of data method
This alternate syntax was proposed by Bryan O'Sullivan in a review of
441ebe37ceb5.  I haven't been able to measure any particular performance
difference, but the new syntax is more concise and easier to read.
2012-09-20 23:30:59 -04:00
David M. Carr
9b004d69e6 formatter: add base implementation of data method
Previously, nothing was done with the passed in values, which clearly wasn't
the intention.
2012-09-15 22:50:34 -04:00
Matt Mackall
0acc5a5ce3 formatter: add basic formatters 2012-02-20 16:42:47 -06:00