Skip to content

Commit

Permalink
Add GitPromptValues global to allow error status display in prompt
Browse files Browse the repository at this point in the history
It is useful to display the error status of the last command.  But in
order to do that we must preserve the value of $? and make the
original $LASTEXITCODE available to string expansion in the prompt.
  • Loading branch information
rkeithhill committed Jul 2, 2019
1 parent 5d2ffbe commit f927f6d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
40 changes: 38 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,12 +375,46 @@ Prompt layout when DefaultPromptWriteStatusFirst is set to $true:
{DPPrefix}<{BeforeStatus}{Status}{AfterStatus}>{PathStatusSeparator}{DPPath}{DPBeforeSuffix}<{DPDebug}><{DPTimingFormat}>{DPSuffix}
```

If you want to display the error status of the last command, you can use the values stored in the `$global:GitPromptValues`
object which includes the value of `$LastExitCode` and `$?` (represented by the property `DollarQuestion`). Here is
a prompt customization that displays a Red exit code value when `$LastExitCode` is non-zero or a Red `!` if `$?`
is `$false`:

```powershell
function global:PromptWriteErrorInfo() {
if ($global:GitPromptValues.DollarQuestion) { return }
if ($global:GitPromptValues.LastExitCode) {
"`e[31m(" + $global:GitPromptValues.LastExitCode + ") `e[0m"
}
else {
"`e[31m! `e[0m"
}
}
$global:GitPromptSettings.DefaultPromptBeforeSuffix.Text = '`n$(PromptWriteErrorInfo)$([DateTime]::now.ToString("MM-dd HH:mm:ss"))'
```

When a PowerShell command fails, this is the prompt you will see:

![~\GitHub\posh-git [master ≡]&#10;! 07-01 22:36:31> ][prompt-error1]

When an external application returns a non-zero exit code, 1 in this case, you will see the exit code in the prompt:

![~\GitHub\posh-git [master ≡]&#10;(1) 07-01 22:32:28> ][prompt-error2]

Note that until you run an external application that resets `$LASTEXITCODE` to zero, you will see the exit code for
any error. In addition to `LastExitCode` and `DollarQuestion`, `$global:GitPromtpValues` also has `IsAdmin` and
`LastPrompt` properties. The `LastPrompt` property contains the ANSI escaped string that was used for the last prompt.
This can be useful for debugging your prompt display particular when using ANSI/VT sequences.

If you require even more customization than `$GitPromptSettings` provides, you can create your own prompt
function to show whatever information you want.
See the [Customizing Your PowerShell Prompt][wiki-custom-prompt] wiki page for details.

However, if you need a custom prompt to perform some non-prompt logic, you can still use posh-git's prompt function to
write out a prompt string. This can be done with the `$GitPromptScriptBlock` variable as shown below e.g.:
However, if you need a custom prompt just in order to perform some non-prompt logic, you can still use posh-git's
prompt function to write out a prompt string. This can be done with the `$GitPromptScriptBlock` variable as shown
below e.g.:

```powershell
# my profile.ps1
Expand Down Expand Up @@ -448,6 +482,8 @@ function prompt {
[prompt-swap]: https://github.com/dahlbyk/posh-git/wiki/images/PromptStatusFirst.png "[master ≡] ~\GitHub\posh-git> "
[prompt-two-line]: https://github.com/dahlbyk/posh-git/wiki/images/PromptTwoLine.png "~\GitHub\posh-git [master ≡]&#10;> "
[prompt-custom]: https://github.com/dahlbyk/posh-git/wiki/images/PromptCustom.png "[master ≡] ~\GitHub\posh-git&#10;02-18 14:04:35 38> "
[prompt-error1]: https://github.com/dahlbyk/posh-git/wiki/images/PromptError1.png "~\GitHub\posh-git [master ≡]&#10;! 07-01 22:36:31> "
[prompt-error2]: https://github.com/dahlbyk/posh-git/wiki/images/PromptError2.png "~\GitHub\posh-git [master ≡]&#10;(1) 07-01 22:32:28> "

[v0-change]: https://github.com/dahlbyk/posh-git/blob/v0/CHANGELOG.md
[v0-readme]: https://github.com/dahlbyk/posh-git/blob/v0/README.md
Expand Down
1 change: 1 addition & 0 deletions src/GitPrompt.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# http://www.markembling.info/view/my-ideal-powershell-prompt-with-git-integration

$global:GitPromptSettings = [PoshGitPromptSettings]::new()
$global:GitPromptValues = [PoshGitPromptValues]::new()

# Override some of the normal colors if the background color is set to the default DarkMagenta.
$s = $global:GitPromptSettings
Expand Down
7 changes: 7 additions & 0 deletions src/PoshGitTypes.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,10 @@ class PoshGitPromptSettings {

[bool]$Debug = $false
}

class PoshGitPromptValues {
[int]$LastExitCode
[bool]$DollarQuestion
[bool]$IsAdmin
[string]$LastPrompt
}
9 changes: 5 additions & 4 deletions src/posh-git.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ else {

# The built-in posh-git prompt function in ScriptBlock form.
$GitPromptScriptBlock = {
$global:GitPromptValues.DollarQuestion = $global:?
$global:GitPromptValues.LastExitCode = $origLastExitCode = $global:LASTEXITCODE
$global:GitPromptValues.IsAdmin = $IsAdmin

$settings = $global:GitPromptSettings
if (!$settings) {
return "<`$GitPromptSettings not found> "
Expand All @@ -38,8 +42,6 @@ $GitPromptScriptBlock = {
$sw = [System.Diagnostics.Stopwatch]::StartNew()
}

$origLastExitCode = $global:LASTEXITCODE

if ($settings.SetEnvColumns) {
# Set COLUMNS so git knows how wide the terminal is
$Env:COLUMNS = $Host.UI.RawUI.WindowSize.Width
Expand Down Expand Up @@ -100,8 +102,7 @@ $GitPromptScriptBlock = {
}
else {
# If using ANSI, set this global to help debug ANSI issues
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')]
$global:PoshGitLastPrompt = EscapeAnsiString $prompt
$global:GitPromptValues.LastPrompt = EscapeAnsiString $prompt
}

$global:LASTEXITCODE = $origLastExitCode
Expand Down

0 comments on commit f927f6d

Please sign in to comment.