true color and 256 terminal formatters added and use PYTEST_THEME env variable for style

This commit is contained in:
pandula12 2024-05-13 14:53:47 +09:30
parent eea04c2891
commit a1591f8aaf
3 changed files with 65 additions and 50 deletions

View File

@ -308,6 +308,7 @@ Omar Kohl
Omer Hadari Omer Hadari
Ondřej Súkup Ondřej Súkup
Oscar Benjamin Oscar Benjamin
Pandula Gajadeera
Parth Patel Parth Patel
Patrick Hayes Patrick Hayes
Patrick Lannigan Patrick Lannigan

View File

@ -0,0 +1,5 @@
Improved the terminal writer to be able to use Terminal256Formatter and TerminalTrueColorFormatter.
These are newer versions of TerminalFormmater from the pygments library that is currently in use.
These style of these formatters are set by the users through the environmental variable PYTEST_THEME.

View File

@ -194,58 +194,67 @@ class TerminalWriter:
for indent, new_line in zip(indents, new_lines): for indent, new_line in zip(indents, new_lines):
self.line(indent + new_line) self.line(indent + new_line)
def _highlight( def _highlight(
self, source: str, lexer: Literal["diff", "python"] = "python" self, source: str, lexer: Literal["diff", "python"] = "python"
) -> str: ) -> str:
"""Highlight the given source if we have markup support.""" """Highlight the given source if we have markup support."""
from _pytest.config.exceptions import UsageError from _pytest.config.exceptions import UsageError
if not source or not self.hasmarkup or not self.code_highlight: if not source or not self.hasmarkup or not self.code_highlight:
return source return source
try: try:
from pygments.formatters.terminal import TerminalFormatter from pygments.formatters.terminal import TerminalFormatter
if lexer == "python": if lexer == "python":
from pygments.lexers.python import PythonLexer as Lexer from pygments.lexers.python import PythonLexer as Lexer
elif lexer == "diff": elif lexer == "diff":
from pygments.lexers.diff import DiffLexer as Lexer from pygments.lexers.diff import DiffLexer as Lexer
else:
assert_never(lexer)
from pygments import highlight
import pygments.util
except ImportError:
return source
else: else:
try: assert_never(lexer)
highlighted: str = highlight(
source,
Lexer(),
TerminalFormatter(
bg=os.getenv("PYTEST_THEME_MODE", "dark"),
style=os.getenv("PYTEST_THEME"),
),
)
# pygments terminal formatter may add a newline when there wasn't one.
# We don't want this, remove.
if highlighted[-1] == "\n" and source[-1] != "\n":
highlighted = highlighted[:-1]
# Some lexers will not set the initial color explicitly from pygments import highlight
# which may lead to the previous color being propagated to the import pygments.util
# start of the expression, so reset first.
return "\x1b[0m" + highlighted if os.getenv("COLORTERM", "") in ("truecolor", "24bit"):
except pygments.util.ClassNotFound as e: from pygments.formatters.terminal256 import TerminalTrueColorFormatter
raise UsageError( terminal_formatter = TerminalTrueColorFormatter(style=os.getenv("PYTEST_THEME", "default"))
"PYTEST_THEME environment variable had an invalid value: '{}'. " elif "256" in os.getenv("TERM", ""):
"Only valid pygment styles are allowed.".format( from pygments.formatters.terminal256 import Terminal256Formatter
os.getenv("PYTEST_THEME") terminal_formatter = Terminal256Formatter(style=os.getenv("PYTEST_THEME", "default"))
) else:
) from e terminal_formatter = TerminalFormatter(bg=os.getenv("PYTEST_THEME_MODE", "dark"),
except pygments.util.OptionError as e: style=os.getenv("PYTEST_THEME", "default"))
raise UsageError(
"PYTEST_THEME_MODE environment variable had an invalid value: '{}'. " except ImportError:
"The only allowed values are 'dark' and 'light'.".format( return source
os.getenv("PYTEST_THEME_MODE") else:
) try:
) from e highlighted: str = highlight(
source,
Lexer(),
terminal_formatter,
)
# pygments terminal formatter may add a newline when there wasn't one.
# We don't want this, remove.
if highlighted[-1] == "\n" and source[-1] != "\n":
highlighted = highlighted[:-1]
# Some lexers will not set the initial color explicitly
# which may lead to the previous color being propagated to the
# start of the expression, so reset first.
return "\x1b[0m" + highlighted
except pygments.util.ClassNotFound as e:
raise UsageError(
"PYTEST_THEME environment variable had an invalid value: '{}'. "
"Only valid pygment styles are allowed.".format(
os.getenv("PYTEST_THEME")
)
) from e
except pygments.util.OptionError as e:
raise UsageError(
"PYTEST_THEME_MODE environment variable had an invalid value: '{}'. "
"The only allowed values are 'dark' and 'light'.".format(
os.getenv("PYTEST_THEME_MODE")
)
) from e