From acc2e613c2943adc289d9c457297bb45174fdc1d Mon Sep 17 00:00:00 2001 From: Ofek Lev Date: Sat, 25 May 2024 22:36:54 -0400 Subject: [PATCH] Fix syntax highlighting contrast for the `config show` command (#1529) --- docs/history/hatch.md | 1 + src/hatch/cli/config/__init__.py | 4 +--- src/hatch/cli/terminal.py | 12 ++++++++++-- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/docs/history/hatch.md b/docs/history/hatch.md index 1b675ab8a..62a25c74f 100644 --- a/docs/history/hatch.md +++ b/docs/history/hatch.md @@ -16,6 +16,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), - The `fmt` command no longer hides the commands that are being executed - Add default timeout for network requests, useful when installing Python distributions +- Fix syntax highlighting contrast for the `config show` command ## [1.11.1](https://github.com/pypa/hatch/releases/tag/hatch-v1.11.1) - 2024-05-23 ## {: #hatch-v1.11.1 } diff --git a/src/hatch/cli/config/__init__.py b/src/hatch/cli/config/__init__.py index 424968e96..f5eec260a 100644 --- a/src/hatch/cli/config/__init__.py +++ b/src/hatch/cli/config/__init__.py @@ -30,10 +30,8 @@ def show(app, all_keys): if not app.config_file.path.is_file(): # no cov app.display_critical('No config file found! Please try `hatch config restore`.') else: - from rich.syntax import Syntax - text = app.config_file.read() if all_keys else app.config_file.read_scrubbed() - app.output(Syntax(text.rstrip(), 'toml', background_color='default')) + app.display_syntax(text.rstrip(), 'toml') @config.command(short_help='Update the config file with any new fields') diff --git a/src/hatch/cli/terminal.py b/src/hatch/cli/terminal.py index c2ccade59..9f3cb7cb9 100644 --- a/src/hatch/cli/terminal.py +++ b/src/hatch/cli/terminal.py @@ -134,6 +134,9 @@ def __output(self, text): class Terminal: def __init__(self, *, verbosity: int, enable_color: bool | None, interactive: bool | None): + # Force consistent output for test assertions + self.testing = 'HATCH_SELF_TESTING' in os.environ + self.verbosity = verbosity self.console = Console( force_terminal=enable_color, @@ -142,8 +145,7 @@ def __init__(self, *, verbosity: int, enable_color: bool | None, interactive: bo markup=False, emoji=False, highlight=False, - # Force consistent output for test assertions - legacy_windows=False if 'HATCH_SELF_TESTING' in os.environ else None, + legacy_windows=False if self.testing else None, ) # Set defaults so we can pretty print before loading user config @@ -276,6 +278,12 @@ def display_mini_header(self, text, *, stderr=False, indent=None, link=None): def display_header(self, title=''): self.console.rule(Text(title, self._style_level_success)) + def display_syntax(self, *args, **kwargs): + from rich.syntax import Syntax + + kwargs.setdefault('background_color', 'default' if self.testing else None) + self.output(Syntax(*args, **kwargs)) + def display_markdown(self, text, **kwargs): # no cov from rich.markdown import Markdown