-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Use code highlighting if pygments is installed #6658
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
eef9b48
Use code highlighting if pygments is installed
nicoddemus d218218
Use colorama constants instead of bare ascii codes
nicoddemus 84406db
Refactor ASCII color handling into a fixture
nicoddemus 74663a1
Revert back to using explicit color codes
nicoddemus ea74b35
In Python 3.5 skip rest of tests that require ordered markup in color…
nicoddemus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Code is now highlighted in tracebacks when ``pygments`` is installed. | ||
|
||
Users are encouraged to install ``pygments`` into their environment and provide feedback, because | ||
the plan is to make ``pygments`` a regular dependency in the future. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,39 @@ | ||
# Reexport TerminalWriter from here instead of py, to make it easier to | ||
# extend or swap our own implementation in the future. | ||
from py.io import TerminalWriter as TerminalWriter # noqa: F401 | ||
from typing import List | ||
from typing import Sequence | ||
|
||
from py.io import TerminalWriter as BaseTerminalWriter # noqa: F401 | ||
|
||
|
||
class TerminalWriter(BaseTerminalWriter): | ||
def _write_source(self, lines: List[str], indents: Sequence[str] = ()) -> None: | ||
"""Write lines of source code possibly highlighted. | ||
|
||
Keeping this private for now because the API is clunky. We should discuss how | ||
to evolve the terminal writer so we can have more precise color support, for example | ||
being able to write part of a line in one color and the rest in another, and so on. | ||
""" | ||
if indents and len(indents) != len(lines): | ||
raise ValueError( | ||
"indents size ({}) should have same size as lines ({})".format( | ||
len(indents), len(lines) | ||
) | ||
) | ||
if not indents: | ||
indents = [""] * len(lines) | ||
source = "\n".join(lines) | ||
new_lines = self._highlight(source).splitlines() | ||
for indent, new_line in zip(indents, new_lines): | ||
self.line(indent + new_line) | ||
|
||
def _highlight(self, source): | ||
"""Highlight the given source code according to the "code_highlight" option""" | ||
nicoddemus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if not self.hasmarkup: | ||
return source | ||
try: | ||
from pygments.formatters.terminal import TerminalFormatter | ||
nicoddemus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
from pygments.lexers.python import PythonLexer | ||
from pygments import highlight | ||
except ImportError: | ||
return source | ||
else: | ||
return highlight(source, PythonLexer(), TerminalFormatter(bg="dark")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import re | ||
from io import StringIO | ||
|
||
import pytest | ||
from _pytest._io import TerminalWriter | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"has_markup, expected", | ||
[ | ||
pytest.param( | ||
True, "{kw}assert{hl-reset} {number}0{hl-reset}\n", id="with markup" | ||
), | ||
pytest.param(False, "assert 0\n", id="no markup"), | ||
], | ||
) | ||
def test_code_highlight(has_markup, expected, color_mapping): | ||
f = StringIO() | ||
tw = TerminalWriter(f) | ||
tw.hasmarkup = has_markup | ||
tw._write_source(["assert 0"]) | ||
assert f.getvalue().splitlines(keepends=True) == color_mapping.format([expected]) | ||
|
||
with pytest.raises( | ||
ValueError, | ||
match=re.escape("indents size (2) should have same size as lines (1)"), | ||
): | ||
tw._write_source(["assert 0"], [" ", " "]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.