Skip to content

Commit

Permalink
Merge pull request #567 from dahlbyk/rkeithhill/window-title-mods
Browse files Browse the repository at this point in the history
Shorten window title text
  • Loading branch information
dahlbyk authored Apr 20, 2018
2 parents a8adab2 + aec5c95 commit dcb96a4
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/PoshGitTypes.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ class PoshGitPromptSettings {
[string[]]$RepositoriesInWhichToDisableFileStatus = @()

[string]$DescribeStyle = ''
[psobject]$WindowTitle = {param($GitStatus, [bool]$IsAdmin) "$(if ($IsAdmin) {'Administrator: '})$(if ($GitStatus) {"posh~git ~ $($GitStatus.RepoName) [$($GitStatus.Branch)] ~ "})PowerShell $($PSVersionTable.PSVersion) $([IntPtr]::Size * 8)-bit ($PID)"}
[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]$DefaultPromptPath = '$(Get-PromptPath)'
Expand Down
6 changes: 4 additions & 2 deletions src/Utils.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,10 @@ function Get-PromptPath {

$stringComparison = Get-PathStringComparison

# Abbreviate path by replacing beginning of path with ~ *iff* the path is in the user's home dir
if ($abbrevHomeDir -and $currentPath -and $currentPath.StartsWith($Home, $stringComparison)) {
# 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
$currentPath.StartsWith($Home, $stringComparison)) {

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

Expand Down
18 changes: 10 additions & 8 deletions test/DefaultPrompt.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Describe 'Default Prompt Tests - NO ANSI' {
Set-Location $Home -ErrorAction Stop
$GitPromptSettings.DefaultPromptAbbreviateHomeDirectory = $true
$res = [string](&$prompt *>&1)
$res | Should BeExactly "~> "
$res | Should BeExactly "$(GetHomePath)> "
}
It 'Returns the expected prompt string with DefaultPromptAbbreviateHomeDirectory disabled' {
Set-Location $Home -ErrorAction Stop
Expand All @@ -60,7 +60,7 @@ Describe 'Default Prompt Tests - NO ANSI' {
$GitPromptSettings.DefaultPromptSuffix.Text = ' - $(6*7)> '
$GitPromptSettings.DefaultPromptAbbreviateHomeDirectory = $true
$res = [string](&$prompt *>&1)
$res | Should BeExactly "[$(hostname)] ~ - 42> "
$res | Should BeExactly "[$(hostname)] $(GetHomePath) - 42> "
}
It 'Returns the expected prompt string with prompt timing enabled' {
Set-Location $Home -ErrorAction Stop
Expand Down Expand Up @@ -154,7 +154,7 @@ Describe 'Default Prompt Tests - ANSI' {
$GitPromptSettings.DefaultPromptPath.ForegroundColor = [ConsoleColor]::DarkCyan
$GitPromptSettings.DefaultPromptPath.BackgroundColor = [ConsoleColor]::DarkRed
$res = &$prompt
$res | Should BeExactly "${csi}36m${csi}41m~${csi}0m> "
$res | Should BeExactly "${csi}36m${csi}41m$(GetHomePath)${csi}0m> "
}
It 'Returns the expected prompt string with prefix, suffix and abbrev home set' {
Set-Location $Home -ErrorAction Stop
Expand All @@ -164,7 +164,7 @@ Describe 'Default Prompt Tests - ANSI' {
$GitPromptSettings.DefaultPromptSuffix.ForegroundColor = [ConsoleColor]::DarkBlue
$GitPromptSettings.DefaultPromptAbbreviateHomeDirectory = $true
$res = &$prompt
$res | Should BeExactly "${csi}38;2;245;245;245m[$(hostname)] ${csi}0m~${csi}34m - 42> ${csi}0m"
$res | Should BeExactly "${csi}38;2;245;245;245m[$(hostname)] ${csi}0m$(GetHomePath)${csi}34m - 42> ${csi}0m"
}
It 'Returns the expected prompt string with prompt timing enabled' {
Set-Location $Home -ErrorAction Stop
Expand Down Expand Up @@ -214,14 +214,16 @@ Describe 'Default Prompt WindowTitle Tests' {
$PSDefaultParameterValues["it:skip"] = $true
}

$homePath = [regex]::Escape((GetHomePath))

[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')]
$repoAdminRegex = '^Administrator: posh~git ~ posh-git \[master\] ~ PowerShell \d+\.\d+\.\d+(\.\d+|-\S+)? \d\d-bit \(\d+\)$'
$repoAdminRegex = '^Admin: posh-git \[master\] \~ PowerShell \d+\.\d+\.\d+(\.\d+|-\S+)? \d\d-bit \(\d+\)$'
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')]
$repoRegex = '^posh~git ~ posh-git \[master\] ~ PowerShell \d+\.\d+\.\d+(\.\d+|-\S+)? \d\d-bit \(\d+\)$'
$repoRegex = '^posh-git \[master\] \~ PowerShell \d+\.\d+\.\d+(\.\d+|-\S+)? \d\d-bit \(\d+\)$'
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')]
$nonRepoAdminRegex = '^Administrator: PowerShell \d+\.\d+\.\d+(\.\d+|-\S+)? \d\d-bit \(\d+\)$'
$nonRepoAdminRegex = '^Admin: ' + $homePath + ' \~ PowerShell \d+\.\d+\.\d+(\.\d+|-\S+)? \d\d-bit \(\d+\)$'
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')]
$nonRepoRegex = '^PowerShell \d+\.\d+\.\d+(\.\d+|-\S+)? \d\d-bit \(\d+\)$'
$nonRepoRegex = '^' + $homePath + ' \~ PowerShell \d+\.\d+\.\d+(\.\d+|-\S+)? \d\d-bit \(\d+\)$'
}
AfterAll {
$global:PSDefaultParameterValues = $originalDefaultParameterValues
Expand Down
2 changes: 1 addition & 1 deletion test/Shared.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function global:Convert-NativeLineEnding([string]$content, [switch]$SplitLines)
}

function GetHomePath() {
if ($GitPromptSettings.DefaultPromptAbbreviateHomeDirectory) {"~"} else {$HOME}
$Home
}

function GetHomeRelPath([string]$Path) {
Expand Down

0 comments on commit dcb96a4

Please sign in to comment.