Skip to content

Commit 248f6c5

Browse files
committed
Improve legibility on light-background terminals
This fixes the problem of light-gray text being rendered on a light background. Users can now choose a different Pygments style/theme using the PY_DEVTOOLS_STYLE environment variable. If PY_DEVTOOLS_STYLE is not set, we attempt auto-detection using the COLORFGBG environment variable, as is commonly done by some tools on Linux and macOS. - Dark-background terminals use the "vim" style, as before. - Light-background terminals now use the "sas" style. - If auto-detection fails, we use the "default" style, which is legible on most terminals. A list of available styles can be obtained by running: import pygments.styles list(pygments.styles.get_all_styles())
1 parent 2aea99d commit 248f6c5

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

devtools/prettier.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from collections import OrderedDict
55
from collections.abc import Generator
66

7-
from .utils import DataClassType, LaxMapping, SQLAlchemyClassType, env_true, isatty
7+
from .utils import DataClassType, LaxMapping, SQLAlchemyClassType, bg_dark_or_light, env_true, isatty
88

99
try:
1010
from functools import cache
@@ -32,6 +32,8 @@
3232
MISSING = object()
3333
PRETTY_KEY = '__prettier_formatted_value__'
3434

35+
PYGMENTS_STYLE = os.getenv('PY_DEVTOOLS_STYLE', {'dark': 'vim', 'light': 'sas', None: 'default'}[bg_dark_or_light()])
36+
3537

3638
def fmt(v: 'Any') -> 'Any':
3739
return {PRETTY_KEY: v}
@@ -50,7 +52,7 @@ def get_pygments() -> 'Tuple[Any, Any, Any]':
5052
except ImportError: # pragma: no cover
5153
return None, None, None
5254
else:
53-
return pygments, PythonLexer(), Terminal256Formatter(style='vim')
55+
return pygments, PythonLexer(), Terminal256Formatter(style=PYGMENTS_STYLE)
5456

5557

5658
# common generator types (this is not exhaustive: things like chain are not include to avoid the import)

devtools/utils.py

+21
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
__all__ = (
55
'isatty',
6+
'bg_dark_or_light',
67
'env_true',
78
'env_bool',
89
'use_highlight',
@@ -111,6 +112,26 @@ def use_highlight(highlight: 'Optional[bool]' = None, file_: 'Any' = None) -> bo
111112
return isatty(file_)
112113

113114

115+
def bg_dark_or_light() -> Optional[str]:
116+
"""
117+
Returns:
118+
'dark' if the terminal background is dark,
119+
'light' if the terminal background is light, or
120+
None if the terminal background color is unknown.
121+
"""
122+
colorfgbg = os.environ.get('COLORFGBG', '')
123+
try:
124+
_, bg_str = colorfgbg.split(';')
125+
bg = int(bg_str)
126+
except ValueError:
127+
return None
128+
if 0 <= bg <= 6 or bg == 8:
129+
return 'dark'
130+
elif bg == 7 or 9 <= bg <= 15:
131+
return 'light'
132+
return None
133+
134+
114135
def is_literal(s: 'Any') -> bool:
115136
import ast
116137

docs/plugins.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def gen_example_html(markdown: str):
5151
def gen_examples_html(m: re.Match) -> str:
5252
sys.path.append(str(THIS_DIR.resolve()))
5353

54-
os.environ.update(PY_DEVTOOLS_HIGHLIGHT='true', PY_DEVTOOLS_WIDTH='80')
54+
os.environ.update(PY_DEVTOOLS_HIGHLIGHT='true', PY_DEVTOOLS_STYLE='vim', PY_DEVTOOLS_WIDTH='80')
5555
conv = Ansi2HTMLConverter()
5656
name = THIS_DIR / Path(m.group(1))
5757

tests/conftest.py

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55

66
def pytest_sessionstart(session):
77
os.environ.pop('PY_DEVTOOLS_HIGHLIGHT', None)
8+
os.environ['PY_DEVTOOLS_STYLE'] = 'vim'

0 commit comments

Comments
 (0)