feat: add verbose argument to run_cmd function

This commit is contained in:
Paul Gauthier (aider) 2024-08-31 12:01:30 -07:00
parent ef38960b0c
commit 95f66431bc

View file

@ -7,16 +7,16 @@ from io import BytesIO
import pexpect import pexpect
def run_cmd(command): def run_cmd(command, verbose=False):
import sys import sys
if sys.stdin.isatty() and hasattr(pexpect, "spawn") and platform.system() != "Windows": if sys.stdin.isatty() and hasattr(pexpect, "spawn") and platform.system() != "Windows":
return run_cmd_pexpect(command) return run_cmd_pexpect(command, verbose)
return run_cmd_subprocess(command) return run_cmd_subprocess(command, verbose)
def run_cmd_subprocess(command): def run_cmd_subprocess(command, verbose=False):
try: try:
process = subprocess.Popen( process = subprocess.Popen(
command, command,
@ -32,7 +32,8 @@ def run_cmd_subprocess(command):
output = [] output = []
for line in process.stdout: for line in process.stdout:
print(line, end="") # Print the line in real-time if verbose:
print(line, end="") # Print the line in real-time only if verbose is True
output.append(line) # Store the line for later use output.append(line) # Store the line for later use
process.wait() process.wait()
@ -41,11 +42,12 @@ def run_cmd_subprocess(command):
return 1, str(e) return 1, str(e)
def run_cmd_pexpect(command): def run_cmd_pexpect(command, verbose=False):
""" """
Run a shell command interactively using pexpect, capturing all output. Run a shell command interactively using pexpect, capturing all output.
:param command: The command to run as a string. :param command: The command to run as a string.
:param verbose: If True, print output in real-time.
:return: A tuple containing (exit_status, output) :return: A tuple containing (exit_status, output)
""" """
import pexpect import pexpect
@ -54,6 +56,8 @@ def run_cmd_pexpect(command):
def output_callback(b): def output_callback(b):
output.write(b) output.write(b)
if verbose:
print(b.decode("utf-8", errors="replace"), end="", flush=True)
return b return b
try: try: