Debian Codegraph @

src/syrupy/terminal.py

raw
1 from collections.abc import Iterator
2 from contextlib import contextmanager
3 from contextvars import ContextVar
4 from dataclasses import dataclass
5
6 from .constants import DISABLE_COLOR_ENV_VARS
7 from .utils import get_env_value
8
9 # Toggled internally to suppress colors for a block of output. A ContextVar is
10 # used instead of mutating os.environ, which is not thread safe and can segfault
11 # unrelated threads that read the environment from native code.
12 _disable_color: ContextVar[bool] = ContextVar("syrupy_disable_color", default=False)
13
14
15 @contextmanager
16 def disable_color() -> Iterator[None]:
17 """Suppress terminal colors for the duration of the context."""
18 token = _disable_color.set(True)
19 try:
20 yield
21 finally:
22 _disable_color.reset(token)
23
24
25 def _is_color_disabled() -> bool:
26 return _disable_color.get() or any(map(get_env_value, DISABLE_COLOR_ENV_VARS))
27
28
29 @dataclass
30 class TerminalCodes:
31 ESC: str = "\x1b["
32 END: str = "m"
33 FOREGROUND_256: str = f"{ESC}38;5;"
34 BACKGROUND_256: str = f"{ESC}48;5;"
35
36 STYLES = {
37 "bold": "1",
38 "dim": "2",
39 "italic": "3",
40 "underline": "4",
41 "reset": "0",
42 }
43
44 COLORS = {
45 "black": "0",
46 "red": "1",
47 "green": "2",
48 "yellow": "3",
49 }
50
51
52 def _attr(style: str) -> str:
53 if _is_color_disabled():
54 return ""
55 return f"{TerminalCodes.ESC}{TerminalCodes.STYLES[style]}{TerminalCodes.END}"
56
57
58 def _fg(color: int | str) -> str:
59 if _is_color_disabled():
60 return ""
61 color_code = TerminalCodes.COLORS[color] if isinstance(color, (str,)) else color
62 return f"{TerminalCodes.FOREGROUND_256}{str(color_code)}{TerminalCodes.END}"
63
64
65 def _bg(color: int) -> str:
66 if _is_color_disabled():
67 return ""
68 return f"{TerminalCodes.BACKGROUND_256}{str(color)}{TerminalCodes.END}"
69
70
71 def _stylize(text: str | int, formatting: str) -> str:
72 if _is_color_disabled():
73 return str(text)
74 return f"{formatting}{text}{_attr('reset')}"
75
76
77 def reset(text: str | int) -> str:
78 return _stylize(text, _attr("reset"))
79
80
81 def red(text: str | int) -> str:
82 return _stylize(text, _fg("red"))
83
84
85 def yellow(text: str | int) -> str:
86 return _stylize(text, _fg("yellow"))
87
88
89 def green(text: str | int) -> str:
90 return _stylize(text, _fg("green"))
91
92
93 def bold(text: str | int) -> str:
94 return _stylize(text, _attr("bold"))
95
96
97 def error_style(text: str | int) -> str:
98 return bold(red(text))
99
100
101 def warning_style(text: str | int) -> str:
102 return bold(yellow(text))
103
104
105 def success_style(text: str | int) -> str:
106 return bold(green(text))
107
108
109 def snapshot_style(text: str | int) -> str:
110 return _stylize(text, _bg(225) + _fg(90))
111
112
113 def snapshot_diff_style(text: str | int) -> str:
114 return _stylize(text, _bg(90) + _fg(225))
115
116
117 def received_style(text: str | int) -> str:
118 return _stylize(text, _bg(195) + _fg(23))
119
120
121 def received_diff_style(text: str | int) -> str:
122 return _stylize(text, _bg(23) + _fg(195))
123
124
125 def context_style(text: str | int) -> str:
126 return _stylize(text, _attr("dim"))