[PATCH 2 of 2] template: filter to fill to terminal width (issue5395)

Kyle Lippincott spectral at pewpew.net
Fri Oct 7 11:31:35 EDT 2016


On Fri, Oct 7, 2016 at 5:07 AM, Simon Farnsworth <simonfar at fb.com> wrote:

> # HG changeset patch
> # User Simon Farnsworth <simonfar at fb.com>
> # Date 1475841473 -7200
> #      Fri Oct 07 13:57:53 2016 +0200
> # Node ID ad9be6bb981ca5dccdd4d64dd682e0cba780067b
> # Parent  5831f8daf49f2eabe5b7e6ba0f3da1fc61b5ed09
> template: filter to fill to terminal width (issue5395)
>
> We can fill to various sizes - terminal width is also useful
>
> diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
> --- a/mercurial/templatefilters.py
> +++ b/mercurial/templatefilters.py
> @@ -165,6 +165,11 @@
>      """Any text. Wraps the text to fit in 76 columns."""
>      return fill(text, 76)
>
> + at templatefilter('fillterminal')
> +def fillterminal(text):
> +    """Any text. Wraps the text to fit in the terminal."""
> +    return fill(text, util.termwidth())
>

Would it make sense to just make 'termwidth' a function in the templater,
which would let you do this yourself?  We may want to do something like:
 -T '{shortest(node,12)} {fill(firstline(),
termwidth()-count(shortest(node,12))-1)}'

Even this is insufficient for many cases, though, unfortunately: we don't
know how many bytes have been taken up by other things (such as the graph
stuff on the left-hand side of `hg log -G`, and also perhaps just even
other things that have happened on the line already, though if it's
something under our control [like above] we can at least handle it by
count()ing the length of it.


+
>  @templatefilter('firstline')
>  def firstline(text):
>      """Any text. Returns the first line of text."""
> diff --git a/tests/test-command-template.t b/tests/test-command-template.t
> --- a/tests/test-command-template.t
> +++ b/tests/test-command-template.t
> @@ -3213,6 +3213,15 @@
>    $ hg log -l1 -T '{fill(desc, date, "", "")}\n'
>    hg: parse error: fill expects an integer width
>    [255]
> +  $ COLUMNS=5 hg log -l1 --template '{fillterminal(desc)}'
> +  desc
> +  to be
> +  wrapp
> +  ed
> +  desc
> +  to be
> +  wrapp
> +  ed (no-eol)
>
>    $ hg log -l 1 --template '{sub(r"[0-9]", "-", author)}'
>    {node|short} (no-eol)
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel at mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.mercurial-scm.org/pipermail/mercurial-devel/attachments/20161007/72abfbe1/attachment.html>


More information about the Mercurial-devel mailing list