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

login
register
mail settings
Submitter Simon Farnsworth
Date Oct. 7, 2016, 12:07 p.m.
Message ID <ad9be6bb981ca5dccdd4.1475842074@simonfar-macbookpro.local>
Download mbox | patch
Permalink /patch/16875/
State Changes Requested
Headers show

Comments

Simon Farnsworth - Oct. 7, 2016, 12:07 p.m.
# HG changeset patch
# User Simon Farnsworth <simonfar@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
Kyle Lippincott - Oct. 7, 2016, 3:31 p.m.
On Fri, Oct 7, 2016 at 5:07 AM, Simon Farnsworth <simonfar@fb.com> wrote:

> # HG changeset patch
> # User Simon Farnsworth <simonfar@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)
>
> +@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@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
>
Yuya Nishihara - Oct. 8, 2016, 6:50 a.m.
On Fri, 7 Oct 2016 08:31:35 -0700, Kyle Lippincott wrote:
> On Fri, Oct 7, 2016 at 5:07 AM, Simon Farnsworth <simonfar@fb.com> wrote:
> > # HG changeset patch
> > # User Simon Farnsworth <simonfar@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)
> >
> > +@templatefilter('fillterminal')
> > +def fillterminal(text):
> > +    """Any text. Wraps the text to fit in the terminal."""
> > +    return fill(text, util.termwidth())

Using ui.termwidth() seems better because it designed to be overridden in
hgweb. See c52c629ce19e and 94e7bd38d9a3. You can get ui object if
fillterminal() is a template function.

> 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)}'

+1. We don't have arithmetic operators yet, but maybe we can add them.

Patch

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)
 
+@templatefilter('fillterminal')
+def fillterminal(text):
+    """Any text. Wraps the text to fit in the terminal."""
+    return fill(text, util.termwidth())
+
 @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)