-
-
Notifications
You must be signed in to change notification settings - Fork 807
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
base: master
Are you sure you want to change the base?
Changes from 4 commits
0fb0b9a
fc5dec1
b8d0042
b1b7393
24ee260
d1ab53e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,3 +58,45 @@ function Set-WindowTitle { | |
} | ||
} | ||
} | ||
|
||
function Set-TabTitle { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")] | ||
param($GitStatus) | ||
$settings = $global:GitPromptSettings | ||
|
||
if ($settings.TabTitle -eq $false) { | ||
return | ||
} | ||
|
||
# If the user is running Powershell ISE then name the tab | ||
if($psISE -and $GitStatus){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
$existingTabNames = $psISE.PowerShellTabs | % {$_.DisplayName} | ||
$currentTabName = $psise.CurrentPowerShellTab.DisplayName | ||
$tabName = Get-TabTitle $GitStatus $existingTabNames $currentTabName | ||
$psise.CurrentPowerShellTab.DisplayName = $tabName | ||
} | ||
} | ||
|
||
function Get-TabTitle { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we change this to There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this correctly fills in the gaps: That said, this does open the possibility of changing branches/tabs such that you get:
As opposed to:
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
[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 | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a final newline to this file. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
There was a problem hiding this comment.
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 aGet-UniqueTabTitle
to handle the logic for appending a number to the title to make it unique. Perhaps it would be something like this: