在配置文件提示中使用条件拆分路径
Using a conditional Split-Path in a profile prompt
我正在使用提示功能来使用自定义提示。我知道了,所以我得到了日期、当前工作目录和对象数量。我在 $scripts
或 $modules
位置,我希望截断当前工作目录。
$scripts = "$(Split-Path $profile)\Scripts"
$modules = "$(Split-Path $profile)\Modules"
负责提示功能的部分是这样的:
Write-Host ($PWD) -NoNewline -ForegroundColor Gray
也许您正在寻找这样的东西:
$basedir = Split-Path $profile
$pattern = [regex]::Escape($basedir) + '\(Scripts|Modules)(\.*|$)'
$path = if ($PWD.Path -match $pattern) {
$PWD.Path.Replace($basedir, '~')
} else {
$PWD.Path
}
Write-Host $path -NoNewline -ForegroundColor Gray
或者像这样:
$pattern = [regex]::Escape((Split-Path $profile)) + '\((Scripts|Modules)(\.*|$))'
Write-Host ($PWD.Path -replace $pattern, '~$1') -NoNewline -ForegroundColor Gray
我正在使用提示功能来使用自定义提示。我知道了,所以我得到了日期、当前工作目录和对象数量。我在 $scripts
或 $modules
位置,我希望截断当前工作目录。
$scripts = "$(Split-Path $profile)\Scripts"
$modules = "$(Split-Path $profile)\Modules"
负责提示功能的部分是这样的:
Write-Host ($PWD) -NoNewline -ForegroundColor Gray
也许您正在寻找这样的东西:
$basedir = Split-Path $profile
$pattern = [regex]::Escape($basedir) + '\(Scripts|Modules)(\.*|$)'
$path = if ($PWD.Path -match $pattern) {
$PWD.Path.Replace($basedir, '~')
} else {
$PWD.Path
}
Write-Host $path -NoNewline -ForegroundColor Gray
或者像这样:
$pattern = [regex]::Escape((Split-Path $profile)) + '\((Scripts|Modules)(\.*|$))'
Write-Host ($PWD.Path -replace $pattern, '~$1') -NoNewline -ForegroundColor Gray