-
Notifications
You must be signed in to change notification settings - Fork 37
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
fix: add support for no colors mode #359
Changes from all commits
9525604
cb8d251
965060e
0ee9d40
51bcaf6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,13 @@ | ||
import typing | ||
|
||
import pytest | ||
|
||
from syrupy.utils import env_context | ||
|
||
typing.TYPE_CHECKING = True | ||
pytest_plugins = "pytester" | ||
|
||
|
||
@pytest.fixture | ||
def osenv(): | ||
return env_context |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,42 @@ | ||
from typing import Union | ||
from typing import ( | ||
Any, | ||
Union, | ||
) | ||
|
||
import colored | ||
|
||
from .constants import DISABLE_COLOR_ENV_VARS | ||
from .utils import get_env_value | ||
|
||
|
||
def _is_color_disabled() -> bool: | ||
return any(map(get_env_value, DISABLE_COLOR_ENV_VARS)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm fine with this (and actually prefer it) but I thought we had a lint rule around preferring comprehension over map/reduce etc There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think there's a lint rule around it, just a general case by case basis imo |
||
|
||
|
||
def _stylize(text: Union[str, int], *args: Any) -> str: | ||
if _is_color_disabled(): | ||
return str(text) | ||
return colored.stylize(text, *args) | ||
|
||
|
||
def reset(text: Union[str, int]) -> str: | ||
return colored.stylize(text, colored.attr("reset")) | ||
return _stylize(text, colored.attr("reset")) | ||
|
||
|
||
def red(text: Union[str, int]) -> str: | ||
return colored.stylize(text, colored.fg("red")) | ||
return _stylize(text, colored.fg("red")) | ||
|
||
|
||
def yellow(text: Union[str, int]) -> str: | ||
return colored.stylize(text, colored.fg("yellow")) | ||
return _stylize(text, colored.fg("yellow")) | ||
|
||
|
||
def green(text: Union[str, int]) -> str: | ||
return colored.stylize(text, colored.fg("green")) | ||
return _stylize(text, colored.fg("green")) | ||
|
||
|
||
def bold(text: Union[str, int]) -> str: | ||
return colored.stylize(text, colored.attr("bold")) | ||
return _stylize(text, colored.attr("bold")) | ||
|
||
|
||
def error_style(text: Union[str, int]) -> str: | ||
|
@@ -36,20 +52,20 @@ def success_style(text: Union[str, int]) -> str: | |
|
||
|
||
def snapshot_style(text: Union[str, int]) -> str: | ||
return colored.stylize(text, colored.bg(225) + colored.fg(90)) | ||
return _stylize(text, colored.bg(225) + colored.fg(90)) | ||
|
||
|
||
def snapshot_diff_style(text: Union[str, int]) -> str: | ||
return colored.stylize(text, colored.bg(90) + colored.fg(225)) | ||
return _stylize(text, colored.bg(90) + colored.fg(225)) | ||
|
||
|
||
def received_style(text: Union[str, int]) -> str: | ||
return colored.stylize(text, colored.bg(195) + colored.fg(23)) | ||
return _stylize(text, colored.bg(195) + colored.fg(23)) | ||
|
||
|
||
def received_diff_style(text: Union[str, int]) -> str: | ||
return colored.stylize(text, colored.bg(23) + colored.fg(195)) | ||
return _stylize(text, colored.bg(23) + colored.fg(195)) | ||
|
||
|
||
def context_style(text: Union[str, int]) -> str: | ||
return colored.stylize(text, colored.attr("dim")) | ||
return _stylize(text, colored.attr("dim")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does pytest also natively support
ANSI_COLORS_DISABLED
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So added both