From 8fbd94f60b1e12809940cfcc4b2ec9b2f2d70141 Mon Sep 17 00:00:00 2001 From: "Florian G. Hechler" Date: Mon, 17 Feb 2025 01:17:31 +0100 Subject: [PATCH] Fix --show-chat and --repl to respect --no-md (#513) --- README.md | 2 +- sgpt/app.py | 4 +++- sgpt/handlers/chat_handler.py | 9 ++------- sgpt/handlers/repl_handler.py | 2 +- tests/test_default.py | 2 +- 5 files changed, 8 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 31770042..1d81a3a7 100644 --- a/README.md +++ b/README.md @@ -365,7 +365,7 @@ sgpt --role json_generator "random: user, password, email, address" } ``` -If the description of the role contains the words "APPLY MARKDOWN" (case sensitive), then chats will be displayed using markdown formatting. +If the description of the role contains the words "APPLY MARKDOWN" (case sensitive), then chats will be displayed using markdown formatting unless it is explicitly turned off with `--no-md`. ### Request cache Control cache using `--cache` (default) and `--no-cache` options. This caching applies for all `sgpt` requests to OpenAI API: diff --git a/sgpt/app.py b/sgpt/app.py index 733b7ce4..9c711e66 100644 --- a/sgpt/app.py +++ b/sgpt/app.py @@ -107,7 +107,6 @@ def main( show_chat: str = typer.Option( None, help="Show all messages from provided chat id.", - callback=ChatHandler.show_messages_callback, rich_help_panel="Chat Options", ), list_chats: bool = typer.Option( @@ -183,6 +182,9 @@ def main( # Non-interactive shell. pass + if show_chat: + ChatHandler.show_messages(show_chat, md) + if sum((shell, describe_shell, code)) > 1: raise BadArgumentUsage( "Only one of --shell, --describe-shell, and --code options can be used at a time." diff --git a/sgpt/handlers/chat_handler.py b/sgpt/handlers/chat_handler.py index 6ba0a18d..d97bf06c 100644 --- a/sgpt/handlers/chat_handler.py +++ b/sgpt/handlers/chat_handler.py @@ -127,9 +127,9 @@ def list_ids(cls, value: str) -> None: typer.echo(chat_id) @classmethod - def show_messages(cls, chat_id: str) -> None: + def show_messages(cls, chat_id: str, markdown: bool) -> None: color = cfg.get("DEFAULT_COLOR") - if "APPLY MARKDOWN" in cls.initial_message(chat_id): + if "APPLY MARKDOWN" in cls.initial_message(chat_id) and markdown: theme = cfg.get("CODE_THEME") for message in cls.chat_session.get_messages(chat_id): if message.startswith("assistant:"): @@ -143,11 +143,6 @@ def show_messages(cls, chat_id: str) -> None: running_color = color if index % 2 == 0 else "green" typer.secho(message, fg=running_color) - @classmethod - @option_callback - def show_messages_callback(cls, chat_id: str) -> None: - cls.show_messages(chat_id) - def validate(self) -> None: if self.initiated: chat_role_name = self.role.get_role_name(self.initial_message(self.chat_id)) diff --git a/sgpt/handlers/repl_handler.py b/sgpt/handlers/repl_handler.py index df03f7df..04019fd8 100644 --- a/sgpt/handlers/repl_handler.py +++ b/sgpt/handlers/repl_handler.py @@ -24,7 +24,7 @@ def _get_multiline_input(cls) -> str: def handle(self, init_prompt: str, **kwargs: Any) -> None: # type: ignore if self.initiated: rich_print(Rule(title="Chat History", style="bold magenta")) - self.show_messages(self.chat_id) + self.show_messages(self.chat_id, self.markdown) rich_print(Rule(style="bold magenta")) info_message = ( diff --git a/tests/test_default.py b/tests/test_default.py index 0e5e4a57..953227c7 100644 --- a/tests/test_default.py +++ b/tests/test_default.py @@ -70,7 +70,7 @@ def test_show_chat_no_use_markdown(completion, console_print): assert result.exit_code == 0 assert chat_path.exists() - result = runner.invoke(app, ["--show-chat", chat_name]) + result = runner.invoke(app, ["--show-chat", chat_name, "--no-md"]) assert result.exit_code == 0 console_print.assert_not_called()