Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions sgpt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,18 @@ def run_command(command: str) -> None:
It is aware of the current user's $SHELL.
:param command: A shell command to run.
"""
if platform.system() == "Windows":
if platform.system() == "Windows":
# Check if pwsh is available
pwsh_available = subprocess.call("pwsh -Command exit", shell=True) == 0

# Determine if PowerShell is the user's shell
is_powershell = len(os.getenv("PSModulePath", "").split(os.pathsep)) >= 3
full_command = (
f'powershell.exe -Command "{command}"'
if is_powershell
else f'cmd.exe /c "{command}"'
)

# Choose the PowerShell command based on availability and user's shell
powershell_or_cmd_cmd = "pwsh.exe -Command" if pwsh_available else ("powershell.exe -Command" if is_powershell else "cmd.exe /c")

# Construct the full command
full_command = f'{powershell_or_cmd_cmd} "{command}"'
else:
shell = os.environ.get("SHELL", "/bin/sh")
full_command = f"{shell} -c {shlex.quote(command)}"
Expand Down