From 7217abfe23ab9e46b632a3d6bda8023438751982 Mon Sep 17 00:00:00 2001 From: Ahmed Allam <49919286+0xallam@users.noreply.github.com> Date: Tue, 9 Jun 2026 09:22:50 -0700 Subject: [PATCH] Strip ANSI escapes and control bytes from terminal tool output (#554) --- strix/interface/tui/renderers/shell_renderer.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/strix/interface/tui/renderers/shell_renderer.py b/strix/interface/tui/renderers/shell_renderer.py index d15c0c5..131bd6c 100644 --- a/strix/interface/tui/renderers/shell_renderer.py +++ b/strix/interface/tui/renderers/shell_renderer.py @@ -26,6 +26,11 @@ _EXIT_RE = re.compile(r"Process exited with code (-?\d+)") _SESSION_RE = re.compile(r"Process running with session ID (\d+)") _OUTPUT_HEADER = "\nOutput:\n" +_CONTROL_BYTES_TO_DROP = dict.fromkeys( + [b for b in range(0x20) if b not in (0x09, 0x0A)] + [0x7F], + None, +) + @cache def _get_style_colors() -> dict[Any, str]: @@ -60,14 +65,13 @@ def _parse_sdk_shell_result(result: Any) -> dict[str, Any]: def _truncate_line(line: str) -> str: - clean_line = re.sub(r"\x1b\[[0-9;]*m", "", line) - if len(clean_line) > MAX_LINE_LENGTH: + if len(line) > MAX_LINE_LENGTH: return line[: MAX_LINE_LENGTH - 3] + "..." return line def _clean_output(output: str) -> str: - cleaned = output + cleaned = Text.from_ansi(output).plain.translate(_CONTROL_BYTES_TO_DROP) for pattern in STRIP_PATTERNS: cleaned = re.sub(pattern, "", cleaned, flags=re.MULTILINE)