Skip to content
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

Show AheadyBy/BehindBy counts based on configuration #256

Merged
merged 5 commits into from
Dec 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 29 additions & 12 deletions GitPrompt.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ $global:GitPromptSettings = New-Object PSObject -Property @{

EnableStashStatus = $false
BeforeStashText = ' ('
BeforeStashBackgroundColor = $Host.UI.RawUI.BackgroundColor
BeforeStashForegroundColor = [ConsoleColor]::Red
BeforeStashBackgroundColor = $Host.UI.RawUI.BackgroundColor
BeforeStashForegroundColor = [ConsoleColor]::Red
AfterStashText = ')'
AfterStashBackgroundColor = $Host.UI.RawUI.BackgroundColor
AfterStashForegroundColor = [ConsoleColor]::Red
Expand All @@ -86,6 +86,9 @@ $global:GitPromptSettings = New-Object PSObject -Property @{

AutoRefreshIndex = $true

# Valid values are "Full", "Compact", and "Minimal"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts on defining an enum to represent the valid states?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about it, but seemed like overkill to me, since nothing else uses enums yet, and the syntax for enums seems less Powershell-y than some weird hack. If enums were already used, I would have defined them that way. But I can add.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to use an enum but once we drop support on master for PS v2, you can use:

[ValidateSet('Compact','Full','Minimal')]
$BranchBehindAndAheadDisplay = "Full"

PS v2 support can't be dropped soon enough. :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, that doesn't work because it's a set of properties being initialized, not a variable. We could move it outside the GitPromptSettings declaration as a special case, but that's kind of ugly.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doh. Yeah, sometimes the narrow GitHub diff window bites me. :-)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For 1.0 we could consider converting $GitPromptSettings to a giant config function of doom, as @Jaykul proposed long ago.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's an interesting approach. It does have the benefit of providing tab-completion/validation on each parameter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That change that was proposed has a side effect of having to pass the same parameters that you've already changed to the config method, since the default value for the parameter is a hard-coded defined value. You could still save the settings in a variable/property bag for storing, and perhaps have the default value for the parameter be the value already in the property bag, so that once changed, it will be used for subsequent sets.

Or you can do the Add-Type -TypeDefinition method to create enums you need and a class that has a bunch of public properties. They won't have tab-completion, but they will have parameter validation.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened #340 for discussion, targeting 1.0.

BranchBehindAndAheadDisplay = "Full"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about defaulting to Full - we're already taking up more space than we used to. Then again, leaving the counts disabled by default means most people will never see them. Anyone else have an opinion?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's exactly why I left it default to Full and then added the example showing how to make it Compact. I find that the staged files section takes way more space than the Full view, unless you're dealing with seriously out of sync branches.


EnablePromptStatus = !$Global:GitMissing
EnableFileStatus = $true
RepositoriesInWhichToDisableFileStatus = @( ) # Array of repository paths
Expand Down Expand Up @@ -141,46 +144,60 @@ function Write-GitStatus($status) {
if ($status -and $s) {
Write-Prompt $s.BeforeText -BackgroundColor $s.BeforeBackgroundColor -ForegroundColor $s.BeforeForegroundColor

$branchStatusSymbol = $null
$branchStatusText = $null
$branchStatusBackgroundColor = $s.BranchBackgroundColor
$branchStatusForegroundColor = $s.BranchForegroundColor

if (!$status.Upstream) {
$branchStatusSymbol = $s.BranchUntrackedSymbol
$branchStatusText = $s.BranchUntrackedSymbol
} elseif ($status.UpstreamGone -eq $true) {
# Upstream branch is gone
$branchStatusSymbol = $s.BranchGoneStatusSymbol
$branchStatusText = $s.BranchGoneStatusSymbol
$branchStatusBackgroundColor = $s.BranchGoneStatusBackgroundColor
$branchStatusForegroundColor = $s.BranchGoneStatusForegroundColor
} elseif ($status.BehindBy -eq 0 -and $status.AheadBy -eq 0) {
# We are aligned with remote
$branchStatusSymbol = $s.BranchIdenticalStatusToSymbol
$branchStatusText = $s.BranchIdenticalStatusToSymbol
$branchStatusBackgroundColor = $s.BranchIdenticalStatusToBackgroundColor
$branchStatusForegroundColor = $s.BranchIdenticalStatusToForegroundColor
} elseif ($status.BehindBy -ge 1 -and $status.AheadBy -ge 1) {
# We are both behind and ahead of remote
$branchStatusSymbol = $s.BranchBehindAndAheadStatusSymbol
if ($s.BranchBehindAndAheadDisplay -eq "Full") {
$branchStatusText = ("{0}{1} {2}{3}" -f $s.BranchBehindStatusSymbol, $status.BehindBy, $s.BranchAheadStatusSymbol, $status.AheadBy)
} elseif ($s.BranchBehindAndAheadDisplay -eq "Compact") {
$branchStatusText = ("{0}{1}{2}" -f $status.BehindBy, $s.BranchBehindAndAheadStatusSymbol, $status.AheadBy)
} else {
$branchStatusText = $s.BranchBehindAndAheadStatusSymbol
}
$branchStatusBackgroundColor = $s.BranchBehindAndAheadStatusBackgroundColor
$branchStatusForegroundColor = $s.BranchBehindAndAheadStatusForegroundColor
} elseif ($status.BehindBy -ge 1) {
# We are behind remote
$branchStatusSymbol = $s.BranchBehindStatusSymbol
if ($s.BranchBehindAndAheadDisplay -eq "Full" -Or $s.BranchBehindAndAheadDisplay -eq "Compact") {
$branchStatusText = ("{0}{1}" -f $s.BranchBehindStatusSymbol, $status.BehindBy)
} else {
$branchStatusText = $s.BranchBehindStatusSymbol
}
$branchStatusBackgroundColor = $s.BranchBehindStatusBackgroundColor
$branchStatusForegroundColor = $s.BranchBehindStatusForegroundColor
} elseif ($status.AheadBy -ge 1) {
# We are ahead of remote
$branchStatusSymbol = $s.BranchAheadStatusSymbol
if ($s.BranchBehindAndAheadDisplay -eq "Full" -Or $s.BranchBehindAndAheadDisplay -eq "Compact") {
$branchStatusText = ("{0}{1}" -f $s.BranchAheadStatusSymbol, $status.AheadBy)
} else {
$branchStatusText = $s.BranchAheadStatusSymbol
}
$branchStatusBackgroundColor = $s.BranchAheadStatusBackgroundColor
$branchStatusForegroundColor = $s.BranchAheadStatusForegroundColor
} else {
# This condition should not be possible but defaulting the variables to be safe
$branchStatusSymbol = "?"
$branchStatusText = "?"
}

Write-Prompt (Format-BranchName($status.Branch)) -BackgroundColor $branchStatusBackgroundColor -ForegroundColor $branchStatusForegroundColor

if ($branchStatusSymbol) {
Write-Prompt (" {0}" -f $branchStatusSymbol) -BackgroundColor $branchStatusBackgroundColor -ForegroundColor $branchStatusForegroundColor
if ($branchStatusText) {
Write-Prompt (" {0}" -f $branchStatusText) -BackgroundColor $branchStatusBackgroundColor -ForegroundColor $branchStatusForegroundColor
}

if($s.EnableFileStatus -and $status.HasIndex) {
Expand Down
4 changes: 4 additions & 0 deletions profile.example.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ function global:prompt {
"> "
}

# Settings for the prompt are in GitPrompt.ps1, so add any desired settings changes here.
# Example:
# $Global:GitPromptSettings.BranchBehindAndAheadDisplay = "Compact"

Start-SshAgent -Quiet