Skip to content
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

Add Get-PromptConnectionInfo as DefaultPromptPrefix #595

Merged
merged 5 commits into from
Jul 11, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/PoshGitTypes.ps1
Original file line number Diff line number Diff line change
@@ -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)
19 changes: 19 additions & 0 deletions src/Utils.ps1
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions src/posh-git.psd1
Original file line number Diff line number Diff line change
@@ -29,6 +29,7 @@ FunctionsToExport = @(
'Get-GitBranchStatusColor',
'Get-GitStatus',
'Get-GitDirectory',
'Get-PromptConnectionInfo',
'Get-PromptPath',
'Update-AllBranches',
'Write-GitStatus',
1 change: 1 addition & 0 deletions src/posh-git.psm1
Original file line number Diff line number Diff line change
@@ -194,6 +194,7 @@ $exportModuleMemberParams = @{
'Get-GitBranchStatusColor',
'Get-GitDirectory',
'Get-GitStatus',
'Get-PromptConnectionInfo',
'Get-PromptPath',
'Update-AllBranches',
'Write-GitStatus',
36 changes: 36 additions & 0 deletions test/Utils.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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', '')]