summaryrefslogtreecommitdiff
path: root/pygments/formatters/terminal256.py
diff options
context:
space:
mode:
Diffstat (limited to 'pygments/formatters/terminal256.py')
-rw-r--r--pygments/formatters/terminal256.py36
1 files changed, 32 insertions, 4 deletions
diff --git a/pygments/formatters/terminal256.py b/pygments/formatters/terminal256.py
index af311955..5110bc9e 100644
--- a/pygments/formatters/terminal256.py
+++ b/pygments/formatters/terminal256.py
@@ -27,6 +27,8 @@
import sys
from pygments.formatter import Formatter
+from pygments.console import codes
+from pygments.style import ansicolors
__all__ = ['Terminal256Formatter', 'TerminalTrueColorFormatter']
@@ -47,9 +49,21 @@ class EscapeSequence:
def color_string(self):
attrs = []
if self.fg is not None:
- attrs.extend(("38", "5", "%i" % self.fg))
+ if self.fg in ansicolors:
+ esc = codes[self.fg[5:]]
+ if ';01m' in esc:
+ self.bold = True
+ # extract fg color code.
+ attrs.append(esc[2:4])
+ else:
+ attrs.extend(("38", "5", "%i" % self.fg))
if self.bg is not None:
- attrs.extend(("48", "5", "%i" % self.bg))
+ if self.bg in ansicolors:
+ esc = codes[self.bg[5:]]
+ # extract fg color code, add 10 for bg.
+ attrs.append(str(int(esc[2:4])+10))
+ else:
+ attrs.extend(("48", "5", "%i" % self.bg))
if self.bold:
attrs.append("01")
if self.underline:
@@ -91,6 +105,11 @@ class Terminal256Formatter(Formatter):
.. versionadded:: 0.9
+ .. versionchanged:: 2.2
+ If the used style defines foreground colors in the form ``#ansi*``, then
+ `Terminal256Formatter` will map these to non extended foreground color.
+ See :ref:`AnsiTerminalStyle` for more information.
+
Options accepted:
`style`
@@ -169,6 +188,10 @@ 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
+ index = color
+ self.best_match[color] = index
if index is None:
try:
rgb = int(str(color), 16)
@@ -185,9 +208,14 @@ class Terminal256Formatter(Formatter):
def _setup_styles(self):
for ttype, ndef in self.style:
escape = EscapeSequence()
- if ndef['color']:
+ # get foreground from ansicolor if set
+ if ndef['ansicolor']:
+ escape.fg = self._color_index(ndef['ansicolor'])
+ elif ndef['color']:
escape.fg = self._color_index(ndef['color'])
- if ndef['bgcolor']:
+ if ndef['bgansicolor']:
+ escape.bg = self._color_index(ndef['bgansicolor'])
+ elif ndef['bgcolor']:
escape.bg = self._color_index(ndef['bgcolor'])
if self.usebold and ndef['bold']:
escape.bold = True