Patchwork How to use the templater without color (even when ui has color)

login
register
mail settings
Submitter via Mercurial-devel
Date Nov. 13, 2020, 5:20 p.m.
Message ID <CAESOdVC2HuqTVzErZ6taPoP-4e7X8-hFrtH_56di=VS1j8JwQQ@mail.gmail.com>
Download mbox | patch
Permalink /patch/47594/
State New
Headers show

Comments

via Mercurial-devel - Nov. 13, 2020, 5:20 p.m.
Hi Yuya,

I recently added support for some templated output in `hg split` (
https://phab.mercurial-scm.org/D9255). I didn't realize when I wrote the
patch that it was producing the templated output for use in a commit
message template. The effect was that color codes ended up in the commit
message template. How should I fix it? The following seems to fix it, but
maybe there's a cleaner way?

```
             )
```


Btw, I noticed that Augie made the same mistake in histedit in
https://phab.mercurial-scm.org/D5742.
Yuya Nishihara - Nov. 14, 2020, 9:18 a.m.
On Fri, 13 Nov 2020 09:20:24 -0800, Martin von Zweigbergk wrote:
> I recently added support for some templated output in `hg split` (
> https://phab.mercurial-scm.org/D9255). I didn't realize when I wrote the
> patch that it was producing the templated output for use in a commit
> message template. The effect was that color codes ended up in the commit
> message template. How should I fix it? The following seems to fix it, but
> maybe there's a cleaner way?
> 
> ```
> diff --git a/hgext/split.py b/hgext/split.py
> --- a/hgext/split.py
> +++ b/hgext/split.py
> @@ -142,9 +142,13 @@ def dosplit(ui, repo, tr, ctx, opts):
>              header = _(
>                  b'HG: Splitting %s. So far it has been split into:\n'
>              ) % short(ctx.node())
> -            for c in committed:
> -                summary = cmdutil.format_changeset_summary(ui, c, b'split')
> -                header += _(b'HG: - %s\n') % summary
> +            from mercurial import color
> +            with ui.configoverride({(b'ui', b'color'): b"no"}, b'split'):
> +                color.setup(ui)
> +                for c in committed:
> +                    summary = cmdutil.format_changeset_summary(ui, c,
> b'split')
> +                    header += _(b'HG: - %s\n') % summary
> +            color.setup(ui)

If we don't want to use separate templates for colored/plain outputs, we'll
need to disable the label() function. I think the easiest option is to
remove label() calls by alias:

  --config 'templatealias.label(l, x)=x'

Maybe local aliases can be inserted at formatter.maketemplater().
via Mercurial-devel - Nov. 16, 2020, 6:24 p.m.
That's not a solution I would have thought of. Thanks! I've sent a fix for
`hg split` in https://phab.mercurial-scm.org/D9322. I'll work on one for
histedit now. Hopefully testing it is not too annoying.

On Sat, Nov 14, 2020 at 1:19 AM Yuya Nishihara <yuya@tcha.org> wrote:

> On Fri, 13 Nov 2020 09:20:24 -0800, Martin von Zweigbergk wrote:
> > I recently added support for some templated output in `hg split` (
> > https://phab.mercurial-scm.org/D9255). I didn't realize when I wrote the
> > patch that it was producing the templated output for use in a commit
> > message template. The effect was that color codes ended up in the commit
> > message template. How should I fix it? The following seems to fix it, but
> > maybe there's a cleaner way?
> >
> > ```
> > diff --git a/hgext/split.py b/hgext/split.py
> > --- a/hgext/split.py
> > +++ b/hgext/split.py
> > @@ -142,9 +142,13 @@ def dosplit(ui, repo, tr, ctx, opts):
> >              header = _(
> >                  b'HG: Splitting %s. So far it has been split into:\n'
> >              ) % short(ctx.node())
> > -            for c in committed:
> > -                summary = cmdutil.format_changeset_summary(ui, c,
> b'split')
> > -                header += _(b'HG: - %s\n') % summary
> > +            from mercurial import color
> > +            with ui.configoverride({(b'ui', b'color'): b"no"},
> b'split'):
> > +                color.setup(ui)
> > +                for c in committed:
> > +                    summary = cmdutil.format_changeset_summary(ui, c,
> > b'split')
> > +                    header += _(b'HG: - %s\n') % summary
> > +            color.setup(ui)
>
> If we don't want to use separate templates for colored/plain outputs, we'll
> need to disable the label() function. I think the easiest option is to
> remove label() calls by alias:
>
>   --config 'templatealias.label(l, x)=x'
>
> Maybe local aliases can be inserted at formatter.maketemplater().
>

Patch

diff --git a/hgext/split.py b/hgext/split.py
--- a/hgext/split.py
+++ b/hgext/split.py
@@ -142,9 +142,13 @@  def dosplit(ui, repo, tr, ctx, opts):
             header = _(
                 b'HG: Splitting %s. So far it has been split into:\n'
             ) % short(ctx.node())
-            for c in committed:
-                summary = cmdutil.format_changeset_summary(ui, c, b'split')
-                header += _(b'HG: - %s\n') % summary
+            from mercurial import color
+            with ui.configoverride({(b'ui', b'color'): b"no"}, b'split'):
+                color.setup(ui)
+                for c in committed:
+                    summary = cmdutil.format_changeset_summary(ui, c,
b'split')
+                    header += _(b'HG: - %s\n') % summary
+            color.setup(ui)
             header += _(
                 b'HG: Write commit message for the next split changeset.\n'