Skip to content

Commit

Permalink
fix: remove colored dependency (#796)
Browse files Browse the repository at this point in the history
  • Loading branch information
noahnu authored Aug 28, 2023
1 parent 22d2197 commit 96bffcc
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 93 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

## Overview

Syrupy is a [pytest](https://docs.pytest.org/en/latest/) snapshot plugin. It enables developers to write tests which assert immutability of computed results.
Syrupy is a zero-dependency [pytest](https://docs.pytest.org/en/latest/) snapshot plugin. It enables developers to write tests which assert immutability of computed results.

## Motivation

Expand Down
1 change: 0 additions & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
[mypy]
mypy_path=stubs
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ syrupy = 'syrupy'

[tool.poetry.dependencies]
python = '>=3.8.1,<4'
colored = '>=1.3.92,<2.0.0'
pytest = '>=7.0.0,<8.0.0'

[tool.poetry.group.test.dependencies]
Expand Down
60 changes: 34 additions & 26 deletions src/syrupy/terminal.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
from typing import (
Any,
Union,
)

import colored
from dataclasses import dataclass
from typing import Union

from .constants import DISABLE_COLOR_ENV_VARS
from .utils import get_env_value
Expand All @@ -13,40 +9,52 @@ def _is_color_disabled() -> bool:
return any(map(get_env_value, DISABLE_COLOR_ENV_VARS))


def _attr(color: Any) -> str:
@dataclass
class TerminalCodes:
ESC: str = "\x1b["
END: str = "m"
FOREGROUND_256: str = f"{ESC}38;5;"
BACKGROUND_256: str = f"{ESC}48;5;"

STYLES = {
"bold": "1",
"dim": "2",
"italic": "3",
"underline": "4",
"reset": "0",
}

COLORS = {
"black": "0",
"red": "1",
"green": "2",
"yellow": "3",
}


def _attr(style: str) -> str:
if _is_color_disabled():
return ""
try:
return colored.attr(color)
except AttributeError:
# colored >=1.5.0, see: https://github.com/tophat/syrupy/issues/758
return colored.style(color) # type: ignore
return f"{TerminalCodes.ESC}{TerminalCodes.STYLES[style]}{TerminalCodes.END}"


def _fg(color: Any) -> str:
def _fg(color: Union[int, str]) -> str:
if _is_color_disabled():
return ""
try:
return colored.fg(color)
except AttributeError:
# colored >=1.5.0, see: https://github.com/tophat/syrupy/issues/758
return colored.fore(color) # type: ignore
color_code = TerminalCodes.COLORS[color] if isinstance(color, (str,)) else color
return f"{TerminalCodes.FOREGROUND_256}{str(color_code)}{TerminalCodes.END}"


def _bg(color: Any) -> str:
def _bg(color: int) -> str:
if _is_color_disabled():
return ""
try:
return colored.bg(color)
except AttributeError:
# colored >=1.5.0, see: https://github.com/tophat/syrupy/issues/758
return colored.back(color) # type: ignore
return f"{TerminalCodes.BACKGROUND_256}{str(color)}{TerminalCodes.END}"


def _stylize(text: Union[str, int], *args: Any) -> str:
def _stylize(text: Union[str, int], formatting: str) -> str:
if _is_color_disabled():
return str(text)
return colored.stylize(text, *args)
return f"{formatting}{text}{_attr('reset')}"


def reset(text: Union[str, int]) -> str:
Expand Down
Empty file removed stubs/__init__.py
Empty file.
6 changes: 0 additions & 6 deletions stubs/colored.pyi

This file was deleted.

58 changes: 0 additions & 58 deletions tests/syrupy/test_terminal.py

This file was deleted.

0 comments on commit 96bffcc

Please sign in to comment.