diff --git a/docs/docs/installation.mdx b/docs/docs/installation.mdx index d67662ca..bbe4e2bb 100644 --- a/docs/docs/installation.mdx +++ b/docs/docs/installation.mdx @@ -186,14 +186,22 @@ Edit `$PROFILE` in your preferred PowerShell version and add the following lines ```powershell [ScriptBlock]$Prompt = { + $lastCommandSuccess = $? $realLASTEXITCODE = $global:LASTEXITCODE - if ($realLASTEXITCODE -isnot [int]) { - $realLASTEXITCODE = 0 + $errorCode = 0 + if ($lastCommandSuccess -eq $false) { + #native app exit code + if ($realLASTEXITCODE -ne 0) { + $errorCode = $realLASTEXITCODE + } + else { + $errorCode = 1 + } } $startInfo = New-Object System.Diagnostics.ProcessStartInfo $startInfo.FileName = "C:\tools\oh-my-posh.exe" $cleanPWD = $PWD.ProviderPath.TrimEnd("\") - $startInfo.Arguments = "-config=""C:\Users\User\.poshthemes\jandedobbeleer.json"" -error=$realLASTEXITCODE -pwd=""$cleanPWD""" + $startInfo.Arguments = "-config=""C:\Users\User\.poshthemes\jandedobbeleer.json"" -error=$errorCode -pwd=""$cleanPWD""" $startInfo.Environment["TERM"] = "xterm-256color" $startInfo.CreateNoWindow = $true $startInfo.StandardOutputEncoding = [System.Text.Encoding]::UTF8 @@ -209,7 +217,9 @@ Edit `$PROFILE` in your preferred PowerShell version and add the following lines $process.WaitForExit() $standardOut $global:LASTEXITCODE = $realLASTEXITCODE + #remove temp variables Remove-Variable realLASTEXITCODE -Confirm:$false + Remove-Variable lastCommandSuccess -Confirm:$false } Set-Item -Path Function:prompt -Value $Prompt -Force ``` diff --git a/packages/powershell/oh-my-posh/oh-my-posh.psm1 b/packages/powershell/oh-my-posh/oh-my-posh.psm1 index 9e686fa5..9234fc64 100644 --- a/packages/powershell/oh-my-posh/oh-my-posh.psm1 +++ b/packages/powershell/oh-my-posh/oh-my-posh.psm1 @@ -4,7 +4,7 @@ #> $global:PoshSettings = New-Object -TypeName PSObject -Property @{ - Theme = "$PSScriptRoot\themes\jandedobbeleer.json" + Theme = "$PSScriptRoot\themes\jandedobbeleer.json"; } function Get-PoshCommand { @@ -50,16 +50,26 @@ function Set-PoshPrompt { } [ScriptBlock]$Prompt = { + #store if the last command was successfull + $lastCommandSuccess = $? + #store the last exit code for restore $realLASTEXITCODE = $global:LASTEXITCODE + $errorCode = 0 Set-PoshContext - if ($realLASTEXITCODE -isnot [int]) { - $realLASTEXITCODE = 0 + if ($lastCommandSuccess -eq $false) { + #native app exit code + if ($realLASTEXITCODE -ne 0) { + $errorCode = $realLASTEXITCODE + } + else { + $errorCode = 1 + } } $startInfo = New-Object System.Diagnostics.ProcessStartInfo $startInfo.FileName = Get-PoshCommand $config = $global:PoshSettings.Theme $cleanPWD = $PWD.ProviderPath.TrimEnd("\") - $startInfo.Arguments = "-config=""$config"" -error=$realLASTEXITCODE -pwd=""$cleanPWD""" + $startInfo.Arguments = "-config=""$config"" -error=$errorCode -pwd=""$cleanPWD""" $startInfo.Environment["TERM"] = "xterm-256color" $startInfo.CreateNoWindow = $true $startInfo.StandardOutputEncoding = [System.Text.Encoding]::UTF8 @@ -75,7 +85,9 @@ function Set-PoshPrompt { $process.WaitForExit() $standardOut $global:LASTEXITCODE = $realLASTEXITCODE + #remove temp variables Remove-Variable realLASTEXITCODE -Confirm:$false + Remove-Variable lastCommandSuccess -Confirm:$false Set-GitStatus } Set-Item -Path Function:prompt -Value $Prompt -Force