feat: get native and powershell error code

Check native and powershell errors before sending it to oh-my-posh
This commit is contained in:
lnu 2020-11-18 08:06:28 +01:00 committed by Jan De Dobbeleer
parent 64f64d20c0
commit 1f3c253e45
2 changed files with 29 additions and 7 deletions

View file

@ -186,14 +186,22 @@ Edit `$PROFILE` in your preferred PowerShell version and add the following lines
```powershell ```powershell
[ScriptBlock]$Prompt = { [ScriptBlock]$Prompt = {
$lastCommandSuccess = $?
$realLASTEXITCODE = $global:LASTEXITCODE $realLASTEXITCODE = $global:LASTEXITCODE
if ($realLASTEXITCODE -isnot [int]) { $errorCode = 0
$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 = New-Object System.Diagnostics.ProcessStartInfo
$startInfo.FileName = "C:\tools\oh-my-posh.exe" $startInfo.FileName = "C:\tools\oh-my-posh.exe"
$cleanPWD = $PWD.ProviderPath.TrimEnd("\") $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.Environment["TERM"] = "xterm-256color"
$startInfo.CreateNoWindow = $true $startInfo.CreateNoWindow = $true
$startInfo.StandardOutputEncoding = [System.Text.Encoding]::UTF8 $startInfo.StandardOutputEncoding = [System.Text.Encoding]::UTF8
@ -209,7 +217,9 @@ Edit `$PROFILE` in your preferred PowerShell version and add the following lines
$process.WaitForExit() $process.WaitForExit()
$standardOut $standardOut
$global:LASTEXITCODE = $realLASTEXITCODE $global:LASTEXITCODE = $realLASTEXITCODE
#remove temp variables
Remove-Variable realLASTEXITCODE -Confirm:$false Remove-Variable realLASTEXITCODE -Confirm:$false
Remove-Variable lastCommandSuccess -Confirm:$false
} }
Set-Item -Path Function:prompt -Value $Prompt -Force Set-Item -Path Function:prompt -Value $Prompt -Force
``` ```

View file

@ -4,7 +4,7 @@
#> #>
$global:PoshSettings = New-Object -TypeName PSObject -Property @{ $global:PoshSettings = New-Object -TypeName PSObject -Property @{
Theme = "$PSScriptRoot\themes\jandedobbeleer.json" Theme = "$PSScriptRoot\themes\jandedobbeleer.json";
} }
function Get-PoshCommand { function Get-PoshCommand {
@ -50,16 +50,26 @@ function Set-PoshPrompt {
} }
[ScriptBlock]$Prompt = { [ScriptBlock]$Prompt = {
#store if the last command was successfull
$lastCommandSuccess = $?
#store the last exit code for restore
$realLASTEXITCODE = $global:LASTEXITCODE $realLASTEXITCODE = $global:LASTEXITCODE
$errorCode = 0
Set-PoshContext Set-PoshContext
if ($realLASTEXITCODE -isnot [int]) { if ($lastCommandSuccess -eq $false) {
$realLASTEXITCODE = 0 #native app exit code
if ($realLASTEXITCODE -ne 0) {
$errorCode = $realLASTEXITCODE
}
else {
$errorCode = 1
}
} }
$startInfo = New-Object System.Diagnostics.ProcessStartInfo $startInfo = New-Object System.Diagnostics.ProcessStartInfo
$startInfo.FileName = Get-PoshCommand $startInfo.FileName = Get-PoshCommand
$config = $global:PoshSettings.Theme $config = $global:PoshSettings.Theme
$cleanPWD = $PWD.ProviderPath.TrimEnd("\") $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.Environment["TERM"] = "xterm-256color"
$startInfo.CreateNoWindow = $true $startInfo.CreateNoWindow = $true
$startInfo.StandardOutputEncoding = [System.Text.Encoding]::UTF8 $startInfo.StandardOutputEncoding = [System.Text.Encoding]::UTF8
@ -75,7 +85,9 @@ function Set-PoshPrompt {
$process.WaitForExit() $process.WaitForExit()
$standardOut $standardOut
$global:LASTEXITCODE = $realLASTEXITCODE $global:LASTEXITCODE = $realLASTEXITCODE
#remove temp variables
Remove-Variable realLASTEXITCODE -Confirm:$false Remove-Variable realLASTEXITCODE -Confirm:$false
Remove-Variable lastCommandSuccess -Confirm:$false
Set-GitStatus Set-GitStatus
} }
Set-Item -Path Function:prompt -Value $Prompt -Force Set-Item -Path Function:prompt -Value $Prompt -Force