Skip to content

Commit

Permalink
Do not update WindowTitle at all in prompt func if setting is $null
Browse files Browse the repository at this point in the history
We used to restore the previousWindowTitle but this had the effect of
always updating the WindowTitle with a fixed string - whatever we
captured in $PreviousWindowTitle.

However, we still restore the original window title when the module is
removed. But we only do this if the module is configured to supply the
window title.

Rename variable to make its intent clearer.

Fixes #594
  • Loading branch information
rkeithhill committed Jul 11, 2018
1 parent 3dd9fdc commit d828fe4
Showing 1 changed file with 22 additions and 37 deletions.
59 changes: 22 additions & 37 deletions src/posh-git.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@ if (!$Env:HOME) { $Env:HOME = "$Env:USERPROFILE" }
$IsAdmin = Test-Administrator

# Probe $Host.UI.RawUI.WindowTitle to see if it can be set without errors
$WindowTitleSupported = $false
$HostSupportsSettingWindowTitle = $false
try {
$global:PreviousWindowTitle = $Host.UI.RawUI.WindowTitle
$newTitle = "${global:PreviousWindowTitle} "
$global:PoshGitOrigWindowTitle = $Host.UI.RawUI.WindowTitle
$newTitle = "${global:PoshGitOrigWindowTitle} "
$Host.UI.RawUI.WindowTitle = $newTitle
$WindowTitleSupported = ($Host.UI.RawUI.WindowTitle -eq $newTitle)
$Host.UI.RawUI.WindowTitle = $global:PreviousWindowTitle
$HostSupportsSettingWindowTitle = ($Host.UI.RawUI.WindowTitle -eq $newTitle)
$Host.UI.RawUI.WindowTitle = $global:PoshGitOrigWindowTitle
}
catch {
Write-Debug "Probing for WindowTitleSupported errored: $_"
$global:PoshGitOrigWindowTitle = $null
Write-Debug "Probing for HostSupportsSettingWindowTitle errored: $_"
}

# Get the default prompt definition.
Expand All @@ -43,10 +44,6 @@ else {
$GitPromptScriptBlock = {
$settings = $global:GitPromptSettings
if (!$settings) {
if ($WindowTitleSupported -and $global:PreviousWindowTitle) {
$Host.UI.RawUI.WindowTitle = $global:PreviousWindowTitle
}

return "<`$GitPromptSettings not found> "
}

Expand Down Expand Up @@ -92,34 +89,22 @@ $GitPromptScriptBlock = {
$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
# Update the host's WindowTitle if 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) {
if ($global:PreviousWindowTitle) {
$Host.UI.RawUI.WindowTitle = $global:PreviousWindowTitle
if ($HostSupportsSettingWindowTitle -and $settings.WindowTitle) {
try {
if ($settings.WindowTitle -is [scriptblock]) {
$windowTitleText = & $settings.WindowTitle $global:GitStatus $IsAdmin
}
}
else {
try {
if ($windowTitle -is [scriptblock]) {
$windowTitleText = & $windowTitle $global:GitStatus $IsAdmin
}
else {
$windowTitleText = $ExecutionContext.SessionState.InvokeCommand.ExpandString("$windowTitle")
}

# Put $windowTitleText in a string to ensure results returned by scriptblock are flattened to a string
$Host.UI.RawUI.WindowTitle = "$windowTitleText"
else {
$windowTitleText = $ExecutionContext.SessionState.InvokeCommand.ExpandString("$($settings.WindowTitle)")
}
catch {
if ($global:PreviousWindowTitle) {
$Host.UI.RawUI.WindowTitle = $global:PreviousWindowTitle
}

Write-Debug "Error occurred during evaluation of `$GitPromptSettings.WindowTitle: $_"
}
# Put $windowTitleText in a string to ensure results returned by scriptblock are flattened into a string
$Host.UI.RawUI.WindowTitle = "$windowTitleText"
}
catch {
Write-Debug "Error occurred during evaluation of `$GitPromptSettings.WindowTitle: $_"
}
}

Expand Down Expand Up @@ -171,9 +156,9 @@ if ($ForcePoshGitPrompt -or !$currentPromptDef -or ($currentPromptDef -eq $defau
$ExecutionContext.SessionState.Module.OnRemove = {
$global:VcsPromptStatuses = $global:VcsPromptStatuses | Where-Object { $_ -ne $PoshGitVcsPrompt }

# Revert original WindowTitle
if ($WindowTitleSupported -and $global:PreviousWindowTitle) {
$Host.UI.RawUI.WindowTitle = $global:PreviousWindowTitle
# Revert original WindowTitle but only if posh-git is currently configured to set it
if ($HostSupportsSettingWindowTitle -and $global:GitPromptSettings.WindowTitle -and $global:PoshGitOrigWindowTitle) {
$Host.UI.RawUI.WindowTitle = $global:PoshGitOrigWindowTitle
}

# Check if the posh-git prompt function itself has been replaced. If so, do not restore the prompt function
Expand Down

0 comments on commit d828fe4

Please sign in to comment.