diff --git a/sgpt/config.py b/sgpt/config.py index 67fad2a..6a19bfe 100644 --- a/sgpt/config.py +++ b/sgpt/config.py @@ -35,7 +35,8 @@ DEFAULT_CONFIG = { "PRETTIFY_MARKDOWN": os.getenv("PRETTIFY_MARKDOWN", "true"), "USE_LITELLM": os.getenv("USE_LITELLM", "false"), "SHELL_INTERACTION": os.getenv("SHELL_INTERACTION ", "true"), - "MARKDOWN_LIVE_VERTICAL_OVERFLOW": os.getenv("MARKDOWN_LIVE_VERTICAL_OVERFLOW", "ellipsis"), + "MARKDOWN_LIVE_VERTICAL_OVERFLOW": os.getenv("MARKDOWN_LIVE_VERTICAL_OVERFLOW", "visible"), + "MARKDOWN_LIVE_REFRESH_INTERVAL": os.getenv("MARKDOWN_LIVE_REFRESH_INTERVAL", "0"), "OS_NAME": os.getenv("OS_NAME", "auto"), "SHELL_NAME": os.getenv("SHELL_NAME", "auto"), # New features might add their own config variables here. diff --git a/sgpt/handlers/handler.py b/sgpt/handlers/handler.py index 20c3d0c..a3a34bb 100644 --- a/sgpt/handlers/handler.py +++ b/sgpt/handlers/handler.py @@ -1,6 +1,8 @@ import json from pathlib import Path -from typing import Any, Callable, Dict, Generator, List, Optional +from typing import Any, Callable, Dict, Generator, List, Optional, cast + +from rich.live_render import VerticalOverflowMethod from ..cache import Cache from ..config import cfg @@ -46,9 +48,12 @@ class Handler: @property def printer(self) -> Printer: - vertical_overflow = cfg.get("MARKDOWN_LIVE_VERTICAL_OVERFLOW") + vertical_overflow = cast( + VerticalOverflowMethod, cfg.get("MARKDOWN_LIVE_VERTICAL_OVERFLOW") + ) + refresh_interval = float(cfg.get("MARKDOWN_LIVE_REFRESH_INTERVAL")) return ( - MarkdownPrinter(self.code_theme, vertical_overflow) + MarkdownPrinter(self.code_theme, refresh_interval, vertical_overflow) if self.markdown else TextPrinter(self.color) ) diff --git a/sgpt/printer.py b/sgpt/printer.py index 3ba88d9..8f70d4c 100644 --- a/sgpt/printer.py +++ b/sgpt/printer.py @@ -1,12 +1,12 @@ +import time from abc import ABC, abstractmethod from typing import Generator, Literal from rich.console import Console from rich.live import Live +from rich.live_render import VerticalOverflowMethod from rich.markdown import Markdown from typer import secho -import time - class Printer(ABC): @@ -33,45 +33,41 @@ class MarkdownPrinter(Printer): def __init__( self, theme: str, - vertical_overflow: Literal["ellipsis", "visible", "crop"] = "ellipsis", + refresh_interval: float, + vertical_overflow: VerticalOverflowMethod, ) -> None: self.console = Console() self.theme = theme - self.vertical_overflow = vertical_overflow + self.refresh_interval = refresh_interval + self.vertical_overflow: VerticalOverflowMethod = vertical_overflow def live_print(self, chunks: Generator[str, None, None]) -> str: - full_completion = "" + full_completion = "" + with Live( + console=self.console, + vertical_overflow=self.vertical_overflow, + auto_refresh=False, + ) as live: + last_refresh = time.monotonic() + for chunk in chunks: + full_completion += chunk + if ( + self.refresh_interval == 0 + or time.monotonic() - last_refresh >= self.refresh_interval + ): + live.update( + Markdown(markup=full_completion, code_theme=self.theme), + refresh=True, + ) + last_refresh = time.monotonic() - with Live( - console=self.console, - vertical_overflow=self.vertical_overflow, - auto_refresh=False, - ) as live: - last_refresh = time.monotonic() - for chunk in chunks: - full_completion += chunk - if time.monotonic() - last_refresh > 0.5: - live.update( - Markdown( - markup=full_completion, - code_theme=self.theme, - ), - refresh=True, - ) - last_refresh = time.monotonic() - - # Avoid excessive terminal flickering caused by frequent refreshes. - # Ensure the complete output is always rendered when streaming finishes. - live.update( - Markdown( - markup=full_completion, - code_theme=self.theme, - ), - refresh=True, - ) - - return full_completion + # Ensure the complete output is always rendered when streaming finishes. + live.update( + Markdown(markup=full_completion, code_theme=self.theme), + refresh=True, + ) + return full_completion def static_print(self, text: str) -> str: markdown = Markdown(markup=text, code_theme=self.theme)