@@ -112,15 +112,19 @@ function enableAutoSwitch(enable) {
112112 require ( './postScript' ) . generate ( null , {
113113 '.PS1' : [
114114 // Patch the function that is invoked every time PowerShell shows a prompt.
115- // Export the function from the script using a dynamic module; this
116- // does NOT require the script to be sourced.
117- 'if (-not $env:NVS_ORIGINAL_PROMPT) { ' ,
118- ' $env:NVS_ORIGINAL_PROMPT = $(Get-Content function:\\prompt)' ,
115+ // This does NOT require the script to be sourced.
116+ 'if (-not $global:NVS_ORIGINAL_PROMPT) {' ,
117+ ' $global:NVS_ORIGINAL_PROMPT = $Function:prompt' ,
118+ '}' ,
119+ 'function global:prompt {' ,
120+ ' # We have to do this so a prompt customization tool (like Oh My Posh or Starship) can get' ,
121+ ' # the correct last command execution status and native command return code.' ,
122+ ' $global:NVS_ORIGINAL_LASTEXECUTIONSTATUS = $?' ,
123+ ' $originalExitCode = $global:LASTEXITCODE' ,
124+ ' . "' + psScriptFile + '" "prompt"' ,
125+ ' $global:LASTEXITCODE = $originalExitCode' ,
126+ ' $global:NVS_ORIGINAL_PROMPT.Invoke()' ,
119127 '}' ,
120- 'New-Module -Script {' ,
121- ' function prompt { . "' + psScriptFile + '" "prompt" }' ,
122- ' Export-ModuleMember -Function prompt' ,
123- '} > $null' ,
124128 ] ,
125129 '.SH' : [
126130 'function cd () { builtin cd "$@" && nvs cd; }' ,
@@ -131,11 +135,9 @@ function enableAutoSwitch(enable) {
131135 } else {
132136 require ( './postScript' ) . generate ( null , {
133137 '.PS1' : [
134- 'if ($env:NVS_ORIGINAL_PROMPT) { ' ,
135- ' New-Module -Script {' ,
136- ' function prompt { Invoke-Expression $env:NVS_ORIGINAL_PROMPT }' ,
137- ' Export-ModuleMember -Function prompt' ,
138- ' } > $null' ,
138+ 'if ($global:NVS_ORIGINAL_PROMPT) {' ,
139+ ' $Function:prompt = $global:NVS_ORIGINAL_PROMPT' ,
140+ ' Remove-Variable -Name @("NVS_ORIGINAL_PROMPT", "NVS_ORIGINAL_LASTEXECUTIONSTATUS") -Scope global' ,
139141 '}' ,
140142 ] ,
141143 '.SH' : [
0 commit comments