-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
--color
/--no-color
and PY_COLORS=
The environment variable `PY_COLORS` overrides `color =` from `pyproject.toml`, and the command line options override both of those.
- Loading branch information
Showing
12 changed files
with
353 additions
and
112 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,12 +1,54 @@ | ||
"""Highlighting of terminal output""" | ||
|
||
try: | ||
import pygments # noqa: F401 | ||
except ImportError: | ||
from darker.highlighting import without_pygments | ||
# pylint: disable=import-outside-toplevel,unused-import | ||
|
||
colorize = without_pygments.colorize | ||
else: | ||
from darker.highlighting import with_pygments | ||
import sys | ||
from typing import Optional, cast | ||
|
||
colorize = with_pygments.colorize | ||
|
||
def should_use_color(config_color: Optional[bool]) -> bool: | ||
"""Return ``True`` if configuration and package support allow output highlighting | ||
In ``config_color``, the combination of ``color =`` in ``pyproject.toml``, the | ||
``PY_COLORS`` environment variable, and the ``--color``/``--no-color`` command line | ||
options determine whether the user wants to force enable or disable highlighting. | ||
If highlighting isn't forced either way, it is automatically enabled for terminal | ||
output. | ||
Finally, if ``pygments`` isn't installed, highlighting is disabled. | ||
:param config_color: The configuration as parsed from ``pyproject.toml`` and | ||
overridden using environment variables and/or command line | ||
options | ||
:return: ``True`` if highlighting should be used | ||
""" | ||
if config_color is not None: | ||
use_color = config_color | ||
else: | ||
use_color = sys.stdout.isatty() | ||
if use_color: | ||
try: | ||
import pygments # noqa | ||
|
||
return True | ||
except ImportError: | ||
pass | ||
return False | ||
|
||
|
||
def colorize(output: str, lexer_name: str, use_color: bool) -> str: | ||
"""Return the output highlighted for terminal if Pygments is available""" | ||
if not use_color: | ||
return output | ||
from pygments import highlight | ||
from pygments.formatters.terminal import TerminalFormatter | ||
from pygments.lexers import get_lexer_by_name | ||
|
||
lexer = get_lexer_by_name(lexer_name) | ||
highlighted = highlight(output, lexer, TerminalFormatter()) | ||
if "\n" not in output: | ||
# see https://github.com/pygments/pygments/issues/1107 | ||
highlighted = highlighted.rstrip("\n") | ||
return cast(str, highlighted) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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 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.