consistency with powershell conventions;

* use "PS" instead of no-width "$([char]0x200B)" character in prompt (credit @skycommand)

* fixes additional space after the lambada and before the path

* display "PS" before path to distinguish between PowerShell and CMD
This commit is contained in:
David Refoua 2022-10-15 12:32:52 +03:30 committed by GitHub
parent 23edc8036f
commit 7f682653e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

169
vendor/profile.ps1 vendored
View File

@ -1,70 +1,71 @@
# Init Script for PowerShell # Init Script for PowerShell
# Created as part of cmder project # Created as part of cmder project
# !!! THIS FILE IS OVERWRITTEN WHEN CMDER IS UPDATED # !!! THIS FILE IS OVERWRITTEN WHEN CMDER IS UPDATED
# !!! Use "%CMDER_ROOT%\config\user_profile.ps1" to add your own startup commands # !!! Use "%CMDER_ROOT%\config\user_profile.ps1" to add your own startup commands
$CMDER_INIT_START=$(Get-Date -UFormat %s)
$CMDER_INIT_START = $(Get-Date -UFormat %s)
# Compatibility with PS major versions <= 2 # Compatibility with PS major versions <= 2
if(!$PSScriptRoot) { if (!$PSScriptRoot) {
$PSScriptRoot = Split-Path $Script:MyInvocation.MyCommand.Path $PSScriptRoot = Split-Path $Script:MyInvocation.MyCommand.Path
} }
if ($ENV:CMDER_USER_CONFIG) { if ($ENV:CMDER_USER_CONFIG) {
# write-host "CMDER IS ALSO USING INDIVIDUAL USER CONFIG FROM '$ENV:CMDER_USER_CONFIG'!" # Write-Host "CMDER IS ALSO USING INDIVIDUAL USER CONFIG FROM '$ENV:CMDER_USER_CONFIG'!"
} }
# We do this for Powershell as Admin Sessions because CMDER_ROOT is not being set. # We do this for Powershell as Admin Sessions because CMDER_ROOT is not being set.
if (! $ENV:CMDER_ROOT ) { if ($null -eq $ENV:CMDER_ROOT) {
if ( $ENV:ConEmuDir ) { if ($ENV:ConEmuDir) {
$ENV:CMDER_ROOT = resolve-path( $ENV:ConEmuDir + "\..\.." ) $ENV:CMDER_ROOT = Resolve-Path $ENV:ConEmuDir + "\..\.."
} else { } else {
$ENV:CMDER_ROOT = resolve-path( $PSScriptRoot + "\.." ) $ENV:CMDER_ROOT = Resolve-Path $PSScriptRoot + "\.."
} }
} }
# Remove trailing '\' # Remove trailing '\'
$ENV:CMDER_ROOT = (($ENV:CMDER_ROOT).trimend("\")) $ENV:CMDER_ROOT = ($ENV:CMDER_ROOT).trimend("\")
# Do not load bundled psget if a module installer is already available # Do not load bundled PsGet if a module installer is already available
# -> recent PowerShell versions include PowerShellGet out of the box # -> recent PowerShell versions include PowerShellGet out of the box
$moduleInstallerAvailable = [bool](Get-Command -Name 'Install-Module' -ErrorAction SilentlyContinue) $moduleInstallerAvailable = [bool](Get-Command -Name 'Install-Module' -ErrorAction SilentlyContinue)
# Add Cmder modules directory to the autoload path. # Add Cmder modules directory to the autoload path.
$CmderModulePath = Join-path $PSScriptRoot "psmodules/" $CmderModulePath = Join-path $PSScriptRoot "psmodules/"
$CmderFunctions = Join-Path $CmderModulePath "Cmder.ps1" $CmderFunctions = Join-Path $CmderModulePath "Cmder.ps1"
. $CmderFunctions . $CmderFunctions
if(-not $moduleInstallerAvailable -and -not $env:PSModulePath.Contains($CmderModulePath) ){ if(-not $moduleInstallerAvailable -and -not $env:PSModulePath.Contains($CmderModulePath) ) {
$env:PSModulePath = $env:PSModulePath.Insert(0, "$CmderModulePath;") $env:PSModulePath = $env:PSModulePath.Insert(0, "$CmderModulePath;")
} }
$gitVersionVendor = (readVersion -gitPath "$ENV:CMDER_ROOT\vendor\git-for-windows\cmd") $gitVersionVendor = (readVersion -gitPath "$ENV:CMDER_ROOT\vendor\git-for-windows\cmd")
# write-host "GIT VENDOR: ${gitVersionVendor}" # Write-Host "GIT VENDOR: ${gitVersionVendor}"
# Get user installed Git Version[s] and Compare with vendored if found. # Get user installed Git Version[s] and Compare with vendored if found.
foreach ($git in (get-command -ErrorAction SilentlyContinue 'git')) { foreach ($git in (Get-Command -ErrorAction SilentlyContinue 'git')) {
# write-host "GIT Path: " + $git.Path # Write-Host "GIT PATH: " + $git.Path
$gitDir = Split-Path -Path $git.Path $gitDir = Split-Path -Path $git.Path
$gitDir = isGitShim -gitPath $gitDir $gitDir = isGitShim -gitPath $gitDir
$gitVersionUser = (readVersion -gitPath $gitDir) $gitVersionUser = (readVersion -gitPath $gitDir)
# write-host "GIT USER: ${gitVersionUser}" # Write-Host "GIT USER: ${gitVersionUser}"
$useGitVersion = compare_git_versions -userVersion $gitVersionUser -vendorVersion $gitVersionVendor $useGitVersion = compare_git_versions -userVersion $gitVersionUser -vendorVersion $gitVersionVendor
# write-host "Using GIT Version: ${useGitVersion}" # Write-Host "Using GIT Version: ${useGitVersion}"
# Use user installed Git # Use user installed Git
if ($gitPathUser -eq $null) { if ($null -eq $gitPathUser) {
if ($gitDir -match '\\mingw32\\bin' -or $gitDir -match '\\mingw64\\bin') { if ($gitDir -match '\\mingw32\\bin' -or $gitDir -match '\\mingw64\\bin') {
$gitPathUser = ($gitDir.subString(0,$gitDir.Length - 12)) $gitPathUser = ($gitDir.subString(0,$gitDir.Length - 12))
} else { } else {
$gitPathUser = ($gitDir.subString(0,$gitDir.Length - 4)) $gitPathUser = ($gitDir.subString(0,$gitDir.Length - 4))
} }
} }
if ($useGitVersion -eq $gitVersionUser) { if ($useGitVersion -eq $gitVersionUser) {
# write-host "Using GIT Dir: ${gitDir}" # Write-Host "Using GIT Dir: ${gitDir}"
$ENV:GIT_INSTALL_ROOT = $gitPathUser $ENV:GIT_INSTALL_ROOT = $gitPathUser
$ENV:GIT_INSTALL_TYPE = 'USER' $ENV:GIT_INSTALL_TYPE = 'USER'
break break
@ -72,38 +73,38 @@ foreach ($git in (get-command -ErrorAction SilentlyContinue 'git')) {
} }
# User vendored Git. # User vendored Git.
if ($ENV:GIT_INSTALL_ROOT -eq $null -and $gitVersionVendor -ne $null) { if ($null -eq $ENV:GIT_INSTALL_ROOT -and $null -ne $gitVersionVendor) {
$ENV:GIT_INSTALL_ROOT = "$ENV:CMDER_ROOT\vendor\git-for-windows" $ENV:GIT_INSTALL_ROOT = "$ENV:CMDER_ROOT\vendor\git-for-windows"
$ENV:GIT_INSTALL_TYPE = 'VENDOR' $ENV:GIT_INSTALL_TYPE = 'VENDOR'
} }
# write-host "GIT_INSTALL_ROOT: ${ENV:GIT_INSTALL_ROOT}" # Write-Host "GIT_INSTALL_ROOT: ${ENV:GIT_INSTALL_ROOT}"
# write-host "GIT_INSTALL_TYPE: ${ENV:GIT_INSTALL_TYPE}" # Write-Host "GIT_INSTALL_TYPE: ${ENV:GIT_INSTALL_TYPE}"
if (-not($ENV:GIT_INSTALL_ROOT -eq $null)) { if (-Not ($null -eq $ENV:GIT_INSTALL_ROOT)) {
$env:Path = Configure-Git -gitRoot "$ENV:GIT_INSTALL_ROOT" -gitType $ENV:GIT_INSTALL_TYPE -gitPathUser $gitPathUser $env:Path = Configure-Git -gitRoot "$ENV:GIT_INSTALL_ROOT" -gitType $ENV:GIT_INSTALL_TYPE -gitPathUser $gitPathUser
} }
if ( Get-command -Name "vim" -ErrorAction silentlycontinue) { if (Get-Command -Name "vim" -ErrorAction silentlycontinue) {
new-alias -name "vi" -value vim New-Alias -name "vi" -value vim
} }
if (Get-Module PSReadline -ErrorAction "SilentlyContinue") { if (Get-Module PSReadline -ErrorAction "SilentlyContinue") {
Set-PSReadlineOption -ExtraPromptLineCount 1 Set-PSReadlineOption -ExtraPromptLineCount 1
} }
# Pre assign default prompt hooks so the first run of cmder gets a working prompt. # Pre-assign default prompt hooks so the first run of cmder gets a working prompt.
$env:gitLoaded = $false $env:gitLoaded = $false
[ScriptBlock]$PrePrompt = {} [ScriptBlock]$PrePrompt = {}
[ScriptBlock]$PostPrompt = {} [ScriptBlock]$PostPrompt = {}
[ScriptBlock]$CmderPrompt = { [ScriptBlock]$CmderPrompt = {
$Host.UI.RawUI.ForegroundColor = "White" $Host.UI.RawUI.ForegroundColor = "White"
Write-Host -NoNewline "$([char]0x200B)" Write-Host -NoNewline "PS "
Microsoft.PowerShell.Utility\Write-Host $pwd.ProviderPath -NoNewLine -ForegroundColor Green Microsoft.PowerShell.Utility\Write-Host $pwd.ProviderPath -NoNewLine -ForegroundColor Green
if (get-command git -erroraction silentlycontinue) { if (Get-Command git -erroraction silentlycontinue) {
checkGit($pwd.ProviderPath) checkGit($pwd.ProviderPath)
} }
Microsoft.PowerShell.Utility\Write-Host "`nλ " -NoNewLine -ForegroundColor "DarkGray" Microsoft.PowerShell.Utility\Write-Host "`nλ" -NoNewLine -ForegroundColor "DarkGray"
} }
# Enhance Path # Enhance Path
@ -111,41 +112,41 @@ $env:Path = "$Env:CMDER_ROOT\bin;$Env:CMDER_ROOT\vendor\bin;$env:Path;$Env:CMDER
# Drop *.ps1 files into "$ENV:CMDER_ROOT\config\profile.d" # Drop *.ps1 files into "$ENV:CMDER_ROOT\config\profile.d"
# to source them at startup. # to source them at startup.
if (-not (test-path "$ENV:CMDER_ROOT\config\profile.d")) { if (-Not (Test-Path -PathType container "$ENV:CMDER_ROOT\config\profile.d")) {
mkdir "$ENV:CMDER_ROOT\config\profile.d" New-Item -ItemType Directory -Path "$ENV:CMDER_ROOT\config\profile.d"
} }
pushd $ENV:CMDER_ROOT\config\profile.d Push-Location $ENV:CMDER_ROOT\config\profile.d
foreach ($x in Get-ChildItem *.psm1) { foreach ($x in Get-ChildItem *.psm1) {
# write-host write-host Sourcing $x # Write-Host Write-Host Sourcing $x
Import-Module $x Import-Module $x
} }
foreach ($x in Get-ChildItem *.ps1) { foreach ($x in Get-ChildItem *.ps1) {
# write-host write-host Sourcing $x # Write-Host Write-Host Sourcing $x
. $x . $x
} }
popd Pop-Location
# Drop *.ps1 files into "$ENV:CMDER_USER_CONFIG\config\profile.d" # Drop *.ps1 files into "$ENV:CMDER_USER_CONFIG\config\profile.d"
# to source them at startup. Requires using cmder.exe /C [cmder_user_root_path] argument # to source them at startup. Requires using cmder.exe /C [cmder_user_root_path] argument
if ($ENV:CMDER_USER_CONFIG -ne "" -and (test-path "$ENV:CMDER_USER_CONFIG\profile.d")) { if ($ENV:CMDER_USER_CONFIG -ne "" -And (Test-Path "$ENV:CMDER_USER_CONFIG\profile.d")) {
pushd $ENV:CMDER_USER_CONFIG\profile.d Push-Location $ENV:CMDER_USER_CONFIG\profile.d
foreach ($x in Get-ChildItem *.psm1) { foreach ($x in Get-ChildItem *.psm1) {
# write-host write-host Sourcing $x # Write-Host Write-Host Sourcing $x
Import-Module $x Import-Module $x
} }
foreach ($x in Get-ChildItem *.ps1) { foreach ($x in Get-ChildItem *.ps1) {
# write-host write-host Sourcing $x # Write-Host Write-Host Sourcing $x
. $x . $x
} }
popd Pop-Location
} }
# Renaming to "config\user_profile.ps1" to "user_profile.ps1" for consistency. # Renaming to "config\user_profile.ps1" to "user_profile.ps1" for consistency.
if (test-path "$env:CMDER_ROOT\config\user-profile.ps1") { if (Test-Path "$env:CMDER_ROOT\config\user-profile.ps1") {
rename-item "$env:CMDER_ROOT\config\user-profile.ps1" user_profile.ps1 Rename-Item "$env:CMDER_ROOT\config\user-profile.ps1" user_profile.ps1
} }
$CmderUserProfilePath = Join-Path $env:CMDER_ROOT "config\user_profile.ps1" $CmderUserProfilePath = Join-Path $env:CMDER_ROOT "config\user_profile.ps1"
@ -156,19 +157,19 @@ if (Test-Path $CmderUserProfilePath) {
if ($ENV:CMDER_USER_CONFIG) { if ($ENV:CMDER_USER_CONFIG) {
# Renaming to "$env:CMDER_USER_CONFIG\user-profile.ps1" to "user_profile.ps1" for consistency. # Renaming to "$env:CMDER_USER_CONFIG\user-profile.ps1" to "user_profile.ps1" for consistency.
if (test-path "$env:CMDER_USER_CONFIG\user-profile.ps1") { if (Test-Path "$env:CMDER_USER_CONFIG\user-profile.ps1") {
rename-item "$env:CMDER_USER_CONFIG\user-profile.ps1" user_profile.ps1 Rename-Item "$env:CMDER_USER_CONFIG\user-profile.ps1" user_profile.ps1
} }
$env:Path = "$Env:CMDER_USER_CONFIG\bin;$env:Path" $env:Path = "$Env:CMDER_USER_CONFIG\bin;$env:Path"
$CmderUserProfilePath = Join-Path $ENV:CMDER_USER_CONFIG "user_profile.ps1" $CmderUserProfilePath = Join-Path $ENV:CMDER_USER_CONFIG "user_profile.ps1"
if (Test-Path $CmderUserProfilePath) { if (Test-Path $CmderUserProfilePath) {
. "$CmderUserProfilePath" # user_profile.ps1 is not a module DO NOT USE import-module . "$CmderUserProfilePath" # user_profile.ps1 is not a module DO NOT USE import-module
} }
} }
if (! (Test-Path $CmderUserProfilePath) ) { if (-Not (Test-Path $CmderUserProfilePath)) {
Write-Host -BackgroundColor Darkgreen -ForegroundColor White "First Run: Creating user startup file: $CmderUserProfilePath" Write-Host -BackgroundColor Darkgreen -ForegroundColor White "First Run: Creating user startup file: $CmderUserProfilePath"
Copy-Item "$env:CMDER_ROOT\vendor\user_profile.ps1.default" -Destination $CmderUserProfilePath Copy-Item "$env:CMDER_ROOT\vendor\user_profile.ps1.default" -Destination $CmderUserProfilePath
} }
@ -180,39 +181,39 @@ if (! (Test-Path $CmderUserProfilePath) ) {
# Only set the prompt if it is currently set to the default # Only set the prompt if it is currently set to the default
# This allows users to configure the prompt in their user_profile.ps1 or config\profile.d\*.ps1 # This allows users to configure the prompt in their user_profile.ps1 or config\profile.d\*.ps1
if ( $(get-command prompt).Definition -match 'PS \$\(\$executionContext.SessionState.Path.CurrentLocation\)\$\(' -and ` if ( $(Get-Command prompt).Definition -match 'PS \$\(\$executionContext.SessionState.Path.CurrentLocation\)\$\(' -and `
$(get-command prompt).Definition -match '\(\$nestedPromptLevel \+ 1\)\) ";') { $(Get-Command prompt).Definition -match '\(\$nestedPromptLevel \+ 1\)\) ";') {
<# <#
This scriptblock runs every time the prompt is returned. This scriptblock runs every time the prompt is returned.
Explicitly use functions from MS namespace to protect from being overridden in the user session. 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 Custom prompt functions are loaded in as constants to get the same behaviour
#> #>
[ScriptBlock]$Prompt = { [ScriptBlock]$Prompt = {
$realLASTEXITCODE = $LASTEXITCODE $realLASTEXITCODE = $LASTEXITCODE
$host.UI.RawUI.WindowTitle = Microsoft.PowerShell.Management\Split-Path $pwd.ProviderPath -Leaf $host.UI.RawUI.WindowTitle = Microsoft.PowerShell.Management\Split-Path $pwd.ProviderPath -Leaf
PrePrompt | Microsoft.PowerShell.Utility\Write-Host -NoNewline PrePrompt | Microsoft.PowerShell.Utility\Write-Host -NoNewline
CmderPrompt CmderPrompt
PostPrompt | Microsoft.PowerShell.Utility\Write-Host -NoNewline PostPrompt | Microsoft.PowerShell.Utility\Write-Host -NoNewline
$global:LASTEXITCODE = $realLASTEXITCODE $global:LASTEXITCODE = $realLASTEXITCODE
return " " return " "
} }
# Once Created these code blocks cannot be overwritten # Once Created these code blocks cannot be overwritten
# if (-not $(get-command PrePrompt).Options -match 'Constant') {Set-Item -Path function:\PrePrompt -Value $PrePrompt -Options Constant} # if (-not $(Get-Command PrePrompt).Options -match 'Constant') {Set-Item -Path function:\PrePrompt -Value $PrePrompt -Options Constant}
# if (-not $(get-command CmderPrompt).Options -match 'Constant') {Set-Item -Path function:\CmderPrompt -Value $CmderPrompt -Options Constant} # if (-not $(Get-Command CmderPrompt).Options -match 'Constant') {Set-Item -Path function:\CmderPrompt -Value $CmderPrompt -Options Constant}
# if (-not $(get-command PostPrompt).Options -match 'Constant') {Set-Item -Path function:\PostPrompt -Value $PostPrompt -Options Constant} # if (-not $(Get-Command PostPrompt).Options -match 'Constant') {Set-Item -Path function:\PostPrompt -Value $PostPrompt -Options Constant}
Set-Item -Path function:\PrePrompt -Value $PrePrompt -Options Constant 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
# 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
# if (!$(get-command Prompt).Options -match 'ReadOnly') {Set-Item -Path function:\prompt -Value $Prompt -Options ReadOnly} # if (!$(Get-Command Prompt).Options -match 'ReadOnly') {Set-Item -Path function:\prompt -Value $Prompt -Options ReadOnly}
Set-Item -Path function:\prompt -Value $Prompt -Options ReadOnly Set-Item -Path function:\prompt -Value $Prompt -Options ReadOnly
} }
$CMDER_INIT_END=$(Get-Date -UFormat %s) $CMDER_INIT_END = $(Get-Date -UFormat %s)
# write-host "Elapsed Time: $(get-Date) `($($CMDER_INIT_END - $CMDER_INIT_START) total`)" # Write-Host "Elapsed Time: $(get-Date) `($($CMDER_INIT_END - $CMDER_INIT_START) total`)"