4
4
# ---------------------------------------------------------------------------------------------
5
5
6
6
# Prevent installing more than once per session
7
- if (Test-Path variable:global:__VSCodeOriginalPrompt ) {
7
+ if (Test-Path variable:global:__VSCodeState.OriginalPrompt ) {
8
8
return ;
9
9
}
10
10
@@ -13,27 +13,33 @@ if ($ExecutionContext.SessionState.LanguageMode -ne "FullLanguage") {
13
13
return ;
14
14
}
15
15
16
- $Global :__VSCodeOriginalPrompt = $function: Prompt
17
-
18
- $Global :__LastHistoryId = -1
19
- $Global :__VSCodeIsInExecution = $false
16
+ $Global :__VSCodeState = @ {
17
+ OriginalPrompt = $function: Prompt
18
+ LastHistoryId = -1
19
+ IsInExecution = $false
20
+ EnvVarsToReport = @ ()
21
+ Nonce = $null
22
+ IsStable = $null
23
+ IsWindows10 = $false
24
+ }
20
25
21
26
# Store the nonce in script scope and unset the global
22
- $Nonce = $env: VSCODE_NONCE
27
+ $Global :__VSCodeState . Nonce = $env: VSCODE_NONCE
23
28
$env: VSCODE_NONCE = $null
24
29
25
- $isStable = $env: VSCODE_STABLE
30
+ $Global :__VSCodeState .IsStable = $env: VSCODE_STABLE
26
31
$env: VSCODE_STABLE = $null
27
32
28
33
$__vscode_shell_env_reporting = $env: VSCODE_SHELL_ENV_REPORTING
29
34
$env: VSCODE_SHELL_ENV_REPORTING = $null
30
- $Global :envVarsToReport = @ ()
31
35
if ($__vscode_shell_env_reporting ) {
32
- $Global :envVarsToReport = $__vscode_shell_env_reporting.Split (' ,' )
36
+ $Global :__VSCodeState .EnvVarsToReport = $__vscode_shell_env_reporting.Split (' ,' )
33
37
}
38
+ Remove-Variable - Name __vscode_shell_env_reporting - ErrorAction SilentlyContinue
34
39
35
40
$osVersion = [System.Environment ]::OSVersion.Version
36
- $isWindows10 = $IsWindows -and $osVersion.Major -eq 10 -and $osVersion.Minor -eq 0 -and $osVersion.Build -lt 22000
41
+ $Global :__VSCodeState.IsWindows10 = $IsWindows -and $osVersion.Major -eq 10 -and $osVersion.Minor -eq 0 -and $osVersion.Build -lt 22000
42
+ Remove-Variable - Name osVersion - ErrorAction SilentlyContinue
37
43
38
44
if ($env: VSCODE_ENV_REPLACE ) {
39
45
$Split = $env: VSCODE_ENV_REPLACE.Split (" :" )
@@ -80,9 +86,9 @@ function Global:Prompt() {
80
86
$Result = " "
81
87
# Skip finishing the command if the first command has not yet started or an execution has not
82
88
# yet begun
83
- if ($Global :__LastHistoryId -ne -1 -and $Global :__VSCodeIsInExecution -eq $true ) {
84
- $Global :__VSCodeIsInExecution = $false
85
- if ($LastHistoryEntry.Id -eq $Global :__LastHistoryId ) {
89
+ if ($Global :__VSCodeState .LastHistoryId -ne -1 -and $Global :__VSCodeState .IsInExecution -eq $true ) {
90
+ $Global :__VSCodeState .IsInExecution = $false
91
+ if ($LastHistoryEntry.Id -eq $Global :__VSCodeState .LastHistoryId ) {
86
92
# Don't provide a command line or exit code if there was no history entry (eg. ctrl+c, enter on no command)
87
93
$Result += " $ ( [char ]0x1b ) ]633;D`a "
88
94
}
@@ -101,34 +107,34 @@ function Global:Prompt() {
101
107
102
108
# Send current environment variables as JSON
103
109
# OSC 633 ; EnvJson ; <Environment> ; <Nonce>
104
- if ($Global :envVarsToReport .Count -gt 0 ) {
110
+ if ($Global :__VSCodeState .EnvVarsToReport .Count -gt 0 ) {
105
111
$envMap = @ {}
106
- foreach ($varName in $envVarsToReport ) {
112
+ foreach ($varName in $Global :__VSCodeState .EnvVarsToReport ) {
107
113
if (Test-Path " env:$varName " ) {
108
114
$envMap [$varName ] = (Get-Item " env:$varName " ).Value
109
115
}
110
116
}
111
117
$envJson = $envMap | ConvertTo-Json - Compress
112
- $Result += " $ ( [char ]0x1b ) ]633;EnvJson;$ ( __VSCode- Escape- Value $envJson ) ;$Nonce `a "
118
+ $Result += " $ ( [char ]0x1b ) ]633;EnvJson;$ ( __VSCode- Escape- Value $envJson ) ;$ ( $ Global :__VSCodeState . Nonce) `a "
113
119
}
114
120
115
121
# Before running the original prompt, put $? back to what it was:
116
122
if ($FakeCode -ne 0 ) {
117
123
Write-Error " failure" - ea ignore
118
124
}
119
125
# Run the original prompt
120
- $OriginalPrompt += $Global :__VSCodeOriginalPrompt .Invoke ()
126
+ $OriginalPrompt += $Global :__VSCodeState .OriginalPrompt .Invoke ()
121
127
$Result += $OriginalPrompt
122
128
123
129
# Prompt
124
130
# OSC 633 ; <Property>=<Value> ST
125
- if ($isStable -eq " 0" ) {
131
+ if ($Global :__VSCodeState .IsStable -eq " 0" ) {
126
132
$Result += " $ ( [char ]0x1b ) ]633;P;Prompt=$ ( __VSCode- Escape- Value $OriginalPrompt ) `a "
127
133
}
128
134
129
135
# Write command started
130
136
$Result += " $ ( [char ]0x1b ) ]633;B`a "
131
- $Global :__LastHistoryId = $LastHistoryEntry.Id
137
+ $Global :__VSCodeState .LastHistoryId = $LastHistoryEntry.Id
132
138
return $Result
133
139
}
134
140
@@ -148,19 +154,19 @@ elseif ((Test-Path variable:global:GitPromptSettings) -and $Global:GitPromptSett
148
154
if (Get-Module - Name PSReadLine) {
149
155
[Console ]::Write(" $ ( [char ]0x1b ) ]633;P;HasRichCommandDetection=True`a " )
150
156
151
- $__VSCodeOriginalPSConsoleHostReadLine = $function: PSConsoleHostReadLine
157
+ $Global :__VSCodeState .OriginalPSConsoleHostReadLine = $function: PSConsoleHostReadLine
152
158
function Global :PSConsoleHostReadLine {
153
- $CommandLine = $__VSCodeOriginalPSConsoleHostReadLine .Invoke ()
154
- $Global :__VSCodeIsInExecution = $true
159
+ $CommandLine = $Global :__VSCodeState .OriginalPSConsoleHostReadLine .Invoke ()
160
+ $Global :__VSCodeState .IsInExecution = $true
155
161
156
162
# Command line
157
163
# OSC 633 ; E [; <CommandLine> [; <Nonce>]] ST
158
164
$Result = " $ ( [char ]0x1b ) ]633;E;"
159
165
$Result += $ (__VSCode- Escape- Value $CommandLine )
160
166
# Only send the nonce if the OS is not Windows 10 as it seems to echo to the terminal
161
167
# sometimes
162
- if ($IsWindows10 -eq $false ) {
163
- $Result += " ;$Nonce "
168
+ if ($Global :__VSCodeState . IsWindows10 -eq $false ) {
169
+ $Result += " ;$ ( $ Global :__VSCodeState . Nonce) "
164
170
}
165
171
$Result += " `a "
166
172
@@ -175,9 +181,9 @@ if (Get-Module -Name PSReadLine) {
175
181
}
176
182
177
183
# Set ContinuationPrompt property
178
- $ContinuationPrompt = (Get-PSReadLineOption ).ContinuationPrompt
179
- if ($ContinuationPrompt ) {
180
- [Console ]::Write(" $ ( [char ]0x1b ) ]633;P;ContinuationPrompt=$ ( __VSCode- Escape- Value $ContinuationPrompt ) `a " )
184
+ $Global :__VSCodeState . ContinuationPrompt = (Get-PSReadLineOption ).ContinuationPrompt
185
+ if ($Global :__VSCodeState . ContinuationPrompt ) {
186
+ [Console ]::Write(" $ ( [char ]0x1b ) ]633;P;ContinuationPrompt=$ ( __VSCode- Escape- Value $Global :__VSCodeState . ContinuationPrompt ) `a " )
181
187
}
182
188
}
183
189
0 commit comments