fix(pwsh): validate execution time type

resolves #2633
This commit is contained in:
Jan De Dobbeleer 2022-08-09 09:27:57 +02:00 committed by Jan De Dobbeleer
parent 4e992d48d1
commit 3345017b2d
2 changed files with 6 additions and 10 deletions

View file

@ -1,7 +1,7 @@
package engine
import (
"io/ioutil"
"io/ioutil" //nolint: staticcheck
"oh-my-posh/color"
"os"
"path/filepath"

View file

@ -286,34 +286,30 @@ Example:
}
function Update-PoshErrorCode {
$lastHistory = Get-History -ErrorAction Ignore -Count 1
# default values
$script:ErrorCode = 0
$script:ExecutionTime = -1
$lastHistory = Get-History -ErrorAction Ignore -Count 1
# error code should be updated only when a non-empty command is run
if (($null -eq $lastHistory) -or ($script:LastHistoryId -eq $lastHistory.Id)) {
return
}
$script:LastHistoryId = $lastHistory.Id
$script:ExecutionTime = ($lastHistory.EndExecutionTime - $lastHistory.StartExecutionTime).TotalMilliseconds
if (-not ($script:ExecutionTime -is [int])) {
$script:ExecutionTime = 0
}
if ($script:OriginalLastExecutionStatus) {
$script:ErrorCode = 0
return
}
$invocationInfo = try {
# retrieve info of the most recent error
$global:Error[0] | Where-Object { $_ -ne $null } | Select-Object -ExpandProperty InvocationInfo
} catch { $null }
# check if the last command caused the last error
if ($null -ne $invocationInfo -and $lastHistory.CommandLine -eq $invocationInfo.Line) {
$script:ErrorCode = 1
return
}
if ($script:OriginalLastExitCode -is [int] -and $script:OriginalLastExitCode -ne 0) {
# native app exit code
$script:ErrorCode = $script:OriginalLastExitCode