Skip to content

Conversation

@Carreau
Copy link
Contributor

@Carreau Carreau commented Feb 14, 2016

Hey,

I know that currently you can use vt100 to get GREN/BLUE ... etc, though if you create your own style that should work with pygments, it wont be accepted as these string won't be detected as correct colors.

Pygments recently added the ability to do that, except the names are slightly different, so this (once finished) should allow to have themes that works correctly with both Pygments 2.2+ and Prompt Toolkit and get the correct ansi escape sequences.

@jonathanslenders
Copy link
Member

Hi @Carreau,
Cool! Thanks for pointing this out.

For this, I don't mind breaking backwards-compatibilty in prompt-toolkit. Ansi colors names have only been added to the latest release of prompt-toolkit, because it was required for pymux. (When an application outputs an ANSI color, instead of using the 256color palette, we wanted to send an ansi color to the terminal as well.) As far as I know, nobody else is really using it.

So, I propose to simply rename our colors that they match those of Pygments.

Jonathan

@Carreau
Copy link
Contributor Author

Carreau commented Feb 15, 2016

So, I propose to simply rename our colors that they match those of Pygments.

Ok, in progress then. Is that true for Win32. enum class keys as well ?

@Carreau Carreau changed the title [WIP] Allow ansi color name in Pygments style. Allow ansi color name in Pygments style. Feb 22, 2016
@Carreau
Copy link
Contributor Author

Carreau commented Feb 22, 2016

ok, done.

Seam to work with current master of IPython, that just got the prompt_toolkit PR merged.

@jonathanslenders
Copy link
Member

That's cool. Thank you!

I was preparing the new release for prompt-toolkit, but I guess, I'll merge this first.

It looks good, but two remarks

  • in ANSI_COLOR_NAMES, all colors have a # in front of them. I think we can remove that. The reason is that in an Attrs instance, normal colors like #ff0000 also don't have a leading # anymore, while they had it in the style dictionary. It's more consistent to remove it in the parser. Having ansi alone as unique prefix should be enough.
  • Could you squash your commits? (If not, I'll do it. ;) )

Thanks again.
Jonathan

@Carreau
Copy link
Contributor Author

Carreau commented Feb 22, 2016

in ANSI_COLOR_NAMES, all colors have a # in front of them. I think we can remove that. The reason is that in an Attrs instance, normal colors like #ff0000 also don't have a leading # anymore, while they had it in the style dictionary. It's more consistent to remove it in the parser. Having ansi alone as unique prefix should be enough.

That will break as pygments does have the # in color names so you need to have the # or Pygments styles will be invalid.

Squash in progress.

@jonathanslenders
Copy link
Member

Yes, sure. It should be in the style dictionary in order to be compatible. But the from_dict function can remove the #. Just like it does for hexadecimal colors.
This is actually the same as Pygments does: https://bitbucket.org/birkenfeld/pygments-main/src/a21baa175f18c45eb5a7b6276333df62a96af1a1/pygments/style.py?at=default&fileviewer=file-view-default#style.py-50:51

@Carreau
Copy link
Contributor Author

Carreau commented Feb 22, 2016

OK, I see.

@Carreau
Copy link
Contributor Author

Carreau commented Feb 22, 2016

Done.

@jonathanslenders
Copy link
Member

Ok, thanks!

I guess, I'll squanch the following change against this commit. Are you fine with that?

diff --git a/prompt_toolkit/styles/base.py b/prompt_toolkit/styles/base.py index dbc7c0a..e9ddaa5 100644 --- a/prompt_toolkit/styles/base.py +++ b/prompt_toolkit/styles/base.py @@ -18,8 +18,8 @@ __all__ = ( #: Style attributes. Attrs = namedtuple('Attrs', 'color bgcolor bold underline italic blink reverse') """ -:param color: Hexadecimal string. E.g. '000000' or Ansi color name: e.g. '#ansiblue' -:param bgcolor: Hexadecimal string. E.g. 'ffffff' or Ansi color name: e.g. '#ansired' +:param color: Hexadecimal string. E.g. '000000' or Ansi color name: e.g. 'ansiblue' +:param bgcolor: Hexadecimal string. E.g. 'ffffff' or Ansi color name: e.g. 'ansired' :param bold: Boolean :param underline: Boolean :param italic: Boolean diff --git a/prompt_toolkit/styles/from_dict.py b/prompt_toolkit/styles/from_dict.py index 34e4ace..4f6e224 100644 --- a/prompt_toolkit/styles/from_dict.py +++ b/prompt_toolkit/styles/from_dict.py @@ -28,7 +28,7 @@ def _colorformat(text): col = text[1:] if col in ANSI_COLOR_NAMES: return col - if len(col) == 6: + elif len(col) == 6: return col elif len(col) == 3: return col[0]*2 + col[1]*2 + col[2]*2 diff --git a/prompt_toolkit/terminal/vt100_output.py b/prompt_toolkit/terminal/vt100_output.py index a98e079..4c33a55 100644 --- a/prompt_toolkit/terminal/vt100_output.py +++ b/prompt_toolkit/terminal/vt100_output.py @@ -99,7 +99,6 @@ ANSI_COLORS_TO_RGB = { } - assert set(FG_ANSI_COLORS) == set(ANSI_COLOR_NAMES) assert set(BG_ANSI_COLORS) == set(ANSI_COLOR_NAMES) assert set(ANSI_COLORS_TO_RGB) == set(ANSI_COLOR_NAMES) diff --git a/prompt_toolkit/terminal/win32_output.py b/prompt_toolkit/terminal/win32_output.py index aef4f52..60d8054 100644 --- a/prompt_toolkit/terminal/win32_output.py +++ b/prompt_toolkit/terminal/win32_output.py @@ -382,28 +382,28 @@ class BACKROUND_COLOR: def _create_ansi_color_dict(color_cls): " Create a table that maps the 16 named ansi colors to their Windows code. " return { - '#ansiblack': color_cls.BLACK, - '#ansidefault': color_cls.BLACK, - '#ansiwhite': color_cls.GRAY | color_cls.INTENSITY, + 'ansiblack': color_cls.BLACK, + 'ansidefault': color_cls.BLACK, + 'ansiwhite': color_cls.GRAY | color_cls.INTENSITY, # Low intensity. - '#ansired': color_cls.RED, - '#ansigreen': color_cls.GREEN, - '#ansiyellow': color_cls.YELLOW, - '#ansiblue': color_cls.BLUE, - '#ansifuchsia': color_cls.MAGENTA, - '#ansiturquoise': color_cls.CYAN, - '#ansilightgray': color_cls.GRAY, + 'ansired': color_cls.RED, + 'ansigreen': color_cls.GREEN, + 'ansiyellow': color_cls.YELLOW, + 'ansiblue': color_cls.BLUE, + 'ansifuchsia': color_cls.MAGENTA, + 'ansiturquoise': color_cls.CYAN, + 'ansilightgray': color_cls.GRAY, # High intensity. - '#ansidarkgray': color_cls.BLACK | color_cls.INTENSITY, - '#ansidarkred': color_cls.RED | color_cls.INTENSITY, - '#ansidarkgreen': color_cls.GREEN | color_cls.INTENSITY, - '#ansibrown': color_cls.YELLOW | color_cls.INTENSITY, - '#ansidarkblue': color_cls.BLUE | color_cls.INTENSITY, - '#ansipurple': color_cls.MAGENTA | color_cls.INTENSITY, - '#ansiteal': color_cls.CYAN | color_cls.INTENSITY, + 'ansidarkgray': color_cls.BLACK | color_cls.INTENSITY, + 'ansidarkred': color_cls.RED | color_cls.INTENSITY, + 'ansidarkgreen': color_cls.GREEN | color_cls.INTENSITY, + 'ansibrown': color_cls.YELLOW | color_cls.INTENSITY, + 'ansidarkblue': color_cls.BLUE | color_cls.INTENSITY, + 'ansipurple': color_cls.MAGENTA | color_cls.INTENSITY, + 'ansiteal': color_cls.CYAN | color_cls.INTENSITY, } FG_ANSI_COLORS = _create_ansi_color_dict(FOREGROUND_COLOR) diff --git a/tests/style_tests.py b/tests/style_tests.py index ff9e71c..cd92143 100644 --- a/tests/style_tests.py +++ b/tests/style_tests.py @@ -32,7 +32,7 @@ class StyleFromDictTest(unittest.TestCase): underline=False, italic=False, blink=False, reverse=False) self.assertEqual(style.get_attrs_for_token(Token.A.B.C), expected) - expected = Attrs(color='#ansired', bgcolor=None, bold=True, + expected = Attrs(color='ansired', bgcolor=None, bold=True, underline=False, italic=False, blink=False, reverse=False) self.assertEqual(style.get_attrs_for_token(Token.A.B.C.D), expected)
@Carreau
Copy link
Contributor Author

Carreau commented Feb 22, 2016

Sure, it's your project, you're the boss.
And sorry I forgot a few #ansi, they should be gone now.

~/d/python-prompt-toolkit (pygments) $ grep -r '#ansi' **.py ~/d/python-prompt-toolkit (pygments) $ 
this allow using `#ansigreen`,`#ansired`... as pygments color names, and will be matched to 30-37 to ansi escape sequences
@Carreau
Copy link
Contributor Author

Carreau commented Feb 23, 2016

Actually I left 1 #ansired in tests

@Carreau
Copy link
Contributor Author

Carreau commented Feb 23, 2016

And travis is happy. He is all green.

@jonathanslenders
Copy link
Member

Merged in: ab62bee

Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants