-
-
Notifications
You must be signed in to change notification settings - Fork 814
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #597 from dahlbyk/rkeithhill/fix-window-title-upda…
…ting Do not update WindowTitle at all in prompt func if setting is $null
- Loading branch information
Showing
2 changed files
with
63 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
$HostSupportsSettingWindowTitle = $null | ||
$OriginalWindowTitle = $null | ||
|
||
function Test-WindowTitleIsWriteable { | ||
if ($null -eq $HostSupportsSettingWindowTitle) { | ||
# Probe $Host.UI.RawUI.WindowTitle to see if it can be set without errors | ||
try { | ||
$script:OriginalWindowTitle = $Host.UI.RawUI.WindowTitle | ||
$newTitle = "${OriginalWindowTitle} " | ||
$Host.UI.RawUI.WindowTitle = $newTitle | ||
$script:HostSupportsSettingWindowTitle = ($Host.UI.RawUI.WindowTitle -eq $newTitle) | ||
$Host.UI.RawUI.WindowTitle = $OriginalWindowTitle | ||
Write-Debug "HostSupportsSettingWindowTitle: $HostSupportsSettingWindowTitle" | ||
Write-Debug "OriginalWindowTitle: $OriginalWindowTitle" | ||
} | ||
catch { | ||
$script:OriginalWindowTitle = $null | ||
$script:HostSupportsSettingWindowTitle = $false | ||
Write-Debug "HostSupportsSettingWindowTitle error: $_" | ||
} | ||
} | ||
return $HostSupportsSettingWindowTitle | ||
} | ||
|
||
function Reset-WindowTitle { | ||
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")] | ||
param() | ||
$settings = $global:GitPromptSettings | ||
|
||
# Revert to original WindowTitle, but only if posh-git is currently configured to set it | ||
if ($HostSupportsSettingWindowTitle -and $OriginalWindowTitle -and $settings.WindowTitle) { | ||
Write-Debug "Resetting WindowTitle: '$OriginalWindowTitle'" | ||
$Host.UI.RawUI.WindowTitle = $OriginalWindowTitle | ||
} | ||
} | ||
|
||
function Set-WindowTitle { | ||
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")] | ||
param($GitStatus, $IsAdmin) | ||
$settings = $global:GitPromptSettings | ||
|
||
# Update the host's WindowTitle if host supports it and user has not disabled $GitPromptSettings.WindowTitle | ||
if ($settings.WindowTitle -and (Test-WindowTitleIsWriteable)) { | ||
try { | ||
if ($settings.WindowTitle -is [scriptblock]) { | ||
# ensure results returned by scriptblock are flattened into a string | ||
$windowTitleText = "$(& $settings.WindowTitle $GitStatus $IsAdmin)" | ||
} | ||
else { | ||
$windowTitleText = $ExecutionContext.SessionState.InvokeCommand.ExpandString("$($settings.WindowTitle)") | ||
} | ||
|
||
Write-Debug "Setting WindowTitle: $windowTitleText" | ||
$Host.UI.RawUI.WindowTitle = "$windowTitleText" | ||
} | ||
catch { | ||
Write-Debug "Error occurred during evaluation of `$GitPromptSettings.WindowTitle: $_" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters