-
-
Notifications
You must be signed in to change notification settings - Fork 810
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update to prompt func to allow path/status order swap #544
Conversation
Oops. Got some test failures to clean up. |
This would allow folks to more fully customize the default prompt. This is one example: $settings = $global:GitPromptSettings
$settings.DefaultPromptAbbreviateHomeDirectory = $true
$settings.DefaultPromptWriteStatusFirst = $true
$settings.BeforeText.Text = '['
$settings.AfterText.Text = '] '
$settings.DefaultPromptTimingFormat = "{0}ms:"
$settings.DefaultPromptPath.Text += "`n"
$settings.DefaultPromptDebug = "[DBG]: "
$settings.DefaultPromptDebug.ForegroundColor = [ConsoleColor]::Magenta
$settings.DefaultPromptSuffix.Text = '$(if ($hist = Get-History -Count 1) {$hist.id+1} else {1})' + $settings.DefaultPromptSuffix.Text To get this form of prompt:
|
src/PoshGitTypes.ps1
Outdated
|
||
[PoshGitTextSpan]$DefaultPromptPrefix = '' | ||
[PoshGitTextSpan]$DefaultPromptSuffix = '$(''>'' * ($nestedPromptLevel + 1)) ' | ||
[PoshGitTextSpan]$DefaultPromptDebugSuffix = ' [DBG]$(''>'' * ($nestedPromptLevel + 1)) ' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI the duplication here of the $nestedPromptLevel code bothered me. So now we display in order (where []
designates optional text:
[ DPDebugSuffix][ DPTimingFormat]DPSuffix
src/PoshGitTypes.ps1
Outdated
[bool]$DefaultPromptAbbreviateHomeDirectory = $false | ||
|
||
[bool]$DefaultPromptEnableTiming = $false | ||
[PoshGitTextSpan]$DefaultPromptTimingFormat = ' {0}ms' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't envision too many folks using prompt timing at all. But for those of us that do, I prefer not to have the ms
in there. This is pretty easy way to accomplish this.
c6fa372
to
ec0318a
Compare
src/PoshGitTypes.ps1
Outdated
@@ -141,12 +141,12 @@ class PoshGitTextSpan { | |||
} | |||
else { | |||
$bg = $this.BackgroundColor | |||
if ($bg -and !(Test-VirtualTerminalSequece $bg)) { | |||
if (($null -ne $bg) -and !(Test-VirtualTerminalSequece $bg)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI this allows you to set the value to 0
(black). Before, it would see the 0
as false in this condition and use the "default" color.
@dahlbyk I think this is ready to go. OK if I merge it? |
A few more changes here that I'm proposing. I have been noodling on how to make it simpler to switch path/status order (fewer settings requiring tweaks) and how to make it simpler to switch to a two-line prompt. For this, I think it is helpful to see the current and proposed prompt layout. Note
The above changes (corresponding to the commit I'm getting ready to push, gives us some nice flexibilty. Is someone wants to swap path/status, then they simply set (no need to fiddle with both BeforeStatus and AfterStatus): $global:GitPromptSettings.DefaultPromptWriteStatusFirst = $true BTW I've rename a few of the stettings that had If they want a two line prompt: $global:GitPromptSettings.DefaultPromptMiddle.Text = '`n' I also have the default prompt, when in ANSI mode, saving the escaped prompt string to the global variable ~\GitHub\rkeithhill\PowerShell [master ≡ +1 ~0 -0 !] [DBG]:>> $PoshGitLastPrompt
~\GitHub\rkeithhill\PowerShell `e[93m[`e[0m`e[96mmaster`e[0m`e[96m ≡`e[0m`e[31m`e[49m +1`e[0m`e[31m`e[49m ~0`e[0m`e[31m`e[49m -0`e[0m`e[31m !`e[0m`e[93m]`e[0m`e[95m [DBG]:`e[0m>> |
9d4465c
to
841eb45
Compare
@dahlbyk Any objections if I go ahead and merge this? Since it is still beta it would be good to get some feedback on the DefaultPrompt setting changes. |
Add $global:PoshGitLastPrompt for ANSI escaped prompt str
841eb45
to
5d70345
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, first and foremost: sorry this sat unanswered for two months. 😭
That said, I love the changes! I squashed up a few of the commits to reduce the code churn a bit, and left a few questions/comments.
And, of course, the build failed with "Unable to locate package powershell".
Note: the number right before the > char is the history id number.
Man, I did something like this forever ago and completely forgot about it. 🥂
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')] | ||
$nonRepoRegex = '^PowerShell \d+\.\d+\.\d+(\.\d+|-\S+)? \(\d+\)$' | ||
$nonRepoRegex = '^PowerShell \d+\.\d+\.\d+(\.\d+|-\S+)? \d\d-bit \(\d+\)$' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are going to break when we get to 128-bit processors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed. :-)
test/DefaultPrompt.Tests.ps1
Outdated
$res = [string](&$prompt 6>&1) | ||
$res | Should BeExactly "${env:HOME}> " | ||
$res = &$prompt | ||
"$res" | Should BeExactly "${env:HOME}> " |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you help me understand why sometimes you use [string](&$prompt 6>&1)
and others you use this version?
src/PoshGitTypes.ps1
Outdated
@@ -259,6 +260,7 @@ class PoshGitPromptSettings { | |||
|
|||
[PoshGitTextSpan]$DefaultPromptPrefix = '' | |||
[PoshGitTextSpan]$DefaultPromptPath = '$(Get-PromptPath)' | |||
[PoshGitTextSpan]$DefaultPromptMiddle = '' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Middle's a bit vague... maybe DefaultPromptBeforeSuffix
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, Middle is a bit vague. OTOH this section isn't always before the suffix i.e. if Debug or Timing is enabled this text appears before that. That said, in most cases (including the default) it is before the suffix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess I was thinking of Debug/Timing as part of the suffix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can make that change but not tonight - getting late and I have VSCode-powershell PR to finish up. I can get to this tomorrow though as I really would like to get this in for beta 2. For somewhat selfish reasons I'm excited about this PR as it allows me to not have to define a custom prompt function anymore. :-)
src/posh-git.psm1
Outdated
# Write default prompt middle if not empty. | ||
if ($settings.DefaultPromptMiddle.Text) { | ||
$promptMiddle = [PoshGitTextSpan]::new($settings.DefaultPromptMiddle) | ||
$promptMiddle.Text = $ExecutionContext.SessionState.InvokeCommand.ExpandString($promptMiddle.Text) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do this enough, I wonder if it would be worth supporting [PoshGitTextSpan]::Expand($settings.DefaultPromptMiddle)
? (Or add [bool]$expand = $false
to the ctor?)
If we include the empty check, we could replace this whole block with just...
$prompt += Write-Prompt [PoshGitTextSpan]::Expand($settings.DefaultPromptMiddle)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is acting kind of like a ctor. Maybe we could add a parameter to the ctor e.g.:
$prompt += Write-Prompt [PoshGitTextSpan]::new($settings.DefaultPromptMiddle, $ExecutionContext)
Or perhaps even better (shorter to type anyway):
$prompt += Write-Prompt $settings.DefaultPromptMiddle.Expand($ExecutionContext)
The Expand()
method is sort of a fluent API that returns a copy of the original textspan but with the text expanded. Note: we have to pass in the $ExecutionContext
or technically the $ExecutionContext.SessionState.InvokeCommand
object but the former is less to type. :-) We have to do this because the context isn't available inside an instance of a PowerShell class.
I've tested the second approach above and it works fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the "extension method" approach the best.
Note: we have to pass in the $ExecutionContext…because the context isn't available inside an instance of a PowerShell class.
TIL
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool. BTW poked around and found a way to get the $ExecutionContext inside the Expand()
method so we won't have to pass that in after all. Turns out PS classes can't find this "automatic" variable when it is compiled but you can work-around it by asking for the variable at runtime with Get-Variable ExecutionContext -ValueOnly
. So we should be able to simplify it to just:
$prompt += Write-Prompt $settings.DefaultPromptMiddle.Expand()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've got a fix for both the DefaultPromptBeforeSuffix
issue and Expand
but with the branch updates on the server, I can no longer push my changes. Can we merge this then I'll submit another PR with those two changes? Or do you know the magic Git command to get my local branch in sync? I suppose I could delete it and check it out again from the server.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assuming origin
is your remote for this repo, and you have two new local commits since I rebased…
git fetch
git rebase HEAD~2 --onto origin/rkeithhill/change-debugprompt
Thanks for the rebase help. I "think" I've addressed all your PR concerns. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Please review the last two commits I added, then I think we're done.
Can you help me understand why sometimes you use
[string](&$prompt 6>&1)
and others you use this version?
Eh?
4df237a
to
30a888e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's pretend I didn't just break the build.
Am I supposed to be using a specific version of Pester? I'm on a new machine that apparently came with Pester 3.4, but posh-git tests are failing. pester/Pester#585
30a888e
to
763368b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Am I supposed to be using a specific version of Pester? I'm on a new machine that apparently came with Pester 3.4, but posh-git tests are failing.
Answered my own question: -MinimumVersion 4.0.8
src/posh-git.psm1
Outdated
@@ -61,9 +61,7 @@ $GitPromptScriptBlock = { | |||
$prompt = '' | |||
|
|||
# Write default prompt prefix if not empty. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Guess we should change the comment to just # Write default prompt prefix
.
src/posh-git.psm1
Outdated
@@ -79,12 +77,10 @@ $GitPromptScriptBlock = { | |||
} | |||
|
|||
# Write default prompt middle if not empty. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto - drop the if not empty
bit.
src/GitPrompt.ps1
Outdated
return $StringBuilder | ||
} | ||
|
||
return "" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could probably simplify this to a one-liner: return $(if ($StringBuilder) {$StringBuilder} else {""})
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would be fine. I'm crashing for the night, but if you want to patch these up I can merge in the morning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do.
RE:
Looks like I missed one in an ANSI test. In general, Want me to fix it? |
I'm content for now as long as the tests are green. I do wonder about hiding some of the |
Also modified ANSI tests to be consistent and not flatten $res with string interpolation. Catch more errors this way.
Plus a few other tweaks to clean up the default prompt function.