refactor: remove direct stdout handling in spinner class
- Eliminated the `_raw_write` function to simplify output handling in the `KawaiiSpinner` class. - Updated spinner animation and final message display to use standard print statements, ensuring compatibility with prompt_toolkit. - Improved code clarity and maintainability by reducing complexity in the output rendering process.
This commit is contained in:
parent
d64f62c2ef
commit
c100541f07
1 changed files with 4 additions and 23 deletions
|
|
@ -7,7 +7,6 @@ Used by AIAgent._execute_tool_calls for CLI feedback.
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
import sys
|
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
|
@ -16,22 +15,6 @@ _RED = "\033[31m"
|
||||||
_RESET = "\033[0m"
|
_RESET = "\033[0m"
|
||||||
|
|
||||||
|
|
||||||
def _raw_write(text: str):
|
|
||||||
"""Write directly to the real stdout, bypassing prompt_toolkit's patch_stdout proxy.
|
|
||||||
|
|
||||||
prompt_toolkit's StdoutProxy intercepts sys.stdout and can swallow or
|
|
||||||
mishandle \\r carriage returns and inline ANSI escapes that the spinner
|
|
||||||
relies on. sys.__stdout__ is the original file object before any monkey
|
|
||||||
patching, so writes go straight to the terminal.
|
|
||||||
"""
|
|
||||||
out = sys.__stdout__
|
|
||||||
try:
|
|
||||||
out.write(text)
|
|
||||||
out.flush()
|
|
||||||
except (ValueError, OSError):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
# =========================================================================
|
# =========================================================================
|
||||||
# Tool preview (one-line summary of a tool call's primary argument)
|
# Tool preview (one-line summary of a tool call's primary argument)
|
||||||
# =========================================================================
|
# =========================================================================
|
||||||
|
|
@ -186,10 +169,8 @@ class KawaiiSpinner:
|
||||||
frame = self.spinner_frames[self.frame_idx % len(self.spinner_frames)]
|
frame = self.spinner_frames[self.frame_idx % len(self.spinner_frames)]
|
||||||
elapsed = time.time() - self.start_time
|
elapsed = time.time() - self.start_time
|
||||||
line = f" {frame} {self.message} ({elapsed:.1f}s)"
|
line = f" {frame} {self.message} ({elapsed:.1f}s)"
|
||||||
# \033[2K erases the entire current line, \r moves cursor to col 0.
|
clear = '\r' + ' ' * self.last_line_len + '\r'
|
||||||
# Using _raw_write bypasses patch_stdout so the animation renders
|
print(clear + line, end='', flush=True)
|
||||||
# even when prompt_toolkit owns the terminal.
|
|
||||||
_raw_write(f"\033[2K\r{line}")
|
|
||||||
self.last_line_len = len(line)
|
self.last_line_len = len(line)
|
||||||
self.frame_idx += 1
|
self.frame_idx += 1
|
||||||
time.sleep(0.12)
|
time.sleep(0.12)
|
||||||
|
|
@ -209,9 +190,9 @@ class KawaiiSpinner:
|
||||||
self.running = False
|
self.running = False
|
||||||
if self.thread:
|
if self.thread:
|
||||||
self.thread.join(timeout=0.5)
|
self.thread.join(timeout=0.5)
|
||||||
_raw_write(f"\033[2K\r")
|
print('\r' + ' ' * (self.last_line_len + 5) + '\r', end='', flush=True)
|
||||||
if final_message:
|
if final_message:
|
||||||
_raw_write(f" {final_message}\n")
|
print(f" {final_message}", flush=True)
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
self.start()
|
self.start()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue