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

Yuya Nishihara yuya at tcha.org
Sat Nov 14 09:18:27 UTC 2020


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().



More information about the Mercurial-devel mailing list