Skip to content
This repository has been archived by the owner on Feb 16, 2021. It is now read-only.

Commit

Permalink
Makes Path Home Symbol Customizable
Browse files Browse the repository at this point in the history
The makes the tilde (`~`) used in home-relative paths be replaceable by themes.
  • Loading branch information
brucificus authored and JanDeDobbeleer committed Nov 10, 2019
1 parent e8eed6c commit ac27f09
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
40 changes: 38 additions & 2 deletions Helpers/Prompt.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ $global:ThemeSettings = New-Object -TypeName PSObject -Property @{
SegmentSeparatorForwardSymbol = '>'
SegmentSeparatorBackwardSymbol = '<'
PathSeparator = '\'
HomeSymbol = '*'
}
}

Expand Down Expand Up @@ -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'}
Expand Down Expand Up @@ -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' }
Expand Down
10 changes: 5 additions & 5 deletions Helpers/Prompt.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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('\')
Expand Down Expand Up @@ -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
)

Expand All @@ -142,7 +142,7 @@ function Get-ShortPath {
}
else {
if ($dir.path -eq (Get-Home)) {
return '~'
return $sl.PromptSymbols.HomeSymbol
}
return "$($dir.Drive.Name):"
}
Expand Down
2 changes: 1 addition & 1 deletion Themes/robbyrussell.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
}
Expand Down
1 change: 1 addition & 0 deletions defaults.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ac27f09

Please sign in to comment.