@@ -16,10 +16,22 @@ if ($ExecutionContext.SessionState.LanguageMode -ne "FullLanguage") {
16
16
$Global :__VSCodeOriginalPrompt = $function: Prompt
17
17
18
18
$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
+ })
23
35
}
24
36
25
37
function Global :Prompt () {
@@ -43,7 +55,7 @@ function Global:Prompt() {
43
55
} else {
44
56
$CommandLine = " "
45
57
}
46
- $Result += Set-Serialized ( $CommandLine )
58
+ $Result += $ (__VSCode - Escape - Value $CommandLine )
47
59
$Result += " `a "
48
60
# Command finished exit code
49
61
# OSC 633 ; D [; <ExitCode>] ST
@@ -55,7 +67,7 @@ function Global:Prompt() {
55
67
$Result += " $ ( [char ]0x1b ) ]633;A`a "
56
68
# Current working directory
57
69
# 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 " }
59
71
# Before running the original prompt, put $? back to what it was:
60
72
if ($FakeCode -ne 0 ) {
61
73
Write-Error " failure" - ea ignore
0 commit comments