Question about applying templating engine to `hg grep` output

Kostia Balytskyi ikostia at fb.com
Wed Feb 10 14:40:10 UTC 2016


Hi guys,

I am going to try to add templating functionality to `hg grep`. This was suggested some time ago as an alternative solution to adding new command line flags. However, I'd like to have your feedback on the approach that I choose and also your answers to a couple of questions.

Approach
I think that it makes sense to render `hg grep`'s output the same way it is rendered now if no --template option is provided. I would like this to be achieved by having a template constructed form parts, depending on which cmd parameters are provided. Other option is just to use existing code if no --template is given. Downside of the second approach is obvious, but it guarantees consistency and also provides a workaround for a problem that I'm going to describe below.

The problem
`hg grep` currently constructs each line of results in the following way: <result metadata: filename, revision, etc><matches with context>. The second part is interesting since it can contain multiple matches. For example, if a particular revision of `a.txt` has contents "----abcd--------------abcd-------" and we do `hg grep "abcd"`, we will see this whole line described as <matches with context> with actual matches ("abcd") highlighted in a certain color. Codewise this is achieved by applying different labels to different parts of this line, something like this:
"[text]----[/text][match]abcd[/match][text]--------------[/text][mathc]abcd[/match][text]-------[/text]"
If we just incrementally output pieces of line, it's clear how to do it, you just output a piece and apply a label. However, to the best of my knowledge, in template I can only apply labels inside of the template definition, e.g. I can't say "please replace keyword "result" with text "abcd" labeled as [match]". This is a problem in a case when I need to render multiple pieces which have different labels as part of the same template keyword. Is there something in template engine that can solve this problem?

Thanks,
Kostia
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.mercurial-scm.org/pipermail/mercurial-devel/attachments/20160210/0b315d40/attachment.html>


More information about the Mercurial-devel mailing list