FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Improve legibility on light-background terminals by dlitz · Pull Request #152 · samuelcolvin/python-devtools · GitHub

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (4) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
6 changes: 4 additions & 2 deletions devtools/prettier.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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'

Back | FazBrowse Home | New Git URL