From ac27f09f1461a65499241002e37a78a46b4d4982 Mon Sep 17 00:00:00 2001 From: Bruce Markham <219281+brucificus@users.noreply.github.com> Date: Thu, 5 Sep 2019 09:47:45 -0400 Subject: [PATCH] Makes Path `Home` Symbol Customizable The makes the tilde (`~`) used in home-relative paths be replaceable by themes. --- Helpers/Prompt.Tests.ps1 | 40 ++++++++++++++++++++++++++++++++++++++-- Helpers/Prompt.ps1 | 10 +++++----- Themes/robbyrussell.psm1 | 2 +- defaults.ps1 | 1 + 4 files changed, 45 insertions(+), 8 deletions(-) diff --git a/Helpers/Prompt.Tests.ps1 b/Helpers/Prompt.Tests.ps1 index 30b5ff40..9b3c97a3 100644 --- a/Helpers/Prompt.Tests.ps1 +++ b/Helpers/Prompt.Tests.ps1 @@ -13,6 +13,7 @@ $global:ThemeSettings = New-Object -TypeName PSObject -Property @{ SegmentSeparatorForwardSymbol = '>' SegmentSeparatorBackwardSymbol = '<' PathSeparator = '\' + HomeSymbol = '*' } } @@ -93,12 +94,12 @@ Describe "Get-Drive" { It "is in the $HOME folder" { Mock Get-Home {return 'C:\Users\Jan'} $path = @{Drive = @{Name = 'C:'}; Path = 'C:\Users\Jan'} - Get-Drive $path | Should Be '~' + Get-Drive $path | Should Be $ThemeSettings.PromptSymbols.HomeSymbol } It "is somewhere in the $HOME folder" { Mock Get-Home {return 'C:\Users\Jan'} $path = @{Drive = @{Name = 'C:'}; Path = 'C:\Users\Jan\Git\Somewhere'} - Get-Drive $path | Should Be '~' + Get-Drive $path | Should Be $ThemeSettings.PromptSymbols.HomeSymbol } It "is in 'Microsoft.PowerShell.Core\FileSystem::\\Test\Hello' with provider X:" { $path = @{Drive = @{Name = 'X:'}; Path = 'Microsoft.PowerShell.Core\FileSystem::\\Test\Hello'} @@ -131,6 +132,41 @@ Describe "Get-Drive" { } } +Describe "Get-FullPath" { + Context "Running in the FileSystem" { + BeforeAll { Mock Get-Provider { return 'FileSystem'} } + It "is in the $HOME folder" { + Mock Get-Home {return 'C:\Users\Jan'} + $path = @{Drive = @{Name = 'C:'}; Path = 'C:\Users\Jan'} + Get-FullPath $path | Should Be $ThemeSettings.PromptSymbols.HomeSymbol + } + It "is somewhere in the $HOME folder" { + Mock Get-Home {return 'C:\Users\Jan'} + $path = @{Drive = @{Name = 'C:'}; Path = 'C:\Users\Jan\Git\Somewhere'} + Get-FullPath $path | Should BeLike "$($ThemeSettings.PromptSymbols.HomeSymbol)*" + } + } +} + +Describe "Get-ShortPath" { + Context "Running in the FileSystem" { + BeforeAll { + Mock Get-Provider { return 'FileSystem'} + Mock Get-Home {return 'C:\Users\Jan'} + } + It "is in the $HOME folder" { + $path = @{Drive = @{Name = 'C:'}; Path = 'C:\Users\Jan'} + Mock Get-Item { return $path } + Get-ShortPath $path | Should Be $ThemeSettings.PromptSymbols.HomeSymbol + } + It "is somewhere in the $HOME folder" { + Mock Get-Item { return $path } + $path = @{Drive = @{Name = 'C:'}; Path = 'C:\Users\Jan\Git\Somewhere'} + Get-ShortPath $path | Should BeLike "$($ThemeSettings.PromptSymbols.HomeSymbol)*" + } + } +} + Describe "Test-NotDefaultUser" { Context "With default user set" { BeforeAll { $DefaultUser = 'name' } diff --git a/Helpers/Prompt.ps1 b/Helpers/Prompt.ps1 index b799cfbe..52952b12 100644 --- a/Helpers/Prompt.ps1 +++ b/Helpers/Prompt.ps1 @@ -68,7 +68,7 @@ function Get-Drive { if($provider -eq 'FileSystem') { $homedir = Get-Home if($dir.Path.StartsWith($homedir)) { - return '~' + return $sl.PromptSymbols.HomeSymbol } elseif($dir.Path.StartsWith('Microsoft.PowerShell.Core')) { $parts = $dir.Path.Replace('Microsoft.PowerShell.Core\FileSystem::\\','').Split('\') @@ -101,21 +101,21 @@ function Test-IsVCSRoot { function Get-FullPath { param( [Parameter(Mandatory = $true)] - [System.Management.Automation.PathInfo] + [System.Object] $dir ) if ($dir.path -eq "$($dir.Drive.Name):\") { return "$($dir.Drive.Name):" } - $path = $dir.path.Replace((Get-Home),'~').Replace('\', $sl.PromptSymbols.PathSeparator) + $path = $dir.path.Replace((Get-Home),$sl.PromptSymbols.HomeSymbol).Replace('\', $sl.PromptSymbols.PathSeparator) return $path } function Get-ShortPath { param( [Parameter(Mandatory = $true)] - [System.Management.Automation.PathInfo] + [System.Object] $dir ) @@ -142,7 +142,7 @@ function Get-ShortPath { } else { if ($dir.path -eq (Get-Home)) { - return '~' + return $sl.PromptSymbols.HomeSymbol } return "$($dir.Drive.Name):" } diff --git a/Themes/robbyrussell.psm1 b/Themes/robbyrussell.psm1 index 5c45abf7..545ab7df 100644 --- a/Themes/robbyrussell.psm1 +++ b/Themes/robbyrussell.psm1 @@ -18,7 +18,7 @@ function Write-Theme { $prompt += Write-Prompt -Object ($sl.PromptSymbols.PromptIndicator + " ") -ForegroundColor $promtSymbolColor # Writes the drive portion - $drive = "~" + $drive = $sl.PromptSymbols.HomeSymbol if ($pwd.Path -ne $HOME) { $drive = "$(Split-Path -path $pwd -Leaf)" } diff --git a/defaults.ps1 b/defaults.ps1 index 64610ef4..cea63219 100644 --- a/defaults.ps1 +++ b/defaults.ps1 @@ -37,6 +37,7 @@ $global:ThemeSettings = New-Object -TypeName PSObject -Property @{ SegmentSeparatorBackwardSymbol = [char]::ConvertFromUtf32(0x26A1) PathSeparator = [System.IO.Path]::DirectorySeparatorChar VirtualEnvSymbol = [char]::ConvertFromUtf32(0xE606) + HomeSymbol = '~' } Colors = @{ GitDefaultColor = [ConsoleColor]::DarkGreen