Skip to content

Commit

Permalink
Add option to abbreviate based path based on the git root
Browse files Browse the repository at this point in the history
  • Loading branch information
Philippe Elsass committed Nov 25, 2019
1 parent 1e9587f commit 3fa372c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/PoshGitTypes.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ class PoshGitPromptSettings {
[PoshGitTextSpan]$DefaultPromptSuffix = '$(">" * ($nestedPromptLevel + 1)) '

[bool]$DefaultPromptAbbreviateHomeDirectory = $true
[bool]$DefaultPromptAbbreviateGitDirectory = $false
[bool]$DefaultPromptWriteStatusFirst = $false
[bool]$DefaultPromptEnableTiming = $false
[PoshGitTextSpan]$DefaultPromptTimingFormat = ' {0}ms'
Expand Down
15 changes: 14 additions & 1 deletion src/Utils.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ function Get-PathStringComparison {
function Get-PromptPath {
$settings = $global:GitPromptSettings
$abbrevHomeDir = $settings -and $settings.DefaultPromptAbbreviateHomeDirectory
$abbrevGitDir = $settings -and $settings.DefaultPromptAbbreviateGitDirectory

# A UNC path has no drive so it's better to use the ProviderPath e.g. "\\server\share".
# However for any path with a drive defined, it's better to use the Path property.
Expand All @@ -289,10 +290,22 @@ function Get-PromptPath {
$pathInfo = $ExecutionContext.SessionState.Path.CurrentLocation
$currentPath = if ($pathInfo.Drive) { $pathInfo.Path } else { $pathInfo.ProviderPath }

# Look up the git root
if ($abbrevGitDir) {
$gitPath = $(Get-GitDirectory)
if ($gitPath) { $gitPath = $(Split-Path $gitPath -Parent) }
}

$stringComparison = Get-PathStringComparison

# Abbreviate path by replacing beginning of path with - *iff* the path is under a git repository
if ($abbrevGitDir -and $currentPath -and $gitPath -and
$currentPath.StartsWith($gitPath, $stringComparison)) {
$removePath = $(Split-Path $gitPath -Parent)
$currentPath = "-" + $currentPath.SubString($removePath.Length)
}
# Abbreviate path by replacing beginning of path with ~ *iff* the path is under the user's home dir
if ($abbrevHomeDir -and $currentPath -and !$currentPath.Equals($Home, $stringComparison) -and
elseif ($abbrevHomeDir -and $currentPath -and !$currentPath.Equals($Home, $stringComparison) -and
$currentPath.StartsWith($Home, $stringComparison)) {

$currentPath = "~" + $currentPath.SubString($Home.Length)
Expand Down

0 comments on commit 3fa372c

Please sign in to comment.