refactor: improve subprocess output handling for real-time display

This commit is contained in:
Paul Gauthier (aider) 2024-09-10 09:34:58 -07:00
parent 625a49752b
commit ab9d9c8429

View file

@ -50,7 +50,6 @@ def run_cmd_subprocess(command, verbose=False):
parent_process = get_windows_parent_process_name() parent_process = get_windows_parent_process_name()
if parent_process == "powershell.exe": if parent_process == "powershell.exe":
command = f"powershell -Command {command}" command = f"powershell -Command {command}"
# else: Assume cmd.exe or other Windows shell, use the command as-is
if verbose: if verbose:
print("Running command:", command) print("Running command:", command)
@ -66,14 +65,17 @@ def run_cmd_subprocess(command, verbose=False):
shell=True, shell=True,
encoding=sys.stdout.encoding, encoding=sys.stdout.encoding,
errors="replace", errors="replace",
bufsize=1, bufsize=0, # Set bufsize to 0 for unbuffered output
universal_newlines=True, universal_newlines=True,
) )
output = [] output = []
for line in process.stdout: while True:
print(line, end="") # Print the line in real-time chunk = process.stdout.read(1024) # Read in 1KB chunks
output.append(line) # Store the line for later use if not chunk:
break
print(chunk, end="", flush=True) # Print the chunk in real-time
output.append(chunk) # Store the chunk for later use
process.wait() process.wait()
return process.returncode, "".join(output) return process.returncode, "".join(output)