Skip to content

Commit

Permalink
Merge pull request #595 from dahlbyk/ssh-host
Browse files Browse the repository at this point in the history
Add Get-PromptConnectionInfo as DefaultPromptPrefix
  • Loading branch information
dahlbyk authored Jul 11, 2018
2 parents 3dd9fdc + 5ff35d3 commit e5bf84c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/PoshGitTypes.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions src/Utils.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/posh-git.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ FunctionsToExport = @(
'Get-GitBranchStatusColor',
'Get-GitStatus',
'Get-GitDirectory',
'Get-PromptConnectionInfo',
'Get-PromptPath',
'Update-AllBranches',
'Write-GitStatus',
Expand Down
1 change: 1 addition & 0 deletions src/posh-git.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ $exportModuleMemberParams = @{
'Get-GitBranchStatusColor',
'Get-GitDirectory',
'Get-GitStatus',
'Get-PromptConnectionInfo',
'Get-PromptPath',
'Update-AllBranches',
'Write-GitStatus',
Expand Down
36 changes: 36 additions & 0 deletions test/Utils.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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', '')]
Expand Down

0 comments on commit e5bf84c

Please sign in to comment.