diff --git a/src/PoshGitTypes.ps1 b/src/PoshGitTypes.ps1 index 9eaf3ac23..65636ebb2 100644 --- a/src/PoshGitTypes.ps1 +++ b/src/PoshGitTypes.ps1 @@ -273,7 +273,7 @@ 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)"} - [PoshGitTextSpan]$DefaultPromptPrefix = '' + [PoshGitTextSpan]$DefaultPromptPrefix = '$(Get-PromptConnectionInfo -Format "[{1}@{0}]: ")' [PoshGitTextSpan]$DefaultPromptPath = '$(Get-PromptPath)' [PoshGitTextSpan]$DefaultPromptBeforeSuffix = '' [PoshGitTextSpan]$DefaultPromptDebug = [PoshGitTextSpan]::new(' [DBG]:', [ConsoleColor]::Magenta) diff --git a/src/Utils.ps1 b/src/Utils.ps1 index d677d32e1..879ffb9ef 100644 --- a/src/Utils.ps1 +++ b/src/Utils.ps1 @@ -301,6 +301,25 @@ function Get-PromptPath { return $currentPath } +<# +.SYNOPSIS + Gets a string with current machine name and user name when connected with SSH +.PARAMETER Format + Format string to use for displaying machine name ({0}) and user name ({1}). + Default: "[{1}@{0}]: ", i.e. "[user@machine]: " +.INPUTS + None +.OUTPUTS + [String] +#> +function Get-PromptConnectionInfo($Format = '[{1}@{0}]: ') { + if ($GitPromptSettings -and (Test-Path Env:SSH_CONNECTION)) { + $MachineName = [System.Environment]::MachineName + $UserName = [System.Environment]::UserName + $Format -f $MachineName,$UserName + } +} + function Get-PSModulePath { $modulePaths = $Env:PSModulePath -split ';' $modulePaths diff --git a/src/posh-git.psd1 b/src/posh-git.psd1 index c2a892d62..4382561b6 100644 --- a/src/posh-git.psd1 +++ b/src/posh-git.psd1 @@ -29,6 +29,7 @@ FunctionsToExport = @( 'Get-GitBranchStatusColor', 'Get-GitStatus', 'Get-GitDirectory', + 'Get-PromptConnectionInfo', 'Get-PromptPath', 'Update-AllBranches', 'Write-GitStatus', diff --git a/src/posh-git.psm1 b/src/posh-git.psm1 index 279171537..b06fa99eb 100644 --- a/src/posh-git.psm1 +++ b/src/posh-git.psm1 @@ -194,6 +194,7 @@ $exportModuleMemberParams = @{ 'Get-GitBranchStatusColor', 'Get-GitDirectory', 'Get-GitStatus', + 'Get-PromptConnectionInfo', 'Get-PromptPath', 'Update-AllBranches', 'Write-GitStatus', diff --git a/test/Utils.Tests.ps1 b/test/Utils.Tests.ps1 index 2a9a7ee84..99bc259c2 100644 --- a/test/Utils.Tests.ps1 +++ b/test/Utils.Tests.ps1 @@ -101,6 +101,42 @@ New-Alias pscore C:\Users\Keith\GitHub\rkeithhill\PowerShell\src\powershell-win- } } + Context 'Get-PromptConnectionInfo' { + BeforeEach { + if (Test-Path Env:SSH_CONNECTION) { + [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')] + $ssh_connection = $Env:SSH_CONNECTION + + Remove-Item Env:SSH_CONNECTION + } + } + AfterEach { + if ($ssh_connection) { + Set-Item Env:SSH_CONNECTION $ssh_connection + } elseif (Test-Path Env:SSH_CONNECTION) { + Remove-Item Env:SSH_CONNECTION + } + } + It 'Returns null if Env:SSH_CONNECTION is not set' { + Get-PromptConnectionInfo | Should BeExactly $null + } + It 'Returns null if Env:SSH_CONNECTION is empty' { + Set-Item Env:SSH_CONNECTION '' + + Get-PromptConnectionInfo | Should BeExactly $null + } + It 'Returns "[username@hostname]: " if Env:SSH_CONNECTION is set' { + Set-Item Env:SSH_CONNECTION 'test' + + Get-PromptConnectionInfo | Should BeExactly "[$([System.Environment]::UserName)@$([System.Environment]::MachineName)]: " + } + It 'Returns formatted string if Env:SSH_CONNECTION is set with -Format' { + Set-Item Env:SSH_CONNECTION 'test' + + Get-PromptConnectionInfo -Format "[{0}]({1}) " | Should BeExactly "[$([System.Environment]::MachineName)]($([System.Environment]::UserName)) " + } + } + Context 'Test-PoshGitImportedInScript Tests' { BeforeEach { [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')]