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

Rename tabs to match the repo and branch when in powershell ise #605

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions src/PoshGitTypes.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ class PoshGitPromptSettings {
[string]$DescribeStyle = ''
[psobject]$WindowTitle = {param($GitStatus, [bool]$IsAdmin) "$(if ($IsAdmin) {'Admin: '})$(if ($GitStatus) {"$($GitStatus.RepoName) [$($GitStatus.Branch)]"} else {Get-PromptPath}) ~ PowerShell $($PSVersionTable.PSVersion) $([IntPtr]::Size * 8)-bit ($PID)"}

[bool]$TabTitle = $true
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we should consider following the pattern that $WindowTitle uses. By specifying a scriptblock, users will be able to easily customize the text that gets put in the tab title. To simplify the scriptblock, you can use functions. Perhaps a Get-UniqueTabTitle to handle the logic for appending a number to the title to make it unique. Perhaps it would be something like this:

[psobject]$TabTitle = {param($GitStatus, [bool]$IsAdmin) "if ($GitStatus) { Get-UniqueTabTitle "$($GitStatus.RepoName) [$($GitStatus.Branch)]" }}


[PoshGitTextSpan]$DefaultPromptPrefix = '$(Get-PromptConnectionInfo -Format "[{1}@{0}]: ")'
[PoshGitTextSpan]$DefaultPromptPath = '$(Get-PromptPath)'
[PoshGitTextSpan]$DefaultPromptBeforeSuffix = ''
Expand Down
42 changes: 42 additions & 0 deletions src/WindowTitle.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,45 @@ function Set-WindowTitle {
}
}
}

function Set-TabTitle {
Copy link
Collaborator

Choose a reason for hiding this comment

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

If we change the setting to be a scriptblock, then this function becomes very similar to the Set-WindowTitle function above except that where it sets the title, it uses $psise.CurrentPowerShellTab.DisplayName = …

[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")]
param($GitStatus)
$settings = $global:GitPromptSettings

if ($settings.TabTitle == $false) {
return
}

# If the user is running Powershell ISE then name the tab
if($psISE -and $GitStatus){
Copy link
Collaborator

Choose a reason for hiding this comment

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

The code style for this project uses a single space after keywords like if, foreach, etc and a single space before an opening {.

$existingTabNames = $psISE.PowerShellTabs | % {$_.DisplayName}
$currentTabName = $psise.CurrentPowerShellTab.DisplayName
$tabName = Get-TabTitle $GitStatus $existingTabNames $currentTabName
$psise.CurrentPowerShellTab.DisplayName = $tabName
}
}

function Get-TabTitle {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we change this to Get-UniqueTableTitle with a single parameter of $Title and this function simply ensures that $Title is unique and if not, it will append numbers until a unique title is found.

Copy link
Owner

@dahlbyk dahlbyk Aug 18, 2018

Choose a reason for hiding this comment

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

👍

I think you can simplify a bit, too:

function Get-UniqueTabDisplayName ($Name, $Format="{0} {1}") {
  $tabNumber = 1
  $names = $psISE.PowerShellTabs | Select-Object -ExpandProperty DisplayName
  $newName = $Name
  while ($names -Contains $newName) {
    $newName = $Format -f $Name,$tabNumber++
  }
  return $newName
}

Note I dropped the parentheses to align with ISE's default numbering scheme.

Copy link
Author

Choose a reason for hiding this comment

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

I think I tried it like this initially but noticed an edge case where if you open a few tabs to the same repo i.e. posh-git [master], posh-git [master] (1), posh-git [master] (2) but then you close tab posh-git [master] (1) and open a new tab at the same repo it tries to create a new tab called posh-git [master] (2) and fails. Hence the logic to get the highest number that has been assigned so far.

Copy link
Owner

Choose a reason for hiding this comment

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

I believe this correctly fills in the gaps:
image
image
image

That said, this does open the possibility of changing branches/tabs such that you get:

[ "posh-git [master]", "posh-git [master] (1)", "posh-git [master] (2)"]
[ "posh-git [master]", "posh-git [george]", "posh-git [master] (2)"]
[ "posh-git [master]", "posh-git [george]", "posh-git [george] (1)"]
[ "posh-git [george] (2)", "posh-git [george]", "posh-git [george] (1)"]

As opposed to:

[ "posh-git [master]", "posh-git [master] (1)", "posh-git [master] (2)"]
[ "posh-git [master]", "posh-git [george] (1)", "posh-git [master] (2)"]
[ "posh-git [master]", "posh-git [george] (1)", "posh-git [george] (2)"]
[ "posh-git [george]", "posh-git [george] (1)", "posh-git [george] (2)"]

Copy link
Author

Choose a reason for hiding this comment

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

You're right this is much better and simpler. I hadn't looked at your looping logic properly I thought it was simply counting the number of tabs with the same name and using that count to name the tab. Wrist slapped.
Personally, I don't really think it matters what the number is as long as it's unique and doesn't change for a given tab.

[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")]
param($GitStatus, [string[]]$existingTabNames, [string]$currentTabName)

$repo = $GitStatus.RepoName
$branch = $GitStatus.Branch
$tabName = "$repo [$branch]"
#you can't have 2 tabs with the same name so shove a number on the end
$tabCount = 0
foreach($existingTabName in $existingTabNames){
if($existingTabName.StartsWith($tabName) -and $existingTabName -ne $currentTabName){
$tabCount++
$tabNumber = [int]$existingTabName.Replace($tabName, "").Replace("(", "").Replace(")", "").Trim()
if($tabCount -lt $tabNumber + 1){
$tabCount = $tabNumber + 1
}
}
}
if($tabCount -gt 0){
$tabName= "$tabName ($tabCount)"
}
return $tabName
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please add a final newline to this file.

1 change: 1 addition & 0 deletions src/posh-git.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ $GitPromptScriptBlock = {

# This has to be *after* the call to Write-VcsStatus, which populates $global:GitStatus
Set-WindowTitle $global:GitStatus $IsAdmin
Set-TabTitle $global:GitStatus
Copy link
Collaborator

Choose a reason for hiding this comment

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

Even if folks don't use $IsAdmin in their customized scriptblock, I think we should pass it. No harm in having this extra bit of info injected into the scriptblock.


# If prompt timing enabled, write elapsed milliseconds
if ($settings.DefaultPromptEnableTiming) {
Expand Down