Skip to content

Duplicated output in zsh terminal #9

@aydis20P

Description

@aydis20P

The problem:

The output in terminal (zsh) is printed duplicated, the first time with quotes to indicate that quoted output should be printed with Markdown style, then the correct "styled" output.

Image

Solution:

I've found the way to solve it by changing stream_text, flush_response and run_query in nano_claude.py methods as follows:

-> add import
from rich.live import Live

def stream_text(chunk: str):
    """Keep text. Only print if Rich is not available."""
    _accumulated_text.append(chunk)
    if not _RICH:
        print(chunk, end="", flush=True)
def flush_response():
    """Clean the buffer. Only print Markdown if not in Live mode."""
    # Clean the buffer for the next part of response
    _accumulated_text.clear()
    if not _RICH:
        print()
    def run_query(user_input: str):
        nonlocal verbose
        verbose = config.get("verbose", False)
        system_prompt = build_system_prompt()

        print(clr("\n╭─ Claude ", "dim") + clr("●", "green") + clr(" ─────────────────────────", "dim"))

        # starts Rich's Live rederer
        live = None
        if _RICH:
            live = Live(console=console, auto_refresh=False, vertical_overflow="visible")
            live.start()

        try:
            thinking_started = False
            for event in run(user_input, state, config, system_prompt):
                if isinstance(event, TextChunk):
                    stream_text(event.text)
                    if live:
                        # Update Markdown in screen without duplicating
                        live.update(Markdown("".join(_accumulated_text)), refresh=True)

                elif isinstance(event, ToolStart):
                    if live: live.stop() # 'freeze' current text in screen
                    flush_response()     # Cleans buffer to avoid repeat
                    print_tool_start(event.name, event.inputs, verbose)

                elif isinstance(event, PermissionRequest):
                    event.granted = ask_permission_interactive(event.description, config)

                elif isinstance(event, ToolEnd):
                    print_tool_end(event.name, event.result, verbose)
                    # If more text upcoming, reset Live
                    if live:
                        print(clr("│ ", "dim"), end="", flush=True)
                        live.start()

                elif isinstance(event, ThinkingChunk):
                    if verbose:
                        if live: live.stop()
                        if not thinking_started:
                            print(clr("\n  [thinking]", "dim"))
                            thinking_started = True
                        stream_thinking(event.text, verbose)
                        if live: live.start()

                elif isinstance(event, TurnDone):
                    if verbose:
                        if live: live.stop()
                        print(clr(f"\n  [tokens: +{event.input_tokens} in / +{event.output_tokens} out]", "dim"))
                        if live: live.start()

        finally:
            if live:
                live.stop() # Ensures last part of text gets rederer
            flush_response()
            print(clr("╰──────────────────────────────────────────────", "dim"))
            print()
            from tools import drain_pending_questions
            drain_pending_questions()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions