This commit is contained in:
pandula12 2024-06-10 10:05:08 +03:00 committed by GitHub
commit 5e266bc386
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 25 additions and 4 deletions

View File

@ -311,6 +311,7 @@ Omar Kohl
Omer Hadari
Ondřej Súkup
Oscar Benjamin
Pandula Gajadeera
Parth Patel
Patrick Hayes
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

@ -218,13 +218,28 @@ class TerminalWriter:
return source
else:
try:
# Import new more rich color formatters from the pygments library
from pygments.formatters.terminal256 import Terminal256Formatter
from pygments.formatters.terminal256 import TerminalTrueColorFormatter
# Use terminal formatters depending on user environment variables
if os.environ.get("COLORTERM", "") in ("truecolor", "24bit"):
# Style determined by user set environment variable, if none then use default style
terminal_formatter = TerminalTrueColorFormatter(
style=os.getenv("PYTEST_THEME", "default")
)
elif "256" in os.environ.get("TERM", ""):
terminal_formatter = Terminal256Formatter(
style=os.getenv("PYTEST_THEME", "default")
)
else:
terminal_formatter = TerminalFormatter(
bg=os.getenv("PYTEST_THEME_MODE", "dark"),
style=os.getenv("PYTEST_THEME"),
)
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.