diff options
author | marcos-quezada <34238856+marcos-quezada@users.noreply.github.com> | 2020-10-24 11:18:58 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-24 11:18:58 +0200 |
commit | 745a554fdc5aeb4e238d6e7fb71154f8c48abda3 (patch) | |
tree | 78734e716d6a6907aa3ec8c00759e428930d2dc9 /src/init | |
parent | ea9f803018ffdf9f1ca48c98b98223d3bb20d21b (diff) |
fix(pwsh): Switched pwsh profile to use dollar hook for a more accurate success/failure handling (#1745)
* [f]Use_global_dollar_hook_for_lastExitCodeForPrompt Switched to use the dollar hook, as source of truth for exit code of las command, included comments to clarify.
* fix: Adjusted accordingly to comments on PR.
* fix: Moved last exit code handling inside if to reuse that variable.
Co-authored-by: Marcos Quezada Perez <marcos.quezadaperez@peakwork.com>
Diffstat (limited to 'src/init')
-rw-r--r-- | src/init/starship.ps1 | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/src/init/starship.ps1 b/src/init/starship.ps1 index 64f524c2e..77fe4290e 100644 --- a/src/init/starship.ps1 +++ b/src/init/starship.ps1 @@ -11,15 +11,22 @@ function global:prompt { $env:PWD = $PWD $current_directory = (Convert-Path -LiteralPath $PWD) - # If an external command has not been executed, then the $LASTEXITCODE will be $null. - # For the purposes of the prompt, replace this with a 0 exit code so that the prompt - # doesn't show the last command as a failure (because it had a non-zero exit code). - $lastExitCodeForPrompt = if ($origLastExitCode) { $origLastExitCode } else { 0 } + # Whe start from the premise that the command executed correctly, which covers also the fresh console. + $lastExitCodeForPrompt = 0 # Save old output encoding and set it to UTF-8 $origOutputEncoding = [Console]::OutputEncoding [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 if ($lastCmd = Get-History -Count 1) { + # In case we have a False on the Dollar hook, we know there's an error. + if( -not $origDollarQuestion){ + # We retrieve the InvocationInfo from the most recent error. + $lastCmdletError = Get-Error | Where-Object {$_ -ne $null} | Select-Object -expand InvocationInfo + # We check if the las command executed matches the line that caused the last error , in which case we know + # it was an internal Powershell command, otherwise, there MUST be an error code. + $lastExitCodeForPrompt = if($lastCmd.CommandLine -eq $lastCmdletError.Line){1} else {$origLastExitCode} + } + $duration = [math]::Round(($lastCmd.EndExecutionTime - $lastCmd.StartExecutionTime).TotalMilliseconds) # & ensures the path is interpreted as something to execute $out = @(&::STARSHIP:: prompt "--path=$current_directory" --status=$lastExitCodeForPrompt --jobs=$jobs --cmd-duration=$duration) |