From 7f4a4d34d6960996ccc45056e2c9f6ad4447b5ae Mon Sep 17 00:00:00 2001 From: Brian Dukes Date: Tue, 28 Feb 2017 16:25:11 -0600 Subject: [PATCH] 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. --- vendor/profile.ps1 | 58 +++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/vendor/profile.ps1 b/vendor/profile.ps1 index 7878ff2..20dc528 100644 --- a/vendor/profile.ps1 +++ b/vendor/profile.ps1 @@ -86,19 +86,6 @@ if (Get-Module PSReadline -ErrorAction "SilentlyContinue") { # Enhance Path $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 # Users should modify their user-profile.ps1 as it will be safe from updates. @@ -113,6 +100,35 @@ popd 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" if(Test-Path $CmderUserProfilePath) { # 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:\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 # ReadOnly at least requires `-force` to be overwritten Set-Item -Path function:\prompt -Value $Prompt -Options ReadOnly