Skip to content

Commit 8e359f9

Browse files
committed
I've refactored the code based on recommendations from the contributors
1 parent fc5dec1 commit 8e359f9

File tree

4 files changed

+45
-23
lines changed

4 files changed

+45
-23
lines changed

src/GitPrompt.ps1

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -209,29 +209,6 @@ function Write-GitStatus {
209209
}
210210

211211
$sb.ToString()
212-
213-
# If the user is running Powershell ISE then name the tab
214-
if($psISE){
215-
$repo = $Status.RepoName
216-
$branch = $Status.Branch
217-
$tabName = "$repo [$branch]"
218-
#you can't have 2 tabs with the same name so shove a number on the end
219-
$tabCount = 0
220-
foreach($tab in $psISE.PowerShellTabs){
221-
$existingTabName = $tab.DisplayName
222-
if($existingTabName.StartsWith($tabName) -and $existingTabName -ne $psise.CurrentPowerShellTab.DisplayName){
223-
$tabCount++
224-
$tabNumber = [int]$existingTabName.Replace($tabName, "").Replace("(", "").Replace(")", "").Trim()
225-
if($tabCount -lt $tabNumber + 1){
226-
$tabCount = $tabNumber + 1
227-
}
228-
}
229-
}
230-
if($tabCount -gt 0){
231-
$tabName= "$tabName ($tabCount)"
232-
}
233-
$psise.CurrentPowerShellTab.DisplayName = $tabName
234-
}
235212
}
236213

237214
<#

src/PoshGitTypes.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ class PoshGitPromptSettings {
273273
[string]$DescribeStyle = ''
274274
[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)"}
275275

276+
[bool]$TabTitle = $true
277+
276278
[PoshGitTextSpan]$DefaultPromptPrefix = '$(Get-PromptConnectionInfo -Format "[{1}@{0}]: ")'
277279
[PoshGitTextSpan]$DefaultPromptPath = '$(Get-PromptPath)'
278280
[PoshGitTextSpan]$DefaultPromptBeforeSuffix = ''

src/WindowTitle.ps1

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,45 @@ function Set-WindowTitle {
5858
}
5959
}
6060
}
61+
62+
function Set-TabTitle {
63+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")]
64+
param($GitStatus)
65+
$settings = $global:GitPromptSettings
66+
67+
if ($settings.TabTitle == $false) {
68+
return
69+
}
70+
71+
# If the user is running Powershell ISE then name the tab
72+
if($psISE -and $GitStatus){
73+
$existingTabNames = $psISE.PowerShellTabs | % {$_.DisplayName}
74+
$currentTabName = $psise.CurrentPowerShellTab.DisplayName
75+
$tabName = Get-TabTitle $GitStatus $existingTabNames $currentTabName
76+
$psise.CurrentPowerShellTab.DisplayName = $tabName
77+
}
78+
}
79+
80+
function Get-TabTitle {
81+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")]
82+
param($GitStatus, [string[]]$existingTabNames, [string]$currentTabName)
83+
84+
$repo = $GitStatus.RepoName
85+
$branch = $GitStatus.Branch
86+
$tabName = "$repo [$branch]"
87+
#you can't have 2 tabs with the same name so shove a number on the end
88+
$tabCount = 0
89+
foreach($existingTabName in $existingTabNames){
90+
if($existingTabName.StartsWith($tabName) -and $existingTabName -ne $currentTabName){
91+
$tabCount++
92+
$tabNumber = [int]$existingTabName.Replace($tabName, "").Replace("(", "").Replace(")", "").Trim()
93+
if($tabCount -lt $tabNumber + 1){
94+
$tabCount = $tabNumber + 1
95+
}
96+
}
97+
}
98+
if($tabCount -gt 0){
99+
$tabName= "$tabName ($tabCount)"
100+
}
101+
return $tabName
102+
}

src/posh-git.psm1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ $GitPromptScriptBlock = {
7878

7979
# This has to be *after* the call to Write-VcsStatus, which populates $global:GitStatus
8080
Set-WindowTitle $global:GitStatus $IsAdmin
81+
Set-TabTitle $global:GitStatus
8182

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

0 commit comments

Comments
 (0)