Skip to content

Commit 0033754

Browse files
committed
Simplify getShellCommandAsString
1 parent 979c0be commit 0033754

File tree

2 files changed

+28
-42
lines changed

2 files changed

+28
-42
lines changed

src/features/terminal/shells/common/shellUtils.ts

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,50 +10,36 @@ import { quoteArgs } from '../../../execution/execUtils';
1010
* - Zsh: When setopt HIST_IGNORE_SPACE is enabled
1111
* - Git Bash: Uses bash under the hood, same behavior as Bash
1212
*/
13-
export const shellsWithLeadingSpaceHistorySupport = [ShellConstants.BASH, ShellConstants.ZSH, ShellConstants.GITBASH];
13+
export const shellsWithLeadingSpaceHistorySupport = new Set([
14+
ShellConstants.BASH,
15+
ShellConstants.ZSH,
16+
ShellConstants.GITBASH,
17+
]);
1418

15-
function getCommandAsString(command: PythonCommandRunConfiguration[], shell: string, delimiter: string): string {
19+
const defaultShellDelimiter = '&&';
20+
const shellDelimiterByShell = new Map<string, string>([
21+
[ShellConstants.PWSH, ';'],
22+
[ShellConstants.NU, ';'],
23+
[ShellConstants.FISH, '; and'],
24+
]);
25+
26+
export function getShellCommandAsString(shell: string, command: PythonCommandRunConfiguration[]): string {
27+
const delimiter = shellDelimiterByShell.get(shell) ?? defaultShellDelimiter;
1628
const parts = [];
1729
for (const cmd of command) {
1830
const args = cmd.args ?? [];
1931
parts.push(quoteArgs([normalizeShellPath(cmd.executable, shell), ...args]).join(' '));
2032
}
21-
if (shell === ShellConstants.PWSH) {
22-
if (parts.length === 1) {
23-
return parts[0];
24-
}
25-
return parts.map((p) => `(${p})`).join(` ${delimiter} `);
26-
}
27-
return parts.join(` ${delimiter} `);
28-
}
2933

30-
export function getShellCommandAsString(shell: string, command: PythonCommandRunConfiguration[]): string {
31-
let commandStr: string;
32-
switch (shell) {
33-
case ShellConstants.PWSH:
34-
commandStr = getCommandAsString(command, shell, ';');
35-
break;
36-
case ShellConstants.NU:
37-
commandStr = getCommandAsString(command, shell, ';');
38-
break;
39-
case ShellConstants.FISH:
40-
commandStr = getCommandAsString(command, shell, '; and');
41-
break;
42-
case ShellConstants.BASH:
43-
case ShellConstants.SH:
44-
case ShellConstants.ZSH:
45-
46-
case ShellConstants.CMD:
47-
case ShellConstants.GITBASH:
48-
default:
49-
commandStr = getCommandAsString(command, shell, '&&');
50-
break;
34+
let commandStr = parts.join(` ${delimiter} `);
35+
if (shell === ShellConstants.PWSH && parts.length > 1) {
36+
commandStr = parts.map((p) => `(${p})`).join(` ${delimiter} `);
5137
}
5238

5339
// Add a leading space for shells that support history ignore with leading space.
5440
// This prevents the activation command from being saved in bash/zsh history
5541
// when HISTCONTROL=ignorespace (bash) or setopt HIST_IGNORE_SPACE (zsh) is set.
56-
if (shellsWithLeadingSpaceHistorySupport.includes(shell)) {
42+
if (shellsWithLeadingSpaceHistorySupport.has(shell)) {
5743
return ` ${commandStr}`;
5844
}
5945
return commandStr;

src/test/features/terminal/shells/common/shellUtils.unit.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import * as assert from 'assert';
2+
import { PythonCommandRunConfiguration } from '../../../../../api';
3+
import { ShellConstants } from '../../../../../features/common/shellConstants';
24
import {
35
extractProfilePath,
46
getShellCommandAsString,
57
PROFILE_TAG_END,
68
PROFILE_TAG_START,
79
shellsWithLeadingSpaceHistorySupport,
810
} from '../../../../../features/terminal/shells/common/shellUtils';
9-
import { ShellConstants } from '../../../../../features/common/shellConstants';
10-
import { PythonCommandRunConfiguration } from '../../../../../api';
1111

1212
suite('Shell Utils', () => {
1313
suite('extractProfilePath', () => {
@@ -84,17 +84,17 @@ suite('Shell Utils', () => {
8484

8585
suite('shellsWithLeadingSpaceHistorySupport', () => {
8686
test('should include bash, zsh, and gitbash', () => {
87-
assert.ok(shellsWithLeadingSpaceHistorySupport.includes(ShellConstants.BASH));
88-
assert.ok(shellsWithLeadingSpaceHistorySupport.includes(ShellConstants.ZSH));
89-
assert.ok(shellsWithLeadingSpaceHistorySupport.includes(ShellConstants.GITBASH));
87+
assert.ok(shellsWithLeadingSpaceHistorySupport.has(ShellConstants.BASH));
88+
assert.ok(shellsWithLeadingSpaceHistorySupport.has(ShellConstants.ZSH));
89+
assert.ok(shellsWithLeadingSpaceHistorySupport.has(ShellConstants.GITBASH));
9090
});
9191

9292
test('should not include shells without leading space history support', () => {
93-
assert.ok(!shellsWithLeadingSpaceHistorySupport.includes(ShellConstants.PWSH));
94-
assert.ok(!shellsWithLeadingSpaceHistorySupport.includes(ShellConstants.CMD));
95-
assert.ok(!shellsWithLeadingSpaceHistorySupport.includes(ShellConstants.FISH));
96-
assert.ok(!shellsWithLeadingSpaceHistorySupport.includes(ShellConstants.SH));
97-
assert.ok(!shellsWithLeadingSpaceHistorySupport.includes(ShellConstants.NU));
93+
assert.ok(!shellsWithLeadingSpaceHistorySupport.has(ShellConstants.PWSH));
94+
assert.ok(!shellsWithLeadingSpaceHistorySupport.has(ShellConstants.CMD));
95+
assert.ok(!shellsWithLeadingSpaceHistorySupport.has(ShellConstants.FISH));
96+
assert.ok(!shellsWithLeadingSpaceHistorySupport.has(ShellConstants.SH));
97+
assert.ok(!shellsWithLeadingSpaceHistorySupport.has(ShellConstants.NU));
9898
});
9999
});
100100

0 commit comments

Comments
 (0)