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

Do not update WindowTitle at all in prompt func if setting is $null #597

Merged
merged 5 commits into from
Jul 12, 2018
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
60 changes: 60 additions & 0 deletions src/WindowTitle.ps1
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) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

👍

# 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: $_"
}
}
}
53 changes: 3 additions & 50 deletions src/posh-git.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ param([switch]$NoVersionWarn, [switch]$ForcePoshGitPrompt)
. $PSScriptRoot\ConsoleMode.ps1
. $PSScriptRoot\Utils.ps1
. $PSScriptRoot\AnsiUtils.ps1
. $PSScriptRoot\WindowTitle.ps1
. $PSScriptRoot\PoshGitTypes.ps1
. $PSScriptRoot\GitUtils.ps1
. $PSScriptRoot\GitPrompt.ps1
Expand All @@ -17,19 +18,6 @@ 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
try {
$global:PreviousWindowTitle = $Host.UI.RawUI.WindowTitle
$newTitle = "${global:PreviousWindowTitle} "
$Host.UI.RawUI.WindowTitle = $newTitle
$WindowTitleSupported = ($Host.UI.RawUI.WindowTitle -eq $newTitle)
$Host.UI.RawUI.WindowTitle = $global:PreviousWindowTitle
}
catch {
Write-Debug "Probing for WindowTitleSupported errored: $_"
}

# Get the default prompt definition.
$initialSessionState = [Runspace]::DefaultRunspace.InitialSessionState
if (!$initialSessionState.Commands -or !$initialSessionState.Commands['prompt']) {
Expand All @@ -43,10 +31,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,36 +76,8 @@ $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
# 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
}
}
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"
}
catch {
if ($global:PreviousWindowTitle) {
$Host.UI.RawUI.WindowTitle = $global:PreviousWindowTitle
}

Write-Debug "Error occurred during evaluation of `$GitPromptSettings.WindowTitle: $_"
}
}
}
Set-WindowTitle $global:GitStatus $IsAdmin

# If prompt timing enabled, write elapsed milliseconds
if ($settings.DefaultPromptEnableTiming) {
Expand Down Expand Up @@ -171,10 +127,7 @@ 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
}
Reset-WindowTitle

# Check if the posh-git prompt function itself has been replaced. If so, do not restore the prompt function
$promptDef = if ($funcInfo = Get-Command prompt -ErrorAction SilentlyContinue) { $funcInfo.Definition }
Expand Down