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

Improved WindowTitle customization experience #541

Merged
merged 7 commits into from
Jan 29, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
50 changes: 34 additions & 16 deletions src/GitPrompt.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Inspired by Mark Embling
# http://www.markembling.info/view/my-ideal-powershell-prompt-with-git-integration

$global:GitPromptSettings = [GitPromptSettings]::new()
$global:GitPromptSettings = [PoshGitPromptSettings]::new()

# Override some of the normal colors if the background color is set to the default DarkMagenta.
$s = $global:GitPromptSettings
Expand All @@ -13,15 +13,19 @@ if ($Host.UI.RawUI.BackgroundColor -eq [ConsoleColor]::DarkMagenta) {
$s.WorkingColor.ForegroundColor = 'Red'
}

$isAdminProcess = Test-Administrator
$adminHeader = if ($isAdminProcess) { 'Administrator: ' } else { '' }
$IsAdmin = Test-Administrator

$WindowTitleSupported = $true
# TODO: Hmm, this is a curious way to detemine window title supported
# Could do $host.Name -eq "Package Manager Host" but that is kinda specific
# Could attempt to change it and catch any exception and then set this to $false
if (Get-Module NuGet) {
$WindowTitleSupported = $false
# Probe $Host.UI.RawUI.WindowTitle to see if it can be set without errors
$WindowTitleSupported = $false
try {
$global:PreviousWindowTitle = $Host.UI.RawUI.WindowTitle
$newTitle = "$origTitle "
Copy link
Owner

Choose a reason for hiding this comment

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

Should this be "$PreviousWindowTitle "?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Doh! Yes it should be. Thanks. I'll fix it tonight.

$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: $_"
}

<#
Expand Down Expand Up @@ -218,14 +222,28 @@ function Write-GitStatus {

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

if ($WindowTitleSupported -and $s.EnableWindowTitle) {
if (!$global:PreviousWindowTitle) {
$global:PreviousWindowTitle = $Host.UI.RawUI.WindowTitle
if ($WindowTitleSupported) {
if (!$s.EnableWindowTitle) {
if ($global:PreviousWindowTitle) {
$Host.UI.RawUI.WindowTitle = $global:PreviousWindowTitle
}
}
else {
try {
if ($s.WindowTitle -is [scriptblock]) {
$windowTitleText = & $s.WindowTitle $Status $IsAdmin
}
else {
$windowTitleText = $ExecutionContext.SessionState.InvokeCommand.ExpandString("$($s.WindowTitle)")
}

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

$repoName = Split-Path -Leaf (Split-Path $status.GitDir)
$prefix = if ($s.EnableWindowTitle -is [string]) { $s.EnableWindowTitle } else { '' }
$Host.UI.RawUI.WindowTitle = "${script:adminHeader}${prefix}${repoName} [$($Status.Branch)]"
}

return $sb.ToString()
Expand Down
1 change: 1 addition & 0 deletions src/GitUtils.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ function Get-GitStatus {

$result = New-Object PSObject -Property @{
GitDir = $GitDir
RepoName = Split-Path (Split-Path $GitDir -Parent) -Leaf
Branch = $branch
AheadBy = $aheadBy
BehindBy = $behindBy
Expand Down
7 changes: 4 additions & 3 deletions src/PoshGitTypes.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ class PoshGitTextSpan {
}
}

class GitPromptSettings {
class PoshGitPromptSettings {
[bool]$AnsiConsole = $Host.UI.SupportsVirtualTerminal -or ($Env:ConEmuANSI -eq "ON")

[PoshGitCellColor]$DefaultColor = [PoshGitCellColor]::new()
Expand Down Expand Up @@ -244,8 +244,9 @@ class GitPromptSettings {
[Nullable[bool]]$EnableFileStatusFromCache = $null
[string[]]$RepositoriesInWhichToDisableFileStatus = @()

[string]$DescribeStyle = ''
[psobject]$EnableWindowTitle = 'posh~git ~ '
[string]$DescribeStyle = ''
[bool]$EnableWindowTitle = $true
Copy link
Owner

Choose a reason for hiding this comment

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

Also, I think we might want to keep EnableWindowTitle as a simple bool. If you disable the WindowTitle by setting it to $null, you lose the original definition.

I'm not too worried about this, since resetting is as simple as opening a new shell. Fewer settings seems better than many.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Well, we have reduced the number of "top-level" settings a lot. :-) But I don't feel that strongly about this.

[psobject]$WindowTitle = {param($GitStatus, [bool]$IsAdmin) "$(if ($IsAdmin) {'Admin: '})posh~git ~ $($GitStatus.RepoName) [$($GitStatus.Branch)]"}

[PoshGitTextSpan]$DefaultPromptPrefix = ''
[PoshGitTextSpan]$DefaultPromptSuffix = '$(''>'' * ($nestedPromptLevel + 1)) '
Expand Down
62 changes: 60 additions & 2 deletions test/DefaultPrompt.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Describe 'Default Prompt Tests - NO ANSI' {
}
BeforeEach {
# Ensure these settings start out set to the default values
$global:GitPromptSettings = & $module.NewBoundScriptBlock({[GitPromptSettings]::new()})
$global:GitPromptSettings = & $module.NewBoundScriptBlock({[PoshGitPromptSettings]::new()})
$GitPromptSettings.AnsiConsole = $false
}

Expand Down Expand Up @@ -99,7 +99,7 @@ Describe 'Default Prompt Tests - ANSI' {
}
BeforeEach {
# Ensure these settings start out set to the default values
$global:GitPromptSettings = & $module.NewBoundScriptBlock({[GitPromptSettings]::new()})
$global:GitPromptSettings = & $module.NewBoundScriptBlock({[PoshGitPromptSettings]::new()})
$GitPromptSettings.AnsiConsole = $true
}

Expand Down Expand Up @@ -194,3 +194,61 @@ A test/Foo.Tests.ps1
}
}
}

Describe 'Default Prompt Window Title Tests' {
BeforeEach {
# Ensure these settings start out set to the default values
$defaultTitle = if ($IsWindows) { "Windows PowerShell" } else { "PowerShell-$($PSVersionTable.PSVersion)" }
$Host.UI.RawUI.WindowTitle = $defaultTitle
$global:PreviousWindowTitle = $defaultTitle
$global:GitPromptSettings = & $module.NewBoundScriptBlock({[PoshGitPromptSettings]::new()})
}

Context 'Default WindowTitle / EnableWindowTitle work ' {
It 'Sets the expected Window title text' {
Mock -ModuleName posh-git -CommandName git {
$OFS = " "
if ($args -contains 'rev-parse') {
$res = Invoke-Expression "&$gitbin $args"
return $res
}
Convert-NativeLineEnding -SplitLines @'
## master
A test/Foo.Tests.ps1
D test/Bar.Tests.ps1
M test/Baz.Tests.ps1

'@
}

Set-Location $PSScriptRoot
& $GitPromptScriptBlock *>$null
Assert-MockCalled git -ModuleName posh-git -Scope It
$title = $Host.UI.RawUI.WindowTitle
$title | Should Match '(Admin: )?posh~git ~ posh-git \[master\]'
}
It 'Does not set Window title when EnableWindowText is $false' {
Mock -ModuleName posh-git -CommandName git {
$OFS = " "
if ($args -contains 'rev-parse') {
$res = Invoke-Expression "&$gitbin $args"
return $res
}
Convert-NativeLineEnding -SplitLines @'
## master
A test/Foo.Tests.ps1
D test/Bar.Tests.ps1
M test/Baz.Tests.ps1

'@
}

Set-Location $PSScriptRoot
$GitPromptSettings.EnableWindowTitle = 0
& $GitPromptScriptBlock *>$null
Assert-MockCalled git -ModuleName posh-git -Scope It
$title = $Host.UI.RawUI.WindowTitle
$title | Should Match '^(Windows )?PowerShell'
}
}
}