diff options
-rw-r--r-- | doc/docs/styles.rst | 49 | ||||
-rw-r--r-- | pygments/console.py | 11 | ||||
-rw-r--r-- | pygments/formatters/terminal256.py | 12 | ||||
-rw-r--r-- | pygments/style.py | 63 | ||||
-rw-r--r-- | tests/test_terminal_formatter.py | 10 |
5 files changed, 103 insertions, 42 deletions
diff --git a/doc/docs/styles.rst b/doc/docs/styles.rst index 1094a270..cd0144ab 100644 --- a/doc/docs/styles.rst +++ b/doc/docs/styles.rst @@ -153,17 +153,17 @@ Terminal Styles .. versionadded:: 2.2 Custom styles used with the 256-color terminal formatter can also map colors to -use the 8 default ANSI colors. To do so, use ``#ansigreen``, ``#ansired`` or +use the 8 default ANSI colors. To do so, use ``ansigreen``, ``ansibrightred`` or any other colors defined in :attr:`pygments.style.ansicolors`. Foreground ANSI colors will be mapped to the corresponding `escape codes 30 to 37 <https://en.wikipedia.org/wiki/ANSI_escape_code#Colors>`_ thus respecting any custom color mapping and themes provided by many terminal emulators. Light variants are treated as foreground color with and an added bold flag. -``bg:#ansi<color>`` will also be respected, except the light variant will be the +``bg:ansi<color>`` will also be respected, except the light variant will be the same shade as their dark variant. See the following example where the color of the string ``"hello world"`` is -governed by the escape sequence ``\x1b[34;01m`` (Ansi Blue, Bold, 41 being red +governed by the escape sequence ``\x1b[34;01m`` (Ansi bright blue, Bold, 41 being red background) instead of an extended foreground & background color. .. sourcecode:: pycon @@ -176,7 +176,7 @@ background) instead of an extended foreground & background color. >>> class MyStyle(Style): styles = { - Token.String: '#ansiblue bg:#ansired', + Token.String: 'ansibrightblue bg:ansibrightred', } >>> code = 'print("Hello World")' @@ -184,18 +184,51 @@ background) instead of an extended foreground & background color. >>> print(result.encode()) b'\x1b[34;41;01m"\x1b[39;49;00m\x1b[34;41;01mHello World\x1b[39;49;00m\x1b[34;41;01m"\x1b[39;49;00m' -Colors specified using ``#ansi*`` are converted to a default set of RGB colors +Colors specified using ``ansi*`` are converted to a default set of RGB colors when used with formatters other than the terminal-256 formatter. By definition of ANSI, the following colors are considered "light" colors, and will be rendered by most terminals as bold: -- "darkgray", "red", "green", "yellow", "blue", "fuchsia", "turquoise", "white" +- "brightblack" (darkgrey), "brightred", "brightgreen", "brightyellow", "brightblue", + "brightmagenta", "brightcyan", "white" The following are considered "dark" colors and will be rendered as non-bold: -- "black", "darkred", "darkgreen", "brown", "darkblue", "purple", "teal", - "lightgray" +- "black", "red", "green", "yellow", "blue", "magenta", "cyan", + "gray" Exact behavior might depends on the terminal emulator you are using, and its settings. + +.. _NewAnsiColorNames: + +.. versionchanged:: 2.3 + +The definition of the ansi color names has changed. +New names are easier to understand and align to the colors used in other projects. + + ++-------------------------+--------------------------+ +| New names | Pygments 2.2 | ++=======================+============================+ +| ``ansiblack`` | ``#ansiblack`` | +| ``ansired`` | ``#ansidarkred`` | +| ``ansigreen`` | ``#ansidarkgreen`` | +| ``ansiyellow`` | ``#ansibrown`` | +| ``ansiblue`` | ``#ansidarkblue`` | +| ``ansimagenta`` | ``#ansipurple`` | +| ``ansicyan`` | ``#ansiteal`` | +| ``ansigray`` | ``#ansilightgray`` | +| ``ansibrightblack`` | ``#ansidarkgray`` | +| ``ansibrightred`` | ``#ansired`` | +| ``ansibrightgreen`` | ``#ansigreen`` | +| ``ansibrightyellow`` | ``#ansiyellow`` | +| ``ansibrightblue`` | ``#ansiblue`` | +| ``ansibrightmagenta`` | ``#ansifuchsia`` | +| ``ansibrightcyan`` | ``#ansiturquoise`` | +| ``ansiwhite`` | ``#ansiwhite`` | ++=========================+==========================+ + +Old ansi color names are deprecated but will still work. + diff --git a/pygments/console.py b/pygments/console.py index 31b6839d..e61744ce 100644 --- a/pygments/console.py +++ b/pygments/console.py @@ -22,10 +22,10 @@ codes["underline"] = esc + "04m" codes["blink"] = esc + "05m" codes["overline"] = esc + "06m" -dark_colors = ["black", "darkred", "darkgreen", "brown", "darkblue", - "purple", "teal", "lightgray"] -light_colors = ["darkgray", "red", "green", "yellow", "blue", - "fuchsia", "turquoise", "white"] +dark_colors = ["black", "red", "green", "yellow", "blue", + "magenta", "cyan", "gray"] +light_colors = ["brightblack", "brightred", "brightgreen", "brightyellow", "brightblue", + "brightmagenta", "brightcyan", "white"] x = 30 for d, l in zip(dark_colors, light_colors): @@ -35,9 +35,6 @@ for d, l in zip(dark_colors, light_colors): del d, l, x -codes["darkteal"] = codes["turquoise"] -codes["darkyellow"] = codes["brown"] -codes["fuscia"] = codes["fuchsia"] codes["white"] = codes["bold"] diff --git a/pygments/formatters/terminal256.py b/pygments/formatters/terminal256.py index b80dc7dd..aec19804 100644 --- a/pygments/formatters/terminal256.py +++ b/pygments/formatters/terminal256.py @@ -50,7 +50,7 @@ class EscapeSequence: attrs = [] if self.fg is not None: if self.fg in ansicolors: - esc = codes[self.fg[5:]] + esc = codes[self.fg.replace('ansi','')] if ';01m' in esc: self.bold = True # extract fg color code. @@ -59,7 +59,7 @@ class EscapeSequence: attrs.extend(("38", "5", "%i" % self.fg)) if self.bg is not None: if self.bg in ansicolors: - esc = codes[self.bg[5:]] + esc = codes[self.bg.replace('ansi','')] # extract fg color code, add 10 for bg. attrs.append(str(int(esc[2:4])+10)) else: @@ -110,6 +110,12 @@ class Terminal256Formatter(Formatter): `Terminal256Formatter` will map these to non extended foreground color. See :ref:`AnsiTerminalStyle` for more information. + .. versionchanged:: 2.3 + The ansi color names have been updated with names that are easier to + understand and align with colornames of other projects and terminals. + See :ref:`NewAnsiColorNames` for more information. + + Options accepted: `style` @@ -189,7 +195,7 @@ class Terminal256Formatter(Formatter): def _color_index(self, color): index = self.best_match.get(color, None) if color in ansicolors: - # strip the `#ansi` part and look up code + # strip the `ansi/#ansi` part and look up code index = color self.best_match[color] = index if index is None: diff --git a/pygments/style.py b/pygments/style.py index 879c4e05..89766d8c 100644 --- a/pygments/style.py +++ b/pygments/style.py @@ -12,26 +12,47 @@ from pygments.token import Token, STANDARD_TYPES from pygments.util import add_metaclass -# Default mapping of #ansixxx to RGB colors. +# Default mapping of ansixxx to RGB colors. _ansimap = { # dark - '#ansiblack': '000000', - '#ansidarkred': '7f0000', - '#ansidarkgreen': '007f00', - '#ansibrown': '7f7fe0', - '#ansidarkblue': '00007f', - '#ansipurple': '7f007f', - '#ansiteal': '007f7f', - '#ansilightgray': 'e5e5e5', + 'ansiblack': '000000', + 'ansired': '7f0000', + 'ansigreen': '007f00', + 'ansiyellow': '7f7fe0', + 'ansiblue': '00007f', + 'ansimagenta': '7f007f', + 'ansicyan': '007f7f', + 'ansigray': 'e5e5e5', # normal - '#ansidarkgray': '555555', - '#ansired': 'ff0000', - '#ansigreen': '00ff00', - '#ansiyellow': 'ffff00', - '#ansiblue': '0000ff', - '#ansifuchsia': 'ff00ff', - '#ansiturquoise': '00ffff', - '#ansiwhite': 'ffffff', + 'ansibrightblack': '555555', + 'ansibrightred': 'ff0000', + 'ansibrightgreen': '00ff00', + 'ansibrightyellow': 'ffff00', + 'ansibrightblue': '0000ff', + 'ansibrightmagenta': 'ff00ff', + 'ansibrightcyan': '00ffff', + 'ansiwhite': 'ffffff', +} +# mapping of deprecated #ansixxx colors to new color names +_deprecated_ansicolors = { + # dark + '#ansiblack': 'ansiblack', + '#ansidarkred': 'ansired', + '#ansidarkgreen': 'ansigreen', + '#ansibrown': 'ansiyellow', + '#ansidarkblue': 'ansiblue', + '#ansipurple': 'ansimagenta', + '#ansiteal': 'ansicyan', + '#ansilightgray': 'ansigray', + # normal + '#ansidarkgray': 'ansibrightblack', + '#ansired': 'ansibrightred', + '#ansigreen': 'ansibrightgreen', + '#ansiyellow': 'ansibrightyellow', + '#ansiblue': 'ansibrightblue', + '#ansifuchsia': 'ansibrightmagenta', + '#ansiturquoise': 'ansibrightcyan', + '#ansiwhite': 'ansiwhite', } ansicolors = set(_ansimap) @@ -106,11 +127,15 @@ class StyleMeta(type): t = cls._styles[token] ansicolor = bgansicolor = None color = t[0] - if color.startswith('#ansi'): + if color in _deprecated_ansicolors: + color = _deprecated_ansicolors[color] + if color in ansicolors: ansicolor = color color = _ansimap[color] bgcolor = t[4] - if bgcolor.startswith('#ansi'): + if bgcolor in _deprecated_ansicolors: + bgcolor = _deprecated_ansicolors[color] + if bgcolor in ansicolors: bgansicolor = bgcolor bgcolor = _ansimap[bgcolor] diff --git a/tests/test_terminal_formatter.py b/tests/test_terminal_formatter.py index ee0ac380..1f44807d 100644 --- a/tests/test_terminal_formatter.py +++ b/tests/test_terminal_formatter.py @@ -61,10 +61,10 @@ class TerminalFormatterTest(unittest.TestCase): class MyStyle(Style): styles = { - Token.Comment: '#ansidarkgray', - Token.String: '#ansiblue bg:#ansidarkred', - Token.Number: '#ansigreen bg:#ansidarkgreen', - Token.Number.Hex: '#ansidarkgreen bg:#ansired', + Token.Comment: 'ansibrightblack', + Token.String: 'ansibrightblue bg:ansired', + Token.Number: 'ansibrightgreen bg:ansigreen', + Token.Number.Hex: 'ansigreen bg:ansibrightred', } @@ -90,7 +90,7 @@ async def function(a,b,c, *d, **kwarg:Bool)->Bool: def test_256esc_seq(self): """ - test that a few escape sequences are actualy used when using #ansi<> color codes + test that a few escape sequences are actualy used when using ansi<> color codes """ def termtest(x): return highlight(x, Python3Lexer(), |