refactor: Simplify terminal event processing in redact-cast.py

This commit is contained in:
Paul Gauthier 2025-03-14 13:34:21 -07:00 committed by Paul Gauthier (aider)
parent f1d00dbd7f
commit 7ac7c72e03

View file

@ -7,6 +7,8 @@ import sys
import pyte import pyte
from tqdm import tqdm from tqdm import tqdm
from aider.dump import dump # noqa
def main(): def main():
if len(sys.argv) != 3: if len(sys.argv) != 3:
@ -30,29 +32,14 @@ def main():
height = header_data.get("height", 24) height = header_data.get("height", 24)
print(f"Terminal dimensions: {width}x{height}") print(f"Terminal dimensions: {width}x{height}")
# Track if we need to check the terminal (if "Atuin" might be on screen) screen = pyte.Screen(width, height)
stream = pyte.Stream(screen)
atuin_pattern = re.compile(r"A.*t.*u.*i.*n")
screen = stream = None
# Process events line by line # Process events line by line
for line in tqdm(fin, desc="Processing events", total=total_lines - 1): for line in tqdm(fin, desc="Processing events", total=total_lines - 1):
if not line.strip(): if not line.strip():
continue continue
# Fast initial check on raw line before JSON parsing
raw_line_has_atuin = bool(atuin_pattern.search(line))
if raw_line_has_atuin:
screen = pyte.Screen(width, height)
stream = pyte.Stream(screen)
# Only parse JSON if we're checking terminal or need to check
if not screen:
fout.write(line)
continue
event = json.loads(line) event = json.loads(line)
if not (len(event) >= 3 and event[1] == "o"): if not (len(event) >= 3 and event[1] == "o"):
@ -60,18 +47,16 @@ def main():
continue continue
output_text = event[2] output_text = event[2]
stream.feed(output_text) stream.feed(output_text)
# Check if "Atuin" is visible on screen # Check if "Atuin" is visible on screen
atuin_visible = False atuin_visible = False
for display_line in screen.display: for display_line in screen.display:
if "Atuin" in "".join(display_line): if "Atuin" in display_line or "[ GLOBAL ]" in display_line:
atuin_visible = True atuin_visible = True
break break
if not atuin_visible: if not atuin_visible:
screen = stream = None
fout.write(line) fout.write(line)