35 lines
942 B
Python
35 lines
942 B
Python
"""Shell operation utilities for nanocode."""
|
|
|
|
import subprocess
|
|
from .display import DIM, RESET
|
|
|
|
|
|
def run_bash(cmd: str) -> str:
|
|
"""Run a shell command and return output.
|
|
|
|
Args:
|
|
cmd: Shell command to execute
|
|
|
|
Returns:
|
|
Command output (stdout and stderr combined)
|
|
"""
|
|
proc = subprocess.Popen(
|
|
cmd, shell=True,
|
|
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
|
|
text=True
|
|
)
|
|
output_lines = []
|
|
try:
|
|
while True:
|
|
line = proc.stdout.readline()
|
|
if not line and proc.poll() is not None:
|
|
break
|
|
if line:
|
|
print(f" {DIM}│ {line.rstrip()}{RESET}", flush=True)
|
|
output_lines.append(line)
|
|
proc.wait(timeout=30)
|
|
except subprocess.TimeoutExpired:
|
|
proc.kill()
|
|
output_lines.append("\n(timed out after 30s)")
|
|
return "".join(output_lines).strip() or "(empty output)"
|