42 lines
1.2 KiB
Python
42 lines
1.2 KiB
Python
|
|
"""
|
||
|
|
Terminal control — runs PowerShell and CMD commands via subprocess.
|
||
|
|
Opens Windows Terminal at a given path.
|
||
|
|
"""
|
||
|
|
import subprocess
|
||
|
|
import os
|
||
|
|
|
||
|
|
def run_powershell(command: str, timeout: int = 30) -> dict:
|
||
|
|
"""Execute a PowerShell command and return stdout/stderr."""
|
||
|
|
result = subprocess.run(
|
||
|
|
["powershell", "-ExecutionPolicy", "Bypass", "-NonInteractive", "-Command", command],
|
||
|
|
capture_output=True, text=True, timeout=timeout
|
||
|
|
)
|
||
|
|
return {
|
||
|
|
"stdout": result.stdout.strip(),
|
||
|
|
"stderr": result.stderr.strip(),
|
||
|
|
"returncode": result.returncode
|
||
|
|
}
|
||
|
|
|
||
|
|
def run_cmd(command: str, timeout: int = 30) -> dict:
|
||
|
|
"""Execute a CMD command."""
|
||
|
|
result = subprocess.run(
|
||
|
|
["cmd", "/c", command],
|
||
|
|
capture_output=True, text=True, timeout=timeout
|
||
|
|
)
|
||
|
|
return {
|
||
|
|
"stdout": result.stdout.strip(),
|
||
|
|
"stderr": result.stderr.strip(),
|
||
|
|
"returncode": result.returncode
|
||
|
|
}
|
||
|
|
|
||
|
|
def open_terminal(path: str = None):
|
||
|
|
"""Open Windows Terminal at the given directory."""
|
||
|
|
cmd = ["wt"]
|
||
|
|
if path:
|
||
|
|
cmd += ["-d", path]
|
||
|
|
subprocess.Popen(cmd)
|
||
|
|
|
||
|
|
def open_vscode(path: str):
|
||
|
|
"""Open VS Code at a given path."""
|
||
|
|
subprocess.Popen(["code", path])
|