From 7312c7257c2440c690a1cb8a06c822801ca5fff9 Mon Sep 17 00:00:00 2001 From: WANG Xuerui Date: Mon, 9 Sep 2024 14:38:27 +0800 Subject: [PATCH 1/2] fix(cli): ensure line-wrapping for RuyiStyledMarkdown The log stdout console was made non-wrapping by default to fix #181, but it had the unfortunate side-effect of cropping list items and blockquotes that are text contents and really need to be line-wrapped. Avoid the side effect by overriding RuyiStyledMarkdown's render options so that line-wrapping is enabled for Markdown rendering alone, and make NonWrappingCodeBlock undo the line-wrapping options. This way Markdown rendering stays the same as before #187, but logs and code blocks (and only these) can be properly non-wrapped. --- ruyi/utils/markdown.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/ruyi/utils/markdown.py b/ruyi/utils/markdown.py index b4194a8..044852c 100644 --- a/ruyi/utils/markdown.py +++ b/ruyi/utils/markdown.py @@ -26,6 +26,9 @@ def __rich_console__( console: Console, options: ConsoleOptions, ) -> RenderResult: + # re-enable non-wrapping options locally for code blocks + render_options = options.update(no_wrap=True, overflow="ignore") + code = str(self.text).rstrip() syntax = Syntax( code, @@ -34,10 +37,20 @@ def __rich_console__( word_wrap=False, padding=0, ) - return syntax.highlight(code).__rich_console__(console, options) + return syntax.highlight(code).__rich_console__(console, render_options) class RuyiStyledMarkdown(Markdown): elements = Markdown.elements elements["fence"] = NonWrappingCodeBlock elements["heading_open"] = SlimHeading + + def __rich_console__( + self, + console: Console, + options: ConsoleOptions, + ) -> RenderResult: + # we have to undo the ruyi-global console's non-wrapping setting + # for proper CLI rendering of long lines + render_options = options.update(no_wrap=False, overflow="fold") + return super().__rich_console__(console, render_options) From c63a0ac1354f157e88e46b39d291a2b048695001 Mon Sep 17 00:00:00 2001 From: WANG Xuerui Date: Mon, 9 Sep 2024 14:43:05 +0800 Subject: [PATCH 2/2] style: ruff format --- ruyi/cli/__init__.py | 1 + ruyi/log/__init__.py | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/ruyi/cli/__init__.py b/ruyi/cli/__init__.py index 6b97ed9..46bd699 100644 --- a/ruyi/cli/__init__.py +++ b/ruyi/cli/__init__.py @@ -13,6 +13,7 @@ "__main__.py", ) + def is_called_as_ruyi(argv0: str) -> bool: return os.path.basename(argv0).lower() in ALLOWED_RUYI_ENTRYPOINT_NAMES diff --git a/ruyi/log/__init__.py b/ruyi/log/__init__.py index dd69b7a..812bd5d 100644 --- a/ruyi/log/__init__.py +++ b/ruyi/log/__init__.py @@ -27,7 +27,11 @@ def log_time_formatter(x: datetime.datetime) -> Text: STDOUT_CONSOLE = Console(file=sys.stdout, highlight=False, soft_wrap=True) -DEBUG_CONSOLE = Console(file=sys.stderr, log_time_format=log_time_formatter, soft_wrap=True) +DEBUG_CONSOLE = Console( + file=sys.stderr, + log_time_format=log_time_formatter, + soft_wrap=True, +) LOG_CONSOLE = Console(file=sys.stderr, highlight=False, soft_wrap=True) PORCELAIN_SINK = PorcelainOutput(sys.stderr.buffer)