From bd3c4037dbac9e52f8ced4e2390ffd655252916f Mon Sep 17 00:00:00 2001 From: Keith Dahlby Date: Mon, 9 Jul 2018 01:12:04 -0500 Subject: [PATCH 1/5] Add Get-PromptConnectionInfo as DefaultPromptPrefix --- src/PoshGitTypes.ps1 | 2 +- src/Utils.ps1 | 14 ++++++++++++++ src/posh-git.psd1 | 1 + src/posh-git.psm1 | 1 + test/Utils.Tests.ps1 | 31 +++++++++++++++++++++++++++++++ 5 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/PoshGitTypes.ps1 b/src/PoshGitTypes.ps1 index 9eaf3ac23..6ca813b4a 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)' [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..37ec53e52 100644 --- a/src/Utils.ps1 +++ b/src/Utils.ps1 @@ -301,6 +301,20 @@ function Get-PromptPath { return $currentPath } +$sshType = 'System.Management.Automation.Runspaces.SSHConnectionInfo' -as [Type] +$sshUserName = Get-Runspace | ForEach-Object ConnectionInfo | + Where-Object { $sshType -and $_ -is $sshType } | ForEach-Object UserName + +function Get-PromptConnectionInfo { + if (Test-Path Env:SSH_CONNECTION) { + if ($sshUserName -and $sshUserName -ne [System.Environment]::UserName) { + "[$sshUserName@$([System.Environment]::MachineName)]: " + } else { + "[$([System.Environment]::MachineName)]: " + } + } +} + 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..ec98e82b7 100644 --- a/test/Utils.Tests.ps1 +++ b/test/Utils.Tests.ps1 @@ -101,6 +101,37 @@ 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 "[hostname]: " if Env:SSH_CONNECTION is set' { + Set-Item Env:SSH_CONNECTION 'test' + + Get-PromptConnectionInfo | Should BeExactly "[$([System.Environment]::MachineName)]: " + } + } + Context 'Test-PoshGitImportedInScript Tests' { BeforeEach { [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')] From ece15a396d0a2cbaa1cda670c8c741056d470317 Mon Sep 17 00:00:00 2001 From: Keith Dahlby Date: Mon, 9 Jul 2018 01:24:08 -0500 Subject: [PATCH 2/5] Make username comparison case-sensitive for Unix --- src/Utils.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Utils.ps1 b/src/Utils.ps1 index 37ec53e52..470d055b4 100644 --- a/src/Utils.ps1 +++ b/src/Utils.ps1 @@ -307,7 +307,7 @@ $sshUserName = Get-Runspace | ForEach-Object ConnectionInfo | function Get-PromptConnectionInfo { if (Test-Path Env:SSH_CONNECTION) { - if ($sshUserName -and $sshUserName -ne [System.Environment]::UserName) { + if ($sshUserName -and $sshUserName -cne [System.Environment]::UserName) { "[$sshUserName@$([System.Environment]::MachineName)]: " } else { "[$([System.Environment]::MachineName)]: " From 11899ad38d85f3c430355a2cee901b8f16f1990c Mon Sep 17 00:00:00 2001 From: Keith Dahlby Date: Tue, 10 Jul 2018 15:31:28 -0500 Subject: [PATCH 3/5] Always show SSH username --- src/Utils.ps1 | 10 +--------- test/Utils.Tests.ps1 | 4 ++-- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/src/Utils.ps1 b/src/Utils.ps1 index 470d055b4..977efc8eb 100644 --- a/src/Utils.ps1 +++ b/src/Utils.ps1 @@ -301,17 +301,9 @@ function Get-PromptPath { return $currentPath } -$sshType = 'System.Management.Automation.Runspaces.SSHConnectionInfo' -as [Type] -$sshUserName = Get-Runspace | ForEach-Object ConnectionInfo | - Where-Object { $sshType -and $_ -is $sshType } | ForEach-Object UserName - function Get-PromptConnectionInfo { if (Test-Path Env:SSH_CONNECTION) { - if ($sshUserName -and $sshUserName -cne [System.Environment]::UserName) { - "[$sshUserName@$([System.Environment]::MachineName)]: " - } else { - "[$([System.Environment]::MachineName)]: " - } + "[$([System.Environment]::UserName)@$([System.Environment]::MachineName)]: " } } diff --git a/test/Utils.Tests.ps1 b/test/Utils.Tests.ps1 index ec98e82b7..db58aea8f 100644 --- a/test/Utils.Tests.ps1 +++ b/test/Utils.Tests.ps1 @@ -125,10 +125,10 @@ New-Alias pscore C:\Users\Keith\GitHub\rkeithhill\PowerShell\src\powershell-win- Get-PromptConnectionInfo | Should BeExactly $null } - It 'Returns "[hostname]: " if Env:SSH_CONNECTION is set' { + It 'Returns "[username@hostname]: " if Env:SSH_CONNECTION is set' { Set-Item Env:SSH_CONNECTION 'test' - Get-PromptConnectionInfo | Should BeExactly "[$([System.Environment]::MachineName)]: " + Get-PromptConnectionInfo | Should BeExactly "[$([System.Environment]::UserName)@$([System.Environment]::MachineName)]: " } } From b1953e87bab38cb162e8408e408a6966bc512679 Mon Sep 17 00:00:00 2001 From: Keith Dahlby Date: Tue, 10 Jul 2018 21:50:20 -0500 Subject: [PATCH 4/5] Add $GitPromptSettings.DefaultPromptConnectionFormat {0} = hostname {1} = username --- src/PoshGitTypes.ps1 | 1 + src/Utils.ps1 | 9 +++++++-- test/Utils.Tests.ps1 | 11 +++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/PoshGitTypes.ps1 b/src/PoshGitTypes.ps1 index 6ca813b4a..807c47251 100644 --- a/src/PoshGitTypes.ps1 +++ b/src/PoshGitTypes.ps1 @@ -280,6 +280,7 @@ class PoshGitPromptSettings { [PoshGitTextSpan]$DefaultPromptSuffix = '$(">" * ($nestedPromptLevel + 1)) ' [bool]$DefaultPromptAbbreviateHomeDirectory = $true + [string]$DefaultPromptConnectionFormat = '[{1}@{0}]: ' [bool]$DefaultPromptWriteStatusFirst = $false [bool]$DefaultPromptEnableTiming = $false [PoshGitTextSpan]$DefaultPromptTimingFormat = ' {0}ms' diff --git a/src/Utils.ps1 b/src/Utils.ps1 index 977efc8eb..285e88d29 100644 --- a/src/Utils.ps1 +++ b/src/Utils.ps1 @@ -302,8 +302,13 @@ function Get-PromptPath { } function Get-PromptConnectionInfo { - if (Test-Path Env:SSH_CONNECTION) { - "[$([System.Environment]::UserName)@$([System.Environment]::MachineName)]: " + if ($GitPromptSettings -and (Test-Path Env:SSH_CONNECTION)) { + $format = $GitPromptSettings.DefaultPromptConnectionFormat + if (!$format) { return } + + $MachineName = [System.Environment]::MachineName + $UserName = [System.Environment]::UserName + $format -f $MachineName,$UserName } } diff --git a/test/Utils.Tests.ps1 b/test/Utils.Tests.ps1 index db58aea8f..79d9e103f 100644 --- a/test/Utils.Tests.ps1 +++ b/test/Utils.Tests.ps1 @@ -103,6 +103,9 @@ New-Alias pscore C:\Users\Keith\GitHub\rkeithhill\PowerShell\src\powershell-win- Context 'Get-PromptConnectionInfo' { BeforeEach { + [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')] + $format = $GitPromptSettings.DefaultPromptConnectionFormat + if (Test-Path Env:SSH_CONNECTION) { [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')] $ssh_connection = $Env:SSH_CONNECTION @@ -111,6 +114,8 @@ New-Alias pscore C:\Users\Keith\GitHub\rkeithhill\PowerShell\src\powershell-win- } } AfterEach { + $GitPromptSettings.DefaultPromptConnectionFormat = $format + if ($ssh_connection) { Set-Item Env:SSH_CONNECTION $ssh_connection } elseif (Test-Path Env:SSH_CONNECTION) { @@ -130,6 +135,12 @@ New-Alias pscore C:\Users\Keith\GitHub\rkeithhill\PowerShell\src\powershell-win- Get-PromptConnectionInfo | Should BeExactly "[$([System.Environment]::UserName)@$([System.Environment]::MachineName)]: " } + It 'Returns custom string if Env:SSH_CONNECTION is set with custom format' { + Set-Item Env:SSH_CONNECTION 'test' + $GitPromptSettings.DefaultPromptConnectionFormat = "[{0}]({1}) " + + Get-PromptConnectionInfo | Should BeExactly "[$([System.Environment]::MachineName)]($([System.Environment]::UserName)) " + } } Context 'Test-PoshGitImportedInScript Tests' { From 5ff35d3a405c60a13d78e38f7a8868c77b1b5e38 Mon Sep 17 00:00:00 2001 From: Keith Dahlby Date: Wed, 11 Jul 2018 14:46:56 -0500 Subject: [PATCH 5/5] Replace DefaultPromptConnectionFormat with -Format param --- src/PoshGitTypes.ps1 | 3 +-- src/Utils.ps1 | 18 +++++++++++++----- test/Utils.Tests.ps1 | 10 ++-------- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/PoshGitTypes.ps1 b/src/PoshGitTypes.ps1 index 807c47251..65636ebb2 100644 --- a/src/PoshGitTypes.ps1 +++ b/src/PoshGitTypes.ps1 @@ -273,14 +273,13 @@ 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 = '$(Get-PromptConnectionInfo)' + [PoshGitTextSpan]$DefaultPromptPrefix = '$(Get-PromptConnectionInfo -Format "[{1}@{0}]: ")' [PoshGitTextSpan]$DefaultPromptPath = '$(Get-PromptPath)' [PoshGitTextSpan]$DefaultPromptBeforeSuffix = '' [PoshGitTextSpan]$DefaultPromptDebug = [PoshGitTextSpan]::new(' [DBG]:', [ConsoleColor]::Magenta) [PoshGitTextSpan]$DefaultPromptSuffix = '$(">" * ($nestedPromptLevel + 1)) ' [bool]$DefaultPromptAbbreviateHomeDirectory = $true - [string]$DefaultPromptConnectionFormat = '[{1}@{0}]: ' [bool]$DefaultPromptWriteStatusFirst = $false [bool]$DefaultPromptEnableTiming = $false [PoshGitTextSpan]$DefaultPromptTimingFormat = ' {0}ms' diff --git a/src/Utils.ps1 b/src/Utils.ps1 index 285e88d29..879ffb9ef 100644 --- a/src/Utils.ps1 +++ b/src/Utils.ps1 @@ -301,14 +301,22 @@ function Get-PromptPath { return $currentPath } -function Get-PromptConnectionInfo { +<# +.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)) { - $format = $GitPromptSettings.DefaultPromptConnectionFormat - if (!$format) { return } - $MachineName = [System.Environment]::MachineName $UserName = [System.Environment]::UserName - $format -f $MachineName,$UserName + $Format -f $MachineName,$UserName } } diff --git a/test/Utils.Tests.ps1 b/test/Utils.Tests.ps1 index 79d9e103f..99bc259c2 100644 --- a/test/Utils.Tests.ps1 +++ b/test/Utils.Tests.ps1 @@ -103,9 +103,6 @@ New-Alias pscore C:\Users\Keith\GitHub\rkeithhill\PowerShell\src\powershell-win- Context 'Get-PromptConnectionInfo' { BeforeEach { - [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')] - $format = $GitPromptSettings.DefaultPromptConnectionFormat - if (Test-Path Env:SSH_CONNECTION) { [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')] $ssh_connection = $Env:SSH_CONNECTION @@ -114,8 +111,6 @@ New-Alias pscore C:\Users\Keith\GitHub\rkeithhill\PowerShell\src\powershell-win- } } AfterEach { - $GitPromptSettings.DefaultPromptConnectionFormat = $format - if ($ssh_connection) { Set-Item Env:SSH_CONNECTION $ssh_connection } elseif (Test-Path Env:SSH_CONNECTION) { @@ -135,11 +130,10 @@ New-Alias pscore C:\Users\Keith\GitHub\rkeithhill\PowerShell\src\powershell-win- Get-PromptConnectionInfo | Should BeExactly "[$([System.Environment]::UserName)@$([System.Environment]::MachineName)]: " } - It 'Returns custom string if Env:SSH_CONNECTION is set with custom format' { + It 'Returns formatted string if Env:SSH_CONNECTION is set with -Format' { Set-Item Env:SSH_CONNECTION 'test' - $GitPromptSettings.DefaultPromptConnectionFormat = "[{0}]({1}) " - Get-PromptConnectionInfo | Should BeExactly "[$([System.Environment]::MachineName)]($([System.Environment]::UserName)) " + Get-PromptConnectionInfo -Format "[{0}]({1}) " | Should BeExactly "[$([System.Environment]::MachineName)]($([System.Environment]::UserName)) " } }