@@ -16,30 +16,41 @@ if ($ExecutionContext.SessionState.LanguageMode -ne "FullLanguage") {
16
16
$Global :__VSCodeOriginalPrompt = $function: Prompt
17
17
18
18
$Global :__LastHistoryId = -1
19
+ function Set-Serialized
20
+ {
21
+ param ([string ] $toSerialize )
22
+ $toSerialize = $toSerialize.Replace (" \" , " \\" ).Replace(" `n " , " \x0a" ).Replace(" ;" , " \x3b" )
23
+ return $toSerialize
24
+ }
19
25
20
-
21
- function Global : Prompt () {
26
+ function Global : Prompt ()
27
+ {
22
28
$FakeCode = [int ]! $global :?
23
29
$LastHistoryEntry = Get-History - Count 1
24
30
# Skip finishing the command if the first command has not yet started
25
- if ($Global :__LastHistoryId -ne -1 ) {
26
- if ($LastHistoryEntry.Id -eq $Global :__LastHistoryId ) {
31
+ if ($Global :__LastHistoryId -ne -1 )
32
+ {
33
+ if ($LastHistoryEntry.Id -eq $Global :__LastHistoryId )
34
+ {
27
35
# Don't provide a command line or exit code if there was no history entry (eg. ctrl+c, enter on no command)
28
36
$Result = " `e ]633;E`a "
29
37
$Result += " `e ]633;D`a "
30
- } else {
38
+ } else
39
+ {
31
40
# Command finished command line
32
41
# OSC 633 ; A ; <CommandLine?> ST
33
42
$Result = " `e ]633;E;"
34
43
# Sanitize the command line to ensure it can get transferred to the terminal and can be parsed
35
44
# correctly. This isn't entirely safe but good for most cases, it's important for the Pt parameter
36
45
# to only be composed of _printable_ characters as per the spec.
37
- if ($LastHistoryEntry.CommandLine ) {
46
+ if ($LastHistoryEntry.CommandLine )
47
+ {
38
48
$CommandLine = $LastHistoryEntry.CommandLine
39
- } else {
49
+ } else
50
+ {
40
51
$CommandLine = " "
41
52
}
42
- $Result += $CommandLine.Replace ( " \ " , " \\ " ).Replace( " `n " , " \x0a " ).Replace( " ; " , " \x3b " )
53
+ $Result += Set-Serialized ( $CommandLine )
43
54
$Result += " `a "
44
55
# Command finished exit code
45
56
# OSC 633 ; D [; <ExitCode>] ST
@@ -51,9 +62,13 @@ function Global:Prompt() {
51
62
$Result += " `e ]633;A`a "
52
63
# Current working directory
53
64
# OSC 633 ; <Property>=<Value> ST
54
- $Result += if ($pwd.Provider.Name -eq ' FileSystem' ){" `e ]633;P;Cwd=$ ( $pwd.ProviderPath ) `a " }
65
+ $Result += if ($pwd.Provider.Name -eq ' FileSystem' )
66
+ {" `e ]633;P;Cwd=$ ( $pwd.ProviderPath ) `a "
67
+ }
55
68
# Before running the original prompt, put $? back to what it was:
56
- if ($FakeCode -ne 0 ) { Write-Error " failure" - ea ignore }
69
+ if ($FakeCode -ne 0 )
70
+ { Write-Error " failure" - ea ignore
71
+ }
57
72
# Run the original prompt
58
73
$Result += $Global :__VSCodeOriginalPrompt.Invoke ()
59
74
# Write command started
@@ -64,9 +79,11 @@ function Global:Prompt() {
64
79
65
80
# Only send the command executed sequence when PSReadLine is loaded, if not shell integration should
66
81
# still work thanks to the command line sequence
67
- if (Get-Module - Name PSReadLine) {
82
+ if (Get-Module - Name PSReadLine)
83
+ {
68
84
$__VSCodeOriginalPSConsoleHostReadLine = $function: PSConsoleHostReadLine
69
- function Global :PSConsoleHostReadLine {
85
+ function Global :PSConsoleHostReadLine
86
+ {
70
87
$tmp = $__VSCodeOriginalPSConsoleHostReadLine.Invoke ()
71
88
# Write command executed sequence directly to Console to avoid the new line from Write-Host
72
89
[Console ]::Write(" `e ]633;C`a " )
@@ -78,21 +95,27 @@ if (Get-Module -Name PSReadLine) {
78
95
[Console ]::Write(" `e ]633;P;IsWindows=$ ( $IsWindows ) `a " )
79
96
80
97
# Set always on key handlers which map to default VS Code keybindings
81
- function Set-MappedKeyHandler {
98
+ function Set-MappedKeyHandler
99
+ {
82
100
param ([string []] $Chord , [string []]$Sequence )
83
101
$Handler = $ (Get-PSReadLineKeyHandler - Chord $Chord | Select-Object - First 1 )
84
- if ($Handler ) {
102
+ if ($Handler )
103
+ {
85
104
Set-PSReadLineKeyHandler - Chord $Sequence - Function $Handler.Function
86
105
}
87
106
}
88
- function Set-MappedKeyHandlers {
107
+ function Set-MappedKeyHandlers
108
+ {
89
109
Set-MappedKeyHandler - Chord Ctrl+ Spacebar - Sequence ' F12,a'
90
110
Set-MappedKeyHandler - Chord Alt+ Spacebar - Sequence ' F12,b'
91
111
Set-MappedKeyHandler - Chord Shift+ Enter - Sequence ' F12,c'
92
112
Set-MappedKeyHandler - Chord Shift+ End - Sequence ' F12,d'
93
113
}
114
+
94
115
Set-MappedKeyHandlers
95
116
$result = $ (Get-Alias | Select-Object Name, Definition)
96
117
$resultStr = Out-String - InputObject $result
97
- $resultStr = $resultStr.Replace ( " \ " , " \\ " ).Replace( " `n " , " \x0a " ).Replace( " ; " , " \x3b " )
118
+ $resultStr = Set-Serialized ( $resultStr )
98
119
[Console ]::Write(" `e ]633;P;UserAliases=$resultStr `a " )
120
+
121
+
0 commit comments