From 6783862a9a3ed6e95ac685d2c8d637aece3a12c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=80=81Idea?= <1021349117@qq.com> Date: Tue, 23 Jun 2026 10:37:42 +0800 Subject: [PATCH] Make markdown vertical_overflow configurable (#779) --- README.md | 23 ++++++++++++++++++ sgpt/config.py | 1 + sgpt/handlers/handler.py | 3 ++- sgpt/printer.py | 50 ++++++++++++++++++++++++++++++++-------- 4 files changed, 67 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 65c4040..3573eb2 100644 --- a/README.md +++ b/README.md @@ -391,9 +391,32 @@ SHOW_FUNCTIONS_OUTPUT=false OPENAI_USE_FUNCTIONS=true # Enforce LiteLLM usage (for local LLMs). USE_LITELLM=false +# Control how markdown live rendering handles overflow when output exceeds terminal height. +# Possible values: ellipsis, visible, crop +MARKDOWN_LIVE_VERTICAL_OVERFLOW=ellipsis ``` Possible options for `DEFAULT_COLOR`: black, red, green, yellow, blue, magenta, cyan, white, bright_black, bright_red, bright_green, bright_yellow, bright_blue, bright_magenta, bright_cyan, bright_white. Possible options for `CODE_THEME`: https://pygments.org/styles/ +Possible options for `MARKDOWN_LIVE_VERTICAL_OVERFLOW`: `ellipsis`, `visible`, `crop`. + +### Configuration Examples + +**Default behavior (ellipsis):** +```text +MARKDOWN_LIVE_VERTICAL_OVERFLOW=ellipsis +``` +When the markdown output exceeds the terminal height, only `...` is shown. This is the default and preserves backward compatibility. + +**Visible mode (recommended for REPL sessions):** +```text +MARKDOWN_LIVE_VERTICAL_OVERFLOW=visible +``` +All generated markdown content is visible in real-time. This is especially useful for long-running REPL interactions or agent workflows where you want to observe the model's reasoning process, tool calls, and intermediate outputs. + +```shell +sgpt --repl +``` +With `visible` mode, you can continuously observe generated markdown output, tool execution details, and progress updates instead of staring at `...` for several minutes. ### Full list of arguments ```text diff --git a/sgpt/config.py b/sgpt/config.py index 24ea5bd..67fad2a 100644 --- a/sgpt/config.py +++ b/sgpt/config.py @@ -35,6 +35,7 @@ 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"), "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 39c675f..20c3d0c 100644 --- a/sgpt/handlers/handler.py +++ b/sgpt/handlers/handler.py @@ -46,8 +46,9 @@ class Handler: @property def printer(self) -> Printer: + vertical_overflow = cfg.get("MARKDOWN_LIVE_VERTICAL_OVERFLOW") return ( - MarkdownPrinter(self.code_theme) + MarkdownPrinter(self.code_theme, vertical_overflow) if self.markdown else TextPrinter(self.color) ) diff --git a/sgpt/printer.py b/sgpt/printer.py index 565b837..3ba88d9 100644 --- a/sgpt/printer.py +++ b/sgpt/printer.py @@ -1,10 +1,12 @@ from abc import ABC, abstractmethod -from typing import Generator +from typing import Generator, Literal from rich.console import Console from rich.live import Live from rich.markdown import Markdown from typer import secho +import time + class Printer(ABC): @@ -28,18 +30,48 @@ class Printer(ABC): class MarkdownPrinter(Printer): - def __init__(self, theme: str) -> None: + def __init__( + self, + theme: str, + vertical_overflow: Literal["ellipsis", "visible", "crop"] = "ellipsis", + ) -> None: self.console = Console() self.theme = theme + self.vertical_overflow = vertical_overflow def live_print(self, chunks: Generator[str, None, None]) -> str: - full_completion = "" - with Live(console=self.console) as live: - for chunk in chunks: - full_completion += chunk - markdown = Markdown(markup=full_completion, code_theme=self.theme) - live.update(markdown, refresh=True) - return 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 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 + def static_print(self, text: str) -> str: markdown = Markdown(markup=text, code_theme=self.theme)