Allow user-profile.ps1 and profile.d/*.ps1 files to override prompt

This defines the default prompt variables ($PrePrompt, $PostPrompt,
$CmderPrompt, and $Prompt) before running the custom scripts (user-profile.ps1
and profile.d/*.ps1), and then saves those prompt values after running the
custom scripts.  This gives the custom scripts and opportunity to override the
prompt code blocks.

This now opens the possibility for profile.d/*.ps1 scripts to affect the
prompt, and also allows both user-profile.ps1 and profile.d/*.ps1 scripts to
overwrite the $Prompt script block and overwrite the prompt behavior entirely.
This commit is contained in:
Brian Dukes 2017-02-28 16:25:11 -06:00
parent f04a4e4846
commit 7f4a4d34d6
No known key found for this signature in database
GPG Key ID: 3C1CA727E76C0AD1

58
vendor/profile.ps1 vendored
View File

@ -86,19 +86,6 @@ if (Get-Module PSReadline -ErrorAction "SilentlyContinue") {
# Enhance Path # Enhance Path
$env:Path = "$Env:CMDER_ROOT\bin;$env:Path;$Env:CMDER_ROOT" $env:Path = "$Env:CMDER_ROOT\bin;$env:Path;$Env:CMDER_ROOT"
# Drop *.ps1 files into "$ENV:CMDER_ROOT\config\profile.d"
# to source them at startup.
if (-not (test-path "$ENV:CMDER_ROOT\config\profile.d")) {
mkdir "$ENV:CMDER_ROOT\config\profile.d"
}
pushd $ENV:CMDER_ROOT\config\profile.d
foreach ($x in ls *.ps1) {
# write-host write-host Sourcing $x
. $x
}
popd
# #
# Prompt Section # Prompt Section
# Users should modify their user-profile.ps1 as it will be safe from updates. # Users should modify their user-profile.ps1 as it will be safe from updates.
@ -113,6 +100,35 @@ popd
checkGit($pwd.ProviderPath) checkGit($pwd.ProviderPath)
} }
<#
This scriptblock runs every time the prompt is returned.
Explicitly use functions from MS namespace to protect from being overridden in the user session.
Custom prompt functions are loaded in as constants to get the same behaviour
#>
[ScriptBlock]$Prompt = {
$realLASTEXITCODE = $LASTEXITCODE
$host.UI.RawUI.WindowTitle = Microsoft.PowerShell.Management\Split-Path $pwd.ProviderPath -Leaf
PrePrompt | Microsoft.PowerShell.Utility\Write-Host -NoNewline
CmderPrompt
Microsoft.PowerShell.Utility\Write-Host "`nλ " -NoNewLine -ForegroundColor "DarkGray"
PostPrompt | Microsoft.PowerShell.Utility\Write-Host -NoNewline
$global:LASTEXITCODE = $realLASTEXITCODE
return " "
}
# Drop *.ps1 files into "$ENV:CMDER_ROOT\config\profile.d"
# to source them at startup.
if (-not (test-path "$ENV:CMDER_ROOT\config\profile.d")) {
mkdir "$ENV:CMDER_ROOT\config\profile.d"
}
pushd $ENV:CMDER_ROOT\config\profile.d
foreach ($x in ls *.ps1) {
# write-host write-host Sourcing $x
. $x
}
popd
$CmderUserProfilePath = Join-Path $env:CMDER_ROOT "config\user-profile.ps1" $CmderUserProfilePath = Join-Path $env:CMDER_ROOT "config\user-profile.ps1"
if(Test-Path $CmderUserProfilePath) { if(Test-Path $CmderUserProfilePath) {
# Create this file and place your own command in there. # Create this file and place your own command in there.
@ -160,22 +176,6 @@ Set-Item -Path function:\PrePrompt -Value $PrePrompt -Options Constant
Set-Item -Path function:\CmderPrompt -Value $CmderPrompt -Options Constant Set-Item -Path function:\CmderPrompt -Value $CmderPrompt -Options Constant
Set-Item -Path function:\PostPrompt -Value $PostPrompt -Options Constant Set-Item -Path function:\PostPrompt -Value $PostPrompt -Options Constant
<#
This scriptblock runs every time the prompt is returned.
Explicitly use functions from MS namespace to protect from being overridden in the user session.
Custom prompt functions are loaded in as constants to get the same behaviour
#>
[ScriptBlock]$Prompt = {
$realLASTEXITCODE = $LASTEXITCODE
$host.UI.RawUI.WindowTitle = Microsoft.PowerShell.Management\Split-Path $pwd.ProviderPath -Leaf
PrePrompt | Microsoft.PowerShell.Utility\Write-Host -NoNewline
CmderPrompt
Microsoft.PowerShell.Utility\Write-Host "`nλ " -NoNewLine -ForegroundColor "DarkGray"
PostPrompt | Microsoft.PowerShell.Utility\Write-Host -NoNewline
$global:LASTEXITCODE = $realLASTEXITCODE
return " "
}
# Functions can be made constant only at creation time # Functions can be made constant only at creation time
# ReadOnly at least requires `-force` to be overwritten # ReadOnly at least requires `-force` to be overwritten
Set-Item -Path function:\prompt -Value $Prompt -Options ReadOnly Set-Item -Path function:\prompt -Value $Prompt -Options ReadOnly