fix(display): use spaces instead of ANSI \033[K in print_above() for prompt_toolkit compat

print_above() used \033[K (erase-to-end-of-line) to clear the spinner
line before printing text above it. This causes garbled escape codes when
prompt_toolkit's patch_stdout is active in CLI mode.

Switched to the same spaces-based clearing approach used by stop() —
overwrite with blanks, then carriage return back to start of line.

Updated test assertion to match the new clearing method.
This commit is contained in:
teknium1 2026-02-28 23:19:23 -08:00
parent dd69f16c3e
commit 4ec386cc72
2 changed files with 6 additions and 3 deletions

View file

@ -211,8 +211,11 @@ class KawaiiSpinner:
if not self.running:
self._write(f" {text}", flush=True)
return
# Clear spinner line, print text above, spinner redraws on next tick
self._write(f"\r\033[K {text}", flush=True)
# Clear spinner line with spaces (not \033[K) to avoid garbled escape
# codes when prompt_toolkit's patch_stdout is active — same approach
# as stop(). Then print text; spinner redraws on next tick.
blanks = ' ' * max(self.last_line_len + 5, 40)
self._write(f"\r{blanks}\r {text}", flush=True)
def stop(self, final_message: str = None):
self.running = False

View file

@ -46,7 +46,7 @@ class TestPrintAbove:
spinner.print_above("tool line")
output = buf.getvalue()
assert "tool line" in output
assert "\r\033[K" in output # Should start with line clear
assert "\r" in output # Should start with carriage return to clear spinner line
def test_print_above_uses_captured_stdout(self):
"""print_above should use self._out, not sys.stdout.