Skip to content

Commit

Permalink
feat: get native and powershell error code
Browse files Browse the repository at this point in the history
Check native and powershell errors before sending it to oh-my-posh
  • Loading branch information
lnu authored and JanDeDobbeleer committed Nov 18, 2020
1 parent 64f64d2 commit 1f3c253
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
16 changes: 13 additions & 3 deletions docs/docs/installation.mdx
Expand Up @@ -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
Expand All @@ -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
```
Expand Down
20 changes: 16 additions & 4 deletions packages/powershell/oh-my-posh/oh-my-posh.psm1
Expand Up @@ -4,7 +4,7 @@
#>

$global:PoshSettings = New-Object -TypeName PSObject -Property @{
Theme = "$PSScriptRoot\themes\jandedobbeleer.json"
Theme = "$PSScriptRoot\themes\jandedobbeleer.json";
}

function Get-PoshCommand {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 1f3c253

Please sign in to comment.