From 15521c41d1c3d6a96a9d57ec7c2bd3e61091625c Mon Sep 17 00:00:00 2001 From: "Paul Gauthier (aider)" Date: Tue, 27 Aug 2024 09:02:21 -0700 Subject: [PATCH] feat: Introduce `run_interactive_command` that uses `pexpect` if available, otherwise falls back to `subprocess` --- aider/utils.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/aider/utils.py b/aider/utils.py index f06765dec..bcb827bdf 100644 --- a/aider/utils.py +++ b/aider/utils.py @@ -8,13 +8,36 @@ from io import BytesIO from pathlib import Path import git -import pexpect from aider.dump import dump # noqa: F401 IMAGE_EXTENSIONS = {".png", ".jpg", ".jpeg", ".gif", ".bmp", ".tiff", ".webp"} +def run_interactive_command(command): + try: + import pexpect + return run_interactive_command_pexpect(command) + except ImportError: + return run_interactive_command_subprocess(command) + + +def run_interactive_command_subprocess(command): + try: + result = subprocess.run( + command, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + text=True, + shell=True, + encoding=sys.stdout.encoding, + errors="replace" + ) + return result.returncode, result.stdout + except Exception as e: + return 1, str(e) + + def run_interactive_command_pexpect(command): """ Run a shell command interactively using pexpect, capturing all output.