diff options
Diffstat (limited to 'src/vs/platform/terminal/common/terminalEnvironment.ts')
-rw-r--r-- | src/vs/platform/terminal/common/terminalEnvironment.ts | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/vs/platform/terminal/common/terminalEnvironment.ts b/src/vs/platform/terminal/common/terminalEnvironment.ts index 66b5ad31a3f..1d24a24f60d 100644 --- a/src/vs/platform/terminal/common/terminalEnvironment.ts +++ b/src/vs/platform/terminal/common/terminalEnvironment.ts @@ -12,3 +12,26 @@ export function escapeNonWindowsPath(path: string): string { newPath = newPath.replace(bannedChars, ''); return `'${newPath}'`; } + +/** + * Collapses the user's home directory into `~` if it exists within the path, this gives a shorter + * path that is more suitable within the context of a terminal. + */ +export function collapseTildePath(path: string | undefined, userHome: string | undefined, separator: string): string { + if (!path) { + return ''; + } + if (!userHome) { + return path; + } + // Trim the trailing separator from the end if it exists + if (userHome.match(/[\/\\]$/)) { + userHome = userHome.slice(0, userHome.length - 1); + } + const normalizedPath = path.replace(/\\/g, '/').toLowerCase(); + const normalizedUserHome = userHome.replace(/\\/g, '/').toLowerCase(); + if (!normalizedPath.includes(normalizedUserHome)) { + return path; + } + return `~${separator}${path.slice(userHome.length + 1)}`; +} |