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

Update to prompt func to allow path/status order swap #544

Merged
merged 11 commits into from
Apr 19, 2018
48 changes: 27 additions & 21 deletions src/GitPrompt.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ $s = $global:GitPromptSettings
if ($Host.UI.RawUI.BackgroundColor -eq [ConsoleColor]::DarkMagenta) {
$s.LocalDefaultStatusSymbol.ForegroundColor = 'Green'
$s.LocalWorkingStatusSymbol.ForegroundColor = 'Red'
$s.BeforeIndexText.ForegroundColor = 'Green'
$s.BeforeIndex.ForegroundColor = 'Green'
$s.IndexColor.ForegroundColor = 'Green'
$s.WorkingColor.ForegroundColor = 'Red'
}
Expand Down Expand Up @@ -69,6 +69,10 @@ function Write-Prompt {
$StringBuilder
)

if (!$Object -or (($Object -is [PoshGitTextSpan]) -and !$Object.Text)) {
return $(if ($StringBuilder) { $StringBuilder } else { "" })
}

if ($PSCmdlet.ParameterSetName -eq "CellColor") {
$bgColor = $Color.BackgroundColor
$fgColor = $Color.ForegroundColor
Expand Down Expand Up @@ -99,11 +103,7 @@ function Write-Prompt {
$str = "${fg}${bg}${Object}${e}0m"
}

if ($StringBuilder) {
return $StringBuilder.Append($str)
}

return $str
return $(if ($StringBuilder) { $StringBuilder.Append($str) } else { $str })
}
}

Expand All @@ -127,11 +127,7 @@ function Write-Prompt {
}

Write-Host @writeHostParams
if ($StringBuilder) {
return $StringBuilder
}

""
return $(if ($StringBuilder) { $StringBuilder } else { "" })
}

<#
Expand Down Expand Up @@ -177,16 +173,21 @@ function Write-GitStatus {

$sb = [System.Text.StringBuilder]::new(150)

$sb | Write-Prompt $s.BeforeText > $null
# When prompt is first (default), place the separator before the status summary
if (!$s.DefaultPromptWriteStatusFirst) {
$sb | Write-Prompt $s.PathStatusSeparator > $null
}

$sb | Write-Prompt $s.BeforeStatus > $null
$sb | Write-GitBranchName $Status -NoLeadingSpace > $null
$sb | Write-GitBranchStatus $Status > $null

if ($s.EnableFileStatus -and $Status.HasIndex) {
$sb | Write-Prompt $s.BeforeIndexText > $null
$sb | Write-Prompt $s.BeforeIndex > $null
$sb | Write-GitIndexStatus $Status > $null

if ($Status.HasWorking) {
$sb | Write-Prompt $s.DelimText > $null
$sb | Write-Prompt $s.DelimStatus > $null
}
}

Expand All @@ -200,7 +201,12 @@ function Write-GitStatus {
$sb | Write-GitStashCount $Status > $null
}

$sb | Write-Prompt $s.AfterText > $null
$sb | Write-Prompt $s.AfterStatus > $null

# When status is first, place the separator after the status summary
if ($s.DefaultPromptWriteStatusFirst) {
$sb | Write-Prompt $s.PathStatusSeparator > $null
}

$sb.ToString()
}
Expand Down Expand Up @@ -807,14 +813,14 @@ function Write-GitStashCount {
$stashText = "$($Status.StashCount)"

if ($StringBuilder) {
$StringBuilder | Write-Prompt $s.BeforeStashText > $null
$StringBuilder | Write-Prompt $s.BeforeStash > $null
$StringBuilder | Write-Prompt $stashText -Color $s.StashColor > $null
$StringBuilder | Write-Prompt $s.AfterStashText > $null
$StringBuilder | Write-Prompt $s.AfterStash > $null
}
else {
$str += Write-Prompt $s.BeforeStashText
$str += Write-Prompt $s.BeforeStash
$str += Write-Prompt $stashText -Color $s.StashColor
$str += Write-Prompt $s.AfterStashText
$str += Write-Prompt $s.AfterStash
}
}

Expand Down Expand Up @@ -856,9 +862,9 @@ $PoshGitVcsPrompt = {
$errorText = "PoshGitVcsPrompt error: $_"
$sb = [System.Text.StringBuilder]::new()

$sb | Write-Prompt $s.BeforeText > $null
$sb | Write-Prompt $s.BeforeStatus > $null
$sb | Write-Prompt $errorText -Color $s.ErrorColor > $null
$sb | Write-Prompt $s.AfterText > $null
$sb | Write-Prompt $s.AfterStatus > $null

$sb.ToString()
}
Expand Down
42 changes: 28 additions & 14 deletions src/PoshGitTypes.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ class PoshGitTextSpan {
$this.CustomAnsi = $null
}

[PoshGitTextSpan] Expand() {
if (!$this.Text) {
return $this
}

$execContext = Get-Variable ExecutionContext -ValueOnly
$expandedText = $execContext.SessionState.InvokeCommand.ExpandString($this.Text)
$newTextSpan = [PoshGitTextSpan]::new($expandedText, $this.ForegroundColor, $this.BackgroundColor)
$newTextSpan.CustomAnsi = $this.CustomAnsi
return $newTextSpan
}

[string] ToAnsiString() {
$e = [char]27 + "["
$txt = $this.Text
Expand Down Expand Up @@ -219,13 +231,14 @@ class PoshGitPromptSettings {
[PoshGitCellColor]$StashColor = [PoshGitCellColor]::new([ConsoleColor]::Red)
[PoshGitCellColor]$ErrorColor = [PoshGitCellColor]::new([ConsoleColor]::Red)

[PoshGitTextSpan]$BeforeText = [PoshGitTextSpan]::new(' [', [ConsoleColor]::Yellow)
[PoshGitTextSpan]$DelimText = [PoshGitTextSpan]::new(' |', [ConsoleColor]::Yellow)
[PoshGitTextSpan]$AfterText = [PoshGitTextSpan]::new(']', [ConsoleColor]::Yellow)
[PoshGitTextSpan]$PathStatusSeparator = ' '
[PoshGitTextSpan]$BeforeStatus = [PoshGitTextSpan]::new('[', [ConsoleColor]::Yellow)
[PoshGitTextSpan]$DelimStatus = [PoshGitTextSpan]::new(' |', [ConsoleColor]::Yellow)
[PoshGitTextSpan]$AfterStatus = [PoshGitTextSpan]::new(']', [ConsoleColor]::Yellow)

[PoshGitTextSpan]$BeforeIndexText = [PoshGitTextSpan]::new('', [ConsoleColor]::DarkGreen)
[PoshGitTextSpan]$BeforeStashText = [PoshGitTextSpan]::new(' (', [ConsoleColor]::Red)
[PoshGitTextSpan]$AfterStashText = [PoshGitTextSpan]::new(')', [ConsoleColor]::Red)
[PoshGitTextSpan]$BeforeIndex = [PoshGitTextSpan]::new('', [ConsoleColor]::DarkGreen)
[PoshGitTextSpan]$BeforeStash = [PoshGitTextSpan]::new(' (', [ConsoleColor]::Red)
[PoshGitTextSpan]$AfterStash = [PoshGitTextSpan]::new(')', [ConsoleColor]::Red)

[PoshGitTextSpan]$LocalDefaultStatusSymbol = [PoshGitTextSpan]::new('', [ConsoleColor]::DarkGreen)
[PoshGitTextSpan]$LocalWorkingStatusSymbol = [PoshGitTextSpan]::new('!', [ConsoleColor]::DarkRed)
Expand Down Expand Up @@ -257,18 +270,19 @@ class PoshGitPromptSettings {
[Nullable[bool]]$EnableFileStatusFromCache = $null
[string[]]$RepositoriesInWhichToDisableFileStatus = @()

[string]$DescribeStyle = ''
[psobject]$WindowTitle = {param($GitStatus, [bool]$IsAdmin) "$(if ($IsAdmin) {'Administrator: '})$(if ($GitStatus) {"posh~git ~ $($GitStatus.RepoName) [$($GitStatus.Branch)] ~ "})PowerShell $($PSVersionTable.PSVersion) ($PID)"}
[string]$DescribeStyle = ''
[psobject]$WindowTitle = {param($GitStatus, [bool]$IsAdmin) "$(if ($IsAdmin) {'Administrator: '})$(if ($GitStatus) {"posh~git ~ $($GitStatus.RepoName) [$($GitStatus.Branch)] ~ "})PowerShell $($PSVersionTable.PSVersion) $([IntPtr]::Size * 8)-bit ($PID)"}

[PoshGitTextSpan]$DefaultPromptPrefix = ''
[PoshGitTextSpan]$DefaultPromptSuffix = '$(''>'' * ($nestedPromptLevel + 1)) '
[PoshGitTextSpan]$DefaultPromptDebugSuffix = ' [DBG]$(''>'' * ($nestedPromptLevel + 1)) '
[PoshGitTextSpan]$DefaultPromptPath = '$(Get-PromptPath)'
[PoshGitTextSpan]$DefaultPromptBeforeSuffix = ''
[PoshGitTextSpan]$DefaultPromptDebug = [PoshGitTextSpan]::new(' [DBG]:', [ConsoleColor]::Magenta)
[PoshGitTextSpan]$DefaultPromptSuffix = '$(">" * ($nestedPromptLevel + 1)) '

[bool]$DefaultPromptAbbreviateHomeDirectory = $true
[bool]$DefaultPromptWriteStatusFirst = $false
[bool]$DefaultPromptEnableTiming = $false
[PoshGitCellColor]$DefaultPromptTimingColor = [PoshGitCellColor]::new()

[PoshGitTextSpan]$DefaultPromptPath = '$(Get-PromptPath)'
[bool]$DefaultPromptAbbreviateHomeDirectory = $false
[PoshGitTextSpan]$DefaultPromptTimingFormat = ' {0}ms'

[int]$BranchNameLimit = 0
[string]$TruncatedBranchSuffix = '...'
Expand Down
71 changes: 44 additions & 27 deletions src/posh-git.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -57,39 +57,44 @@ $GitPromptScriptBlock = {

$origLastExitCode = $global:LASTEXITCODE

# Construct/write the prompt text
$prompt = ''

# Display default prompt prefix if not empty.
$defaultPromptPrefix = $settings.DefaultPromptPrefix
if ($defaultPromptPrefix.Text) {
$promptPrefix = [PoshGitTextSpan]::new($settings.DefaultPromptPrefix)
$promptPrefix.Text = $ExecutionContext.SessionState.InvokeCommand.ExpandString($defaultPromptPrefix.Text)
$prompt += Write-Prompt $promptPrefix
}
# Write default prompt prefix
$prompt += Write-Prompt $settings.DefaultPromptPrefix.Expand()

# Write the abbreviated current path
$promptPath = [PoshGitTextSpan]::new($settings.DefaultPromptPath)
$promptPath.Text = $ExecutionContext.SessionState.InvokeCommand.ExpandString($promptPath.Text)
$prompt += Write-Prompt $promptPath
# Get the current path - formatted correctly
$promptPath = $settings.DefaultPromptPath.Expand()

# Write the Git status summary information
$prompt += Write-VcsStatus
# Write the path and Git status summary information
if ($settings.DefaultPromptWriteStatusFirst) {
$prompt += Write-VcsStatus
$prompt += Write-Prompt $promptPath
}
else {
$prompt += Write-Prompt $promptPath
$prompt += Write-VcsStatus
}

# If stopped in the debugger, the prompt needs to indicate that in some fashion
$hasInBreakpoint = [runspace]::DefaultRunspace.Debugger | Get-Member -Name InBreakpoint -MemberType property
$debugMode = (Test-Path Variable:/PSDebugContext) -or ($hasInBreakpoint -and [runspace]::DefaultRunspace.Debugger.InBreakpoint)
$defaultPromptSuffix = if ($debugMode) { $settings.DefaultPromptDebugSuffix } else { $settings.DefaultPromptSuffix }
# Write default prompt before suffix text
$prompt += Write-Prompt $settings.DefaultPromptBeforeSuffix.Expand()

$promptSuffix = [PoshGitTextSpan]::new($defaultPromptSuffix)
if ($defaultPromptSuffix.Text) {
$promptSuffix.Text = $ExecutionContext.SessionState.InvokeCommand.ExpandString($defaultPromptSuffix.Text)
# If stopped in the debugger, the prompt needs to indicate that by writing default propmt debug
if ((Test-Path Variable:/PSDebugContext) -or [runspace]::DefaultRunspace.Debugger.InBreakpoint) {
$prompt += Write-Prompt $settings.DefaultPromptDebug.Expand()
}
# If user specifies $null or empty string, set to ' ' to avoid "PS>" unexpectedly being displayed
else {
$promptSuffix.Text = ' '

# Get the prompt suffix text
$promptSuffix = $settings.DefaultPromptSuffix.Expand()

# When using Write-Host, we return a single space from this function to prevent PowerShell from displaying "PS>"
# So to avoid two spaces at the end of the suffix, remove one here if it exists
if (!$settings.AnsiConsole -and $promptSuffix.Text.EndsWith(' ')) {
$promptSuffix.Text = $promptSuffix.Text.Substring(0, $promptSuffix.Text.Length - 1)
}

# Update the host's WindowTitle is host supports it and user has not disabled $GitPromptSettings.WindowTitle
# This has to be *after* the call to Write-VcsStatus, which populates $global:GitStatus
if ($WindowTitleSupported) {
$windowTitle = $settings.WindowTitle
if (!$windowTitle) {
Expand Down Expand Up @@ -119,15 +124,27 @@ $GitPromptScriptBlock = {
}
}

# If prompt timing enabled, display elapsed milliseconds
# If prompt timing enabled, write elapsed milliseconds
if ($settings.DefaultPromptEnableTiming) {
$timingInfo = [PoshGitTextSpan]::new($settings.DefaultPromptTimingFormat)
$sw.Stop()
$elapsed = $sw.ElapsedMilliseconds
$prompt += Write-Prompt " ${elapsed}ms" -Color $settings.DefaultPromptTimingColor
$timingInfo.Text = $timingInfo.Text -f $sw.ElapsedMilliseconds
$prompt += Write-Prompt $timingInfo
}

$global:LASTEXITCODE = $origLastExitCode
$prompt += Write-Prompt $promptSuffix

# When using Write-Host, return at least a space to avoid "PS>" being unexpectedly displayed
if (!$settings.AnsiConsole) {
$prompt += " "
}
else {
# If using ANSI, set this global to help debug ANSI issues
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')]
$global:PoshGitLastPrompt = EscapeAnsiString $prompt
}

$global:LASTEXITCODE = $origLastExitCode
$prompt
}

Expand Down
Loading