Skip to content

Commit bd3c403

Browse files
committed
Add Get-PromptConnectionInfo as DefaultPromptPrefix
1 parent 3dd9fdc commit bd3c403

File tree

5 files changed

+48
-1
lines changed

5 files changed

+48
-1
lines changed

src/PoshGitTypes.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ 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-
[PoshGitTextSpan]$DefaultPromptPrefix = ''
276+
[PoshGitTextSpan]$DefaultPromptPrefix = '$(Get-PromptConnectionInfo)'
277277
[PoshGitTextSpan]$DefaultPromptPath = '$(Get-PromptPath)'
278278
[PoshGitTextSpan]$DefaultPromptBeforeSuffix = ''
279279
[PoshGitTextSpan]$DefaultPromptDebug = [PoshGitTextSpan]::new(' [DBG]:', [ConsoleColor]::Magenta)

src/Utils.ps1

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,20 @@ function Get-PromptPath {
301301
return $currentPath
302302
}
303303

304+
$sshType = 'System.Management.Automation.Runspaces.SSHConnectionInfo' -as [Type]
305+
$sshUserName = Get-Runspace | ForEach-Object ConnectionInfo |
306+
Where-Object { $sshType -and $_ -is $sshType } | ForEach-Object UserName
307+
308+
function Get-PromptConnectionInfo {
309+
if (Test-Path Env:SSH_CONNECTION) {
310+
if ($sshUserName -and $sshUserName -ne [System.Environment]::UserName) {
311+
"[$sshUserName@$([System.Environment]::MachineName)]: "
312+
} else {
313+
"[$([System.Environment]::MachineName)]: "
314+
}
315+
}
316+
}
317+
304318
function Get-PSModulePath {
305319
$modulePaths = $Env:PSModulePath -split ';'
306320
$modulePaths

src/posh-git.psd1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ FunctionsToExport = @(
2929
'Get-GitBranchStatusColor',
3030
'Get-GitStatus',
3131
'Get-GitDirectory',
32+
'Get-PromptConnectionInfo',
3233
'Get-PromptPath',
3334
'Update-AllBranches',
3435
'Write-GitStatus',

src/posh-git.psm1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ $exportModuleMemberParams = @{
194194
'Get-GitBranchStatusColor',
195195
'Get-GitDirectory',
196196
'Get-GitStatus',
197+
'Get-PromptConnectionInfo',
197198
'Get-PromptPath',
198199
'Update-AllBranches',
199200
'Write-GitStatus',

test/Utils.Tests.ps1

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,37 @@ New-Alias pscore C:\Users\Keith\GitHub\rkeithhill\PowerShell\src\powershell-win-
101101
}
102102
}
103103

104+
Context 'Get-PromptConnectionInfo' {
105+
BeforeEach {
106+
if (Test-Path Env:SSH_CONNECTION) {
107+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')]
108+
$ssh_connection = $Env:SSH_CONNECTION
109+
110+
Remove-Item Env:SSH_CONNECTION
111+
}
112+
}
113+
AfterEach {
114+
if ($ssh_connection) {
115+
Set-Item Env:SSH_CONNECTION $ssh_connection
116+
} elseif (Test-Path Env:SSH_CONNECTION) {
117+
Remove-Item Env:SSH_CONNECTION
118+
}
119+
}
120+
It 'Returns null if Env:SSH_CONNECTION is not set' {
121+
Get-PromptConnectionInfo | Should BeExactly $null
122+
}
123+
It 'Returns null if Env:SSH_CONNECTION is empty' {
124+
Set-Item Env:SSH_CONNECTION ''
125+
126+
Get-PromptConnectionInfo | Should BeExactly $null
127+
}
128+
It 'Returns "[hostname]: " if Env:SSH_CONNECTION is set' {
129+
Set-Item Env:SSH_CONNECTION 'test'
130+
131+
Get-PromptConnectionInfo | Should BeExactly "[$([System.Environment]::MachineName)]: "
132+
}
133+
}
134+
104135
Context 'Test-PoshGitImportedInScript Tests' {
105136
BeforeEach {
106137
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')]

0 commit comments

Comments
 (0)