Skip to content

Commit dd21124

Browse files
authored
Merge pull request microsoft#165634 from rwe/serialize-message-ps1
shellIntegration.ps1: escape values in "E" (executed command) and "P" (property KV) codes
2 parents 387bab6 + 91f0eae commit dd21124

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/vs/workbench/contrib/terminal/browser/media/shellIntegration.ps1

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,22 @@ if ($ExecutionContext.SessionState.LanguageMode -ne "FullLanguage") {
1616
$Global:__VSCodeOriginalPrompt = $function:Prompt
1717

1818
$Global:__LastHistoryId = -1
19-
function Set-Serialized {
20-
param ([string] $toSerialize)
21-
$toSerialize = $toSerialize.Replace("\", "\\").Replace("`n", "\x0a").Replace(";", "\x3b")
22-
return $toSerialize
19+
20+
function Global:__VSCode-Escape-Value([string]$value) {
21+
# NOTE: In PowerShell v6.1+, this can be written `$value -replace '…', { … }` instead of `[regex]::Replace`.
22+
# Replace any non-alphanumeric characters.
23+
[regex]::Replace($value, '[^a-zA-Z0-9]', { param($match)
24+
# Backslashes must be doubled.
25+
if ($match.Value -eq '\') {
26+
'\\'
27+
} else {
28+
# Any other characters are encoded as their UTF-8 hex values.
29+
-Join (
30+
[System.Text.Encoding]::UTF8.GetBytes($match.Value)
31+
| ForEach-Object { '\x{0:x2}' -f $_ }
32+
)
33+
}
34+
})
2335
}
2436

2537
function Global:Prompt() {
@@ -43,7 +55,7 @@ function Global:Prompt() {
4355
} else {
4456
$CommandLine = ""
4557
}
46-
$Result += Set-Serialized($CommandLine)
58+
$Result += $(__VSCode-Escape-Value $CommandLine)
4759
$Result += "`a"
4860
# Command finished exit code
4961
# OSC 633 ; D [; <ExitCode>] ST
@@ -55,7 +67,7 @@ function Global:Prompt() {
5567
$Result += "$([char]0x1b)]633;A`a"
5668
# Current working directory
5769
# OSC 633 ; <Property>=<Value> ST
58-
$Result += if($pwd.Provider.Name -eq 'FileSystem'){"$([char]0x1b)]633;P;Cwd=$($pwd.ProviderPath)`a"}
70+
$Result += if($pwd.Provider.Name -eq 'FileSystem'){"$([char]0x1b)]633;P;Cwd=$(__VSCode-Escape-Value $pwd.ProviderPath)`a"}
5971
# Before running the original prompt, put $? back to what it was:
6072
if ($FakeCode -ne 0) {
6173
Write-Error "failure" -ea ignore

0 commit comments

Comments
 (0)