From ddc167f86e63bebe2db20fb083c649096ae5dae1 Mon Sep 17 00:00:00 2001 From: Jean-Luc Thumm Date: Sun, 14 Jan 2024 07:44:23 -0800 Subject: [PATCH] Fix #422: Markdown formatting for chat history --- sgpt/handlers/chat_handler.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/sgpt/handlers/chat_handler.py b/sgpt/handlers/chat_handler.py index 9919df5..4ad91f5 100644 --- a/sgpt/handlers/chat_handler.py +++ b/sgpt/handlers/chat_handler.py @@ -1,6 +1,8 @@ import json from pathlib import Path from typing import Any, Callable, Dict, Generator, List, Optional +from rich.console import Console +from rich.markdown import Markdown import typer from click import BadArgumentUsage @@ -125,10 +127,11 @@ def list_ids(cls, value: str) -> None: @classmethod def show_messages(cls, chat_id: str) -> None: - # Prints all messages from a specified chat ID to the console. - for index, message in enumerate(cls.chat_session.get_messages(chat_id)): - color = "magenta" if index % 2 == 0 else "green" - typer.secho(message, fg=color) + # Concatenate messages from a specified chat ID and render markdown + history = "\n\n".join(cls.chat_session.get_messages(chat_id)) + console = Console() + md = Markdown(history) + console.print(md) @classmethod @option_callback