Skip to content

Improve legibility on light-background terminals #152

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions devtools/prettier.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from collections import OrderedDict
from collections.abc import Generator

from .utils import DataClassType, LaxMapping, SQLAlchemyClassType, env_true, isatty
from .utils import DataClassType, LaxMapping, SQLAlchemyClassType, bg_dark_or_light, env_true, isatty

try:
from functools import cache
Expand Down Expand Up @@ -32,6 +32,8 @@
MISSING = object()
PRETTY_KEY = '__prettier_formatted_value__'

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


def fmt(v: 'Any') -> 'Any':
return {PRETTY_KEY: v}
Expand All @@ -50,7 +52,7 @@ def get_pygments() -> 'Tuple[Any, Any, Any]':
except ImportError: # pragma: no cover
return None, None, None
else:
return pygments, PythonLexer(), Terminal256Formatter(style='vim')
return pygments, PythonLexer(), Terminal256Formatter(style=PYGMENTS_STYLE)


# common generator types (this is not exhaustive: things like chain are not include to avoid the import)
Expand Down
21 changes: 21 additions & 0 deletions devtools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

__all__ = (
'isatty',
'bg_dark_or_light',
'env_true',
'env_bool',
'use_highlight',
Expand Down Expand Up @@ -111,6 +112,26 @@ def use_highlight(highlight: 'Optional[bool]' = None, file_: 'Any' = None) -> bo
return isatty(file_)


def bg_dark_or_light() -> 'Optional[str]':
"""
Returns:
'dark' if the terminal background is dark,
'light' if the terminal background is light, or
None if the terminal background color is unknown.
"""
colorfgbg = os.environ.get('COLORFGBG', '')
try:
_, bg_str = colorfgbg.split(';')
bg = int(bg_str)
except ValueError:
return None
if 0 <= bg <= 6 or bg == 8:
return 'dark'
elif bg == 7 or 9 <= bg <= 15:
return 'light'
return None


def is_literal(s: 'Any') -> bool:
import ast

Expand Down
2 changes: 1 addition & 1 deletion docs/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def gen_example_html(markdown: str):
def gen_examples_html(m: re.Match) -> str:
sys.path.append(str(THIS_DIR.resolve()))

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

Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@

def pytest_sessionstart(session):
os.environ.pop('PY_DEVTOOLS_HIGHLIGHT', None)
os.environ['PY_DEVTOOLS_STYLE'] = 'vim'
Loading