Skip to content

Commit 8f8a574

Browse files
committed
Run recent command: Collapse $HOME into ~
Fixes microsoft#153109
1 parent c5a9280 commit 8f8a574

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/vs/platform/terminal/common/terminalEnvironment.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,22 @@ export function escapeNonWindowsPath(path: string): string {
1212
newPath = newPath.replace(bannedChars, '');
1313
return `'${newPath}'`;
1414
}
15+
16+
/**
17+
* Collapses the user's home directory into `~` if it exists within the path, this gives a shorter
18+
* path that is more suitable within the context of a terminal.
19+
*/
20+
export function getTildePath(path: string | undefined, userHome: string | undefined, separator: string): string {
21+
if (!path) {
22+
return '';
23+
}
24+
if (!userHome) {
25+
return path;
26+
}
27+
const normalizedPath = path.replace(/\\/g, '/\//').toLowerCase();
28+
const normalizedUserHome = userHome.replace(/\\/g, '/\//').toLowerCase();
29+
if (!normalizedPath.includes(normalizedUserHome)) {
30+
return path;
31+
}
32+
return `~${separator}${path.slice(userHome.length)}`;
33+
}

src/vs/workbench/contrib/terminal/browser/terminalInstance.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
4747
import { ITerminalCommand, TerminalCapability } from 'vs/platform/terminal/common/capabilities/capabilities';
4848
import { TerminalCapabilityStoreMultiplexer } from 'vs/platform/terminal/common/capabilities/terminalCapabilityStore';
4949
import { IProcessDataEvent, IProcessPropertyMap, IShellLaunchConfig, ITerminalDimensionsOverride, ITerminalLaunchError, PosixShellType, ProcessPropertyType, TerminalIcon, TerminalLocation, TerminalSettingId, TerminalShellType, TitleEventSource, WindowsShellType } from 'vs/platform/terminal/common/terminal';
50-
import { escapeNonWindowsPath } from 'vs/platform/terminal/common/terminalEnvironment';
50+
import { escapeNonWindowsPath, getTildePath } from 'vs/platform/terminal/common/terminalEnvironment';
5151
import { activeContrastBorder, scrollbarSliderActiveBackground, scrollbarSliderBackground, scrollbarSliderHoverBackground } from 'vs/platform/theme/common/colorRegistry';
5252
import { IColorTheme, ICssStyleCollector, IThemeService, registerThemingParticipant, ThemeIcon } from 'vs/platform/theme/common/themeService';
5353
import { IWorkspaceContextService, IWorkspaceFolder } from 'vs/platform/workspace/common/workspace';
@@ -851,7 +851,7 @@ export class TerminalInstance extends Disposable implements ITerminalInstance {
851851
if (label.length === 0 || commandMap.has(label)) {
852852
continue;
853853
}
854-
let description = `${entry.cwd}`;
854+
let description = getTildePath(entry.cwd, this._userHome, this._processManager?.os === OperatingSystem.Windows ? '\\' : '/');
855855
if (entry.exitCode) {
856856
// Since you cannot get the last command's exit code on pwsh, just whether it failed
857857
// or not, -1 is treated specially as simply failed

0 commit comments

Comments
 (0)