Skip to content

Commit

Permalink
Merge pull request #137 from skogsbaer/improve-powershell-detection
Browse files Browse the repository at this point in the history
Always use cmd.exe on Windows
  • Loading branch information
skogsbaer authored Sep 25, 2024
2 parents a53cd17 + 1b8fa57 commit 9739138
Showing 1 changed file with 6 additions and 54 deletions.
60 changes: 6 additions & 54 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,73 +39,25 @@ function showButtons() {
});
}

function getTerminalConfig(): {shellPath: string | undefined, shellArgs: string | string[] | undefined} {
const shellConfig = vscode.workspace.getConfiguration('terminal.integrated.shell');
const shellArgsConfig = vscode.workspace.getConfiguration('terminal.integrated.shellArgs');
let osSection: string | undefined;
if (process.platform === "win32") {
osSection = 'windows';
} else if (process.platform === "darwin") {
osSection = 'osx';
} else if (process.platform === "linux") {
osSection = 'linux';
}
if (osSection) {
return {
shellPath: shellConfig.get<string>(osSection) || undefined,
shellArgs: shellArgsConfig.get<string | string[]>(osSection) || undefined
};
} else {
return {
shellPath: undefined,
shellArgs: undefined
};
}
}

const POWERSHELLS = [/(powershell.exe$|powershell$)/i, /(pwsh.exe$|pwsh$)/i];

type ShellKind = 'powershell' | 'other';

function identifyShell(shellPath: string): ShellKind {
for (let pat of POWERSHELLS) {
if (pat.test(shellPath)) {
return 'powershell';
}
}
return 'other';
}

async function startTerminal(
existing: vscode.Terminal | undefined, name: string, cmd: string
): Promise<vscode.Terminal> {
if (existing) {
existing.dispose();
}
const terminalOptions: vscode.TerminalOptions = {name: name};
let cmdPrefix = "";
if (isWindows) {
const shellCfg = getTerminalConfig();
const shellPath = shellCfg.shellPath;
const shellArgs = shellCfg.shellArgs;
if (shellPath) {
terminalOptions.shellPath = shellPath;
if (shellArgs) {
terminalOptions.shellArgs = shellArgs;
}
if (identifyShell(shellPath) === "powershell") {
cmdPrefix = "& ";
}
} else {
// powershell is the default
cmdPrefix = "& ";
}
// We don't know which shell will be used by default.
// If PowerShell is the default, we need to prefix the command with "& ".
// Otherwise, the prefix is not allowed and results in a syntax error.
// -> Just force cmd.exe.
terminalOptions.shellPath = "cmd.exe";
}
const terminal = vscode.window.createTerminal(terminalOptions);
// Sometimes the terminal takes some time to start up before it can start accepting input.
await new Promise((resolve) => setTimeout(resolve, 100));
terminal.show(false); // focus the terminal
terminal.sendText(cmdPrefix + cmd);
terminal.sendText(cmd);
return terminal;
}

Expand Down

0 comments on commit 9739138

Please sign in to comment.