Skip to content

Commit ab62bee

Browse files
Carreaujonathanslenders
authored andcommitted
Allow ansi color name in Pygments style.
this allow using `#ansigreen`,`#ansired`... as pygments color names, and will be matched to 30-37 to ansi escape sequences
1 parent 114f30c commit ab62bee

File tree

5 files changed

+86
-83
lines changed

5 files changed

+86
-83
lines changed

prompt_toolkit/styles/base.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
#: Style attributes.
1919
Attrs = namedtuple('Attrs', 'color bgcolor bold underline italic blink reverse')
2020
"""
21-
:param color: Hexadecimal string. E.g. '000000' or Ansi color name: e.g. 'magenta'
22-
:param bgcolor: Hexadecimal string. E.g. 'ffffff' or Ansi color name: e.g. 'magenta'
21+
:param color: Hexadecimal string. E.g. '000000' or Ansi color name: e.g. 'ansiblue'
22+
:param bgcolor: Hexadecimal string. E.g. 'ffffff' or Ansi color name: e.g. 'ansired'
2323
:param bold: Boolean
2424
:param underline: Boolean
2525
:param italic: Boolean
@@ -37,14 +37,14 @@
3737
#: Usually, in that case, the terminal application allows to configure the RGB
3838
#: values for these names.
3939
ANSI_COLOR_NAMES = [
40-
'black', 'white', 'default',
40+
'ansiblack', 'ansiwhite', 'ansidefault',
4141

4242
# Low intensity.
43-
'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'gray',
43+
'ansired', 'ansigreen', 'ansiyellow', 'ansiblue', 'ansifuchsia', 'ansiturquoise', 'ansilightgray',
4444

4545
# High intensity. (Not supported everywhere.)
46-
'dark-gray', 'bright-red', 'bright-green', 'bright-yellow', 'bright-blue',
47-
'bright-magenta', 'bright-cyan',
46+
'ansidarkgray', 'ansidarkred', 'ansidarkgreen', 'ansibrown', 'ansidarkblue',
47+
'ansipurple', 'ansiteal',
4848
]
4949

5050

prompt_toolkit/styles/from_dict.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ def _colorformat(text):
2626
"""
2727
if text[0:1] == '#':
2828
col = text[1:]
29-
if len(col) == 6:
29+
if col in ANSI_COLOR_NAMES:
30+
return col
31+
elif len(col) == 6:
3032
return col
3133
elif len(col) == 3:
3234
return col[0]*2 + col[1]*2 + col[2]*2
33-
elif text == '' or text in ANSI_COLOR_NAMES:
35+
elif text == '':
3436
return text
3537

3638
raise ValueError('Wrong color format %r' % text)

prompt_toolkit/terminal/vt100_output.py

Lines changed: 54 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -24,79 +24,81 @@
2424

2525

2626
FG_ANSI_COLORS = {
27-
'black': 30,
28-
'default': 39,
29-
'white': 97,
27+
'ansiblack': 30,
28+
'ansidefault': 39,
29+
'ansiwhite': 97,
3030

3131
# Low intensity.
32-
'red': 31,
33-
'green': 32,
34-
'yellow': 33,
35-
'blue': 34,
36-
'magenta': 35,
37-
'cyan': 36,
38-
'gray': 37,
32+
'ansired': 31,
33+
'ansigreen': 32,
34+
'ansiyellow': 33,
35+
'ansiblue': 34,
36+
'ansifuchsia': 35,
37+
'ansiturquoise': 36,
38+
'ansilightgray': 37,
3939

4040

4141
# High intensity.
42-
'dark-gray': 90, # Bright black.
43-
'bright-red': 91,
44-
'bright-green': 92,
45-
'bright-yellow': 93,
46-
'bright-blue': 94,
47-
'bright-magenta': 95,
48-
'bright-cyan': 96,
42+
'ansidarkgray': 90, # Bright black.
43+
'ansidarkred': 91,
44+
'ansidarkgreen': 92,
45+
'ansibrown': 93,
46+
'ansidarkblue': 94,
47+
'ansipurple': 95,
48+
'ansiteal': 96,
4949
}
5050

5151
BG_ANSI_COLORS = {
52-
'black': 40,
53-
'default': 49,
54-
'white': 107,
52+
'ansiblack': 40,
53+
'ansidefault': 49,
54+
'ansiwhite': 107,
5555

5656
# Low intensity.
57-
'red': 41,
58-
'green': 42,
59-
'yellow': 43,
60-
'blue': 44,
61-
'magenta': 45,
62-
'cyan': 46,
63-
'gray': 47,
57+
'ansired': 41,
58+
'ansigreen': 42,
59+
'ansiyellow': 43,
60+
'ansiblue': 44,
61+
'ansifuchsia': 45,
62+
'ansiturquoise': 46,
63+
'ansilightgray': 47,
6464

6565
# High intensity.
66-
'dark-gray': 100, # bright black.
67-
'bright-red': 101,
68-
'bright-green': 102,
69-
'bright-yellow': 103,
70-
'bright-blue': 104,
71-
'bright-magenta': 105,
72-
'bright-cyan': 106,
66+
'ansidarkgray': 100, # bright black.
67+
'ansidarkred': 101,
68+
'ansidarkgreen': 102,
69+
'ansibrown': 103,
70+
'ansidarkblue': 104,
71+
'ansipurple': 105,
72+
'ansiteal': 106,
7373
}
7474

75+
7576
ANSI_COLORS_TO_RGB = {
76-
'black': (0x00, 0x00, 0x00),
77-
'default': (0x00, 0x00, 0x00), # Don't use, 'default' doesn't really have a value.
78-
'white': (0xff, 0xff, 0xff),
77+
'ansiblack': (0x00, 0x00, 0x00),
78+
'ansidefault': (0x00, 0x00, 0x00), # Don't use, 'default' doesn't really have a value.
79+
'ansiwhite': (0xff, 0xff, 0xff),
7980

8081
# Low intensity.
81-
'red': (0xcd, 0x00, 0x00),
82-
'green': (0x00, 0xcd, 0x00),
83-
'yellow': (0xcd, 0xcd, 0x00),
84-
'blue': (0x00, 0x00, 0xcd),
85-
'magenta': (0xcd, 0x00, 0xcd),
86-
'cyan': (0x00, 0xcd, 0xcd),
87-
'gray': (0xe5, 0xe5, 0xe5),
82+
'ansired': (0xcd, 0x00, 0x00),
83+
'ansigreen': (0x00, 0xcd, 0x00),
84+
'ansiyellow': (0xcd, 0xcd, 0x00),
85+
'ansiblue': (0x00, 0x00, 0xcd),
86+
'ansifuchsia': (0xcd, 0x00, 0xcd),
87+
'ansiturquoise': (0x00, 0xcd, 0xcd),
88+
'ansilightgray': (0xe5, 0xe5, 0xe5),
8889

8990

9091
# High intensity.
91-
'dark-gray': (0x7f, 0x7f, 0x7f), # Bright black.
92-
'bright-red': (0xff, 0x00, 0x00),
93-
'bright-green': (0x00, 0xff, 0x00),
94-
'bright-yellow': (0xff, 0xff, 0x00),
95-
'bright-blue': (0x00, 0x00, 0xff),
96-
'bright-magenta': (0xff, 0x00, 0xff),
97-
'bright-cyan': (0x00, 0xff, 0xff),
92+
'ansidarkgray': (0x7f, 0x7f, 0x7f), # Bright black.
93+
'ansidarkred': (0xff, 0x00, 0x00),
94+
'ansidarkgreen': (0x00, 0xff, 0x00),
95+
'ansibrown': (0xff, 0xff, 0x00),
96+
'ansidarkblue': (0x00, 0x00, 0xff),
97+
'ansipurple': (0xff, 0x00, 0xff),
98+
'ansiteal': (0x00, 0xff, 0xff),
9899
}
99100

101+
100102
assert set(FG_ANSI_COLORS) == set(ANSI_COLOR_NAMES)
101103
assert set(BG_ANSI_COLORS) == set(ANSI_COLOR_NAMES)
102104
assert set(ANSI_COLORS_TO_RGB) == set(ANSI_COLOR_NAMES)
@@ -219,7 +221,6 @@ def __init__(self, true_color=False, term='xterm'):
219221

220222
def __missing__(self, attrs):
221223
fgcolor, bgcolor, bold, underline, italic, blink, reverse = attrs
222-
223224
parts = []
224225

225226
if fgcolor:
@@ -268,7 +269,7 @@ def _color_to_code(self, color, bg=False):
268269
# RGB colors. (Defined as 'ffffff'.)
269270
else:
270271
try:
271-
rgb = self._color_name_to_rgb(color)
272+
rgb = self._color_name_to_rgb(color)
272273
except ValueError:
273274
return ()
274275

prompt_toolkit/terminal/win32_output.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -382,28 +382,28 @@ class BACKROUND_COLOR:
382382
def _create_ansi_color_dict(color_cls):
383383
" Create a table that maps the 16 named ansi colors to their Windows code. "
384384
return {
385-
'black': color_cls.BLACK,
386-
'default': color_cls.BLACK,
387-
'white': color_cls.GRAY | color_cls.INTENSITY,
385+
'ansiblack': color_cls.BLACK,
386+
'ansidefault': color_cls.BLACK,
387+
'ansiwhite': color_cls.GRAY | color_cls.INTENSITY,
388388

389389
# Low intensity.
390-
'red': color_cls.RED,
391-
'green': color_cls.GREEN,
392-
'yellow': color_cls.YELLOW,
393-
'blue': color_cls.BLUE,
394-
'magenta': color_cls.MAGENTA,
395-
'cyan': color_cls.CYAN,
396-
'gray': color_cls.GRAY,
390+
'ansired': color_cls.RED,
391+
'ansigreen': color_cls.GREEN,
392+
'ansiyellow': color_cls.YELLOW,
393+
'ansiblue': color_cls.BLUE,
394+
'ansifuchsia': color_cls.MAGENTA,
395+
'ansiturquoise': color_cls.CYAN,
396+
'ansilightgray': color_cls.GRAY,
397397

398398

399399
# High intensity.
400-
'dark-gray': color_cls.BLACK | color_cls.INTENSITY,
401-
'bright-red': color_cls.RED | color_cls.INTENSITY,
402-
'bright-green': color_cls.GREEN | color_cls.INTENSITY,
403-
'bright-yellow': color_cls.YELLOW | color_cls.INTENSITY,
404-
'bright-blue': color_cls.BLUE | color_cls.INTENSITY,
405-
'bright-magenta': color_cls.MAGENTA | color_cls.INTENSITY,
406-
'bright-cyan': color_cls.CYAN | color_cls.INTENSITY,
400+
'ansidarkgray': color_cls.BLACK | color_cls.INTENSITY,
401+
'ansidarkred': color_cls.RED | color_cls.INTENSITY,
402+
'ansidarkgreen': color_cls.GREEN | color_cls.INTENSITY,
403+
'ansibrown': color_cls.YELLOW | color_cls.INTENSITY,
404+
'ansidarkblue': color_cls.BLUE | color_cls.INTENSITY,
405+
'ansipurple': color_cls.MAGENTA | color_cls.INTENSITY,
406+
'ansiteal': color_cls.CYAN | color_cls.INTENSITY,
407407
}
408408

409409
FG_ANSI_COLORS = _create_ansi_color_dict(FOREGROUND_COLOR)

tests/style_tests.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,18 @@ def test_style_inheritance(self):
2424
style = style_from_dict({
2525
Token: '#ff0000',
2626
Token.A.B.C: 'bold',
27-
Token.A.B.C.D: 'red',
27+
Token.A.B.C.D: '#ansired',
2828
Token.A.B.C.D.E: 'noinherit blink'
2929
})
3030

3131
expected = Attrs(color='ff0000', bgcolor=None, bold=True,
3232
underline=False, italic=False, blink=False, reverse=False)
33-
assert style.get_attrs_for_token(Token.A.B.C) == expected
33+
self.assertEqual(style.get_attrs_for_token(Token.A.B.C), expected)
3434

35-
expected = Attrs(color='red', bgcolor=None, bold=True,
35+
expected = Attrs(color='ansired', bgcolor=None, bold=True,
3636
underline=False, italic=False, blink=False, reverse=False)
37-
assert style.get_attrs_for_token(Token.A.B.C.D) == expected
37+
self.assertEqual(style.get_attrs_for_token(Token.A.B.C.D), expected)
3838

3939
expected = Attrs(color=None, bgcolor=None, bold=False,
4040
underline=False, italic=False, blink=True, reverse=False)
41-
assert style.get_attrs_for_token(Token.A.B.C.D.E) == expected
41+
self.assertEqual(style.get_attrs_for_token(Token.A.B.C.D.E), expected)

0 commit comments

Comments
 (0)