diff options
| author | Georg Brandl <georg@python.org> | 2015-10-17 08:54:08 +0200 |
|---|---|---|
| committer | Georg Brandl <georg@python.org> | 2015-10-17 08:54:08 +0200 |
| commit | d910efe6b3876dabd8ca7a40ab7bd66b1b135162 (patch) | |
| tree | 98a10acb65f270286898ca645b417dbf73028a0e /pygments/formatters | |
| parent | e23a91d9384494f6543313df8a702ef1c4bec3f1 (diff) | |
| download | pygments-d910efe6b3876dabd8ca7a40ab7bd66b1b135162.tar.gz | |
Closes #1142: add 16-million color terminal formatter.
Diffstat (limited to 'pygments/formatters')
| -rwxr-xr-x | pygments/formatters/_mapping.py | 3 | ||||
| -rw-r--r-- | pygments/formatters/terminal256.py | 64 |
2 files changed, 63 insertions, 4 deletions
diff --git a/pygments/formatters/_mapping.py b/pygments/formatters/_mapping.py index 76f1b13f..a2e612ad 100755 --- a/pygments/formatters/_mapping.py +++ b/pygments/formatters/_mapping.py @@ -28,8 +28,9 @@ FORMATTERS = { 'RawTokenFormatter': ('pygments.formatters.other', 'Raw tokens', ('raw', 'tokens'), ('*.raw',), 'Format tokens as a raw representation for storing token streams.'), 'RtfFormatter': ('pygments.formatters.rtf', 'RTF', ('rtf',), ('*.rtf',), 'Format tokens as RTF markup. This formatter automatically outputs full RTF documents with color information and other useful stuff. Perfect for Copy and Paste into Microsoft(R) Word(R) documents.'), 'SvgFormatter': ('pygments.formatters.svg', 'SVG', ('svg',), ('*.svg',), 'Format tokens as an SVG graphics file. This formatter is still experimental. Each line of code is a ``<text>`` element with explicit ``x`` and ``y`` coordinates containing ``<tspan>`` elements with the individual token styles.'), - 'Terminal256Formatter': ('pygments.formatters.terminal256', 'Terminal256', ('terminal256', 'console256', '256'), (), 'Format tokens with ANSI color sequences, for output in a 256-color terminal or console. Like in `TerminalFormatter` color sequences are terminated at newlines, so that paging the output works correctly.'), + 'Terminal256Formatter': ('pygments.formatters.terminal256', 'Terminal256', ('terminal256', 'console256', '256'), (), 'Format tokens with ANSI color sequences, for output in a 256-color terminal or console. Like in `TerminalFormatter` color sequences are terminated at newlines, so that paging the output works correctly.'), 'TerminalFormatter': ('pygments.formatters.terminal', 'Terminal', ('terminal', 'console'), (), 'Format tokens with ANSI color sequences, for output in a text console. Color sequences are terminated at newlines, so that paging the output works correctly.'), + 'TerminalTrueColorFormatter': ('pygments.formatters.terminal256', 'TerminalTrueColor', ('terminal16m', 'console16m', '16m'), (), 'Format tokens with ANSI color sequences, for output in a true-color terminal or console. Like in `TerminalFormatter` color sequences are terminated at newlines, so that paging the output works correctly.'), 'TestcaseFormatter': ('pygments.formatters.other', 'Testcase', ('testcase',), (), 'Format tokens as appropriate for a new testcase.') } diff --git a/pygments/formatters/terminal256.py b/pygments/formatters/terminal256.py index 5d794f4e..af311955 100644 --- a/pygments/formatters/terminal256.py +++ b/pygments/formatters/terminal256.py @@ -29,7 +29,7 @@ import sys from pygments.formatter import Formatter -__all__ = ['Terminal256Formatter'] +__all__ = ['Terminal256Formatter', 'TerminalTrueColorFormatter'] class EscapeSequence: @@ -56,6 +56,18 @@ class EscapeSequence: attrs.append("04") return self.escape(attrs) + def true_color_string(self): + attrs = [] + if self.fg: + attrs.extend(("38", "2", str(self.fg[0]), str(self.fg[1]), str(self.fg[2]))) + if self.bg: + attrs.extend(("48", "2", str(self.bg[0]), str(self.bg[1]), str(self.bg[2]))) + if self.bold: + attrs.append("01") + if self.underline: + attrs.append("04") + return self.escape(attrs) + def reset_string(self): attrs = [] if self.fg is not None: @@ -68,9 +80,9 @@ class EscapeSequence: class Terminal256Formatter(Formatter): - r""" + """ Format tokens with ANSI color sequences, for output in a 256-color - terminal or console. Like in `TerminalFormatter` color sequences + terminal or console. Like in `TerminalFormatter` color sequences are terminated at newlines, so that paging the output works correctly. The formatter takes colors from a style defined by the `style` option @@ -221,3 +233,49 @@ class Terminal256Formatter(Formatter): if not_found: outfile.write(value) + + +class TerminalTrueColorFormatter(Terminal256Formatter): + r""" + Format tokens with ANSI color sequences, for output in a true-color + terminal or console. Like in `TerminalFormatter` color sequences + are terminated at newlines, so that paging the output works correctly. + + .. versionadded:: 2.1 + + Options accepted: + + `style` + The style to use, can be a string or a Style subclass (default: + ``'default'``). + """ + name = 'TerminalTrueColor' + aliases = ['terminal16m', 'console16m', '16m'] + filenames = [] + + def _build_color_table(self): + pass + + def _color_tuple(self, color): + try: + rgb = int(str(color), 16) + except ValueError: + return None + r = (rgb >> 16) & 0xff + g = (rgb >> 8) & 0xff + b = rgb & 0xff + return (r, g, b) + + def _setup_styles(self): + for ttype, ndef in self.style: + escape = EscapeSequence() + if ndef['color']: + escape.fg = self._color_tuple(ndef['color']) + if ndef['bgcolor']: + escape.bg = self._color_tuple(ndef['bgcolor']) + if self.usebold and ndef['bold']: + escape.bold = True + if self.useunderline and ndef['underline']: + escape.underline = True + self.style_string[str(ttype)] = (escape.true_color_string(), + escape.reset_string()) |
