Commit 29cd10a5 authored by Dustin L. Howett's avatar Dustin L. Howett

Migrate a bunch of profile.ps1 to profile.d/

parent 230a12f7
$Env:PATH += [System.IO.Path]::PathSeparator + "$(Join-Path (Split-Path $Profile) Scripts)"
Function _dh_ShortHomePath {
$p = $PWD.ProviderPath
If($p.StartsWith($HOME)) {
$p = "~" + $p.Substring($HOME.Length)
}
$p
}
Function _dh_SetWindowTitle {
$title = ""
If($null -ne $Global:DevEnv -and $Global:DevEnv.Name) {
$title += "(" + $Global:DevEnv.Name + ") ";
}
if ($PWD.Provider.Name -ne "FileSystem") {
$title += $PWD.Provider.Name.ToLower()
if ($PWD.ProviderPath[0] -ne "\") {
$title += ":\"
} else {
$title += ":"
}
}
$title += _dh_ShortHomePath
$Host.UI.RawUI.WindowTitle = $title
}
Import-Module PromptEd -EA Ignore
If ($null -eq (Get-Module PromptEd)) {
Return
}
$script:Hostname = [System.Environment]::MachineName
Function pe_Hostname {
Write-Host "(" -NoNewLine -ForegroundColor (Get-PromptColor Brackets)
Write-Host $Script:HOSTNAME.ToLower() -NoNewLine -ForegroundColor (Get-PromptColor Hostname)
Write-Host ")" -NoNewLine -ForegroundColor (Get-PromptColor Brackets)
}
Function pe_DevEnvHostname {
Write-Host "(" -NoNewLine -ForegroundColor (Get-PromptColor Brackets)
If($null -ne $Global:DevEnv -and $Global:DevEnv.Name) {
Write-Host $Global:DevEnv.Name -NoNewLine -ForegroundColor (Get-PromptColor DevEnv)
Write-Host "\" -NoNewLine -ForegroundColor (Get-PromptColor Brackets)
}
Write-Host $Script:HOSTNAME.ToLower() -NoNewLine -ForegroundColor (Get-PromptColor Hostname)
Write-Host ")" -NoNewLine -ForegroundColor (Get-PromptColor Brackets)
}
Function pe_Provider {
if ($PWD.Provider.Name -ne "FileSystem") {
Write-Host $PWD.Provider.Name.ToLower() -NoNewLine -ForegroundColor (Get-PromptColor Provider)
if ($PWD.ProviderPath[0] -ne "\") {
Write-Host ":\" -NoNewLine -ForegroundColor (Get-PromptColor Provider)
} else {
Write-Host ":" -NoNewLine -ForegroundColor (Get-PromptColor Provider)
}
}
}
Function pe_ShortHomePath {
Write-Host (_dh_ShortHomePath) -NoNewLine -ForegroundColor (Get-PromptColor Path)
}
Function pe_NestedPromptLevel {
If ($NestedPromptLevel -Gt 0) {
Write-Host "<" -NoNewLine -ForegroundColor DarkYellow
Write-Host "$NestedPromptLevel" -NoNewLine -ForegroundColor Yellow
Write-Host "> " -NoNewLine -ForegroundColor DarkYellow
}
}
Function pe_Sigil {
If($IsWindows) {
if((
New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Host "#" -NoNewLine -ForegroundColor (Get-PromptColor Sigil)
} else {
Write-Host "%" -NoNewLine -ForegroundColor (Get-PromptColor Sigil)
}
} Else {
Write-Host $(If($Env:UID -Eq 0) { "#" } Else { "%" }) -NoNewLine -ForegroundColor (Get-PromptColor Sigil)
}
}
Function pe_LastCommandRunTime {
If ($global:_LastCommandTimeSpan -And ($global:_LastCommandTimeSpan.TotalSeconds) -Ge 10) {
# Format buffer: Total Minutes, Seconds, Milliseconds
# We could use total seconds in the second position, but it's only ever used as a primary
# unit if it numbers less than 60.
$fmtBuff = ([int]$global:_LastCommandTimeSpan.TotalMinutes, [int]$global:_LastCommandTimeSpan.Seconds, [int]$global:_LastCommandTimeSpan.Milliseconds)
$fmt = "{1}.{2:D3}s"
If (($global:_LastCommandTimeSpan.TotalSeconds) -Ge 60) {
$fmt = "{0}m{1}s"
}
Write-Host "[" -NoNewLine -ForegroundColor (Get-PromptColor CommandTimeBrackets)
Write-Host ($fmt -f $fmtBuff) -NoNewLine -ForegroundColor (Get-PromptColor CommandTime)
Write-Host "] " -NoNewLine -ForegroundColor (Get-PromptColor CommandTimeBrackets)
}
}
Add-PromptTask CalculateCommandLength {
If ($global:_LastCommandLaunchDate) {
$global:_LastCommandTimeSpan = (Get-Date).Subtract($global:_LastCommandLaunchDate)
$global:_LastCommandLaunchDate = $null
Set-PromptColor CommandTimeBrackets DarkYellow
Set-PromptColor CommandTime Yellow
If (($global:_LastCommandTimeSpan.TotalSeconds) -Ge 30) {
Set-PromptColor CommandTimeBrackets DarkRed
Set-PromptColor CommandTime Red
}
}
}
Add-BuiltinPrompt DHowettPrompt @(
$function:pe_LastCommandRunTime,
$function:pe_NoSeparator,
$function:pe_DevEnvHostname,
$function:pe_Provider,
$function:pe_NoSeparator,
$function:pe_ShortHomePath,
$function:pe_NestedPromptLevel,
$function:pe_NoSeparator,
$function:pe_Sigil
)
Add-PromptTask WindowTitlePath { _dh_SetWindowTitle }
Set-PromptColor Provider DarkMagenta
Set-PromptColor Sigil Green
Set-PromptColor Hostname White
Set-PromptColor DevEnv Green
Set-PromptColor CommandTime Black
Set-PromptColor CommandTimeBrackets Black
# These are already defined by PromptEd
Set-PromptColor Path Magenta
Set-PromptColor Brackets Blue
Set-Prompt DHowettPrompt
If ($null -ne (Get-Module PSReadline)) {
Set-PSReadlineKeyHandler "Enter" -ScriptBlock {
$global:_LastCommandLaunchDate = Get-Date
[Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
}
}
If($null -ne (Get-Module PromptEd)) {
Return
}
Write-Warning "Module PromptEd not found; falling back to legacy prompt."
Function Prompt {
_dh_SetWindowTitle
Write-Host "(" -nonewline -foregroundcolor Blue
If($null -ne $Global:DevEnv -and $Global:DevEnv.Name) {
Write-Host $Global:DevEnv.Name -nonewline -foregroundcolor Green
Write-Host "\" -nonewline -foregroundcolor Blue
}
Write-Host $env:COMPUTERNAME.ToLower() -nonewline -foregroundcolor White
Write-Host ") " -nonewline -foregroundcolor Blue
# Show providername if you are outside FileSystem
if ($pwd.Provider.Name -ne "FileSystem") {
Write-Host $pwd.Provider.Name.ToLower() -nonewline -foregroundcolor DarkMagenta
if ($pwd.ProviderPath[0] -ne "\") {
Write-Host ":\" -nonewline -foregroundcolor DarkMagenta
} else {
Write-Host ":" -nonewline -foregroundcolor DarkMagenta
}
}
Write-Host (_dh_ShortHomePath) -nonewline -foregroundcolor Magenta
if((
New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Host " #" -nonewline -foregroundcolor Green
} else {
Write-Host " %" -nonewline -foregroundcolor Green
}
return " "
}
Function Set-Prompt([string]$prompt) {
}
Import-Module DirColors -EA Ignore
If ($null -eq (Get-Module DirColors)) {
Return
}
Update-DirColors "$(Split-Path $PROFILE)\dir_colors"
If ($null -eq (Get-Module PSReadline)) {
Write-Warning "PSReadline wasn't loaded; line editing may be impacted"
Return
}
Set-PSReadlineOption -EditMode Emacs
Set-PSReadlineKeyHandler "Ctrl+Delete" KillWord
Set-PSReadlineKeyHandler "Ctrl+Backspace" BackwardKillWord
Set-PSReadlineKeyHandler "Ctrl+LeftArrow" BackwardWord
Set-PSReadlineKeyHandler "Ctrl+RightArrow" NextWord
Set-PSReadlineKeyHandler "Tab" MenuComplete
$Env:PATH += [System.IO.Path]::PathSeparator + "$(Join-Path (Split-Path $Profile) Scripts)"
# PSReadline profile
Set-PSReadlineOption -EditMode Emacs
Set-PSReadlineKeyHandler "Ctrl+Delete" KillWord
Set-PSReadlineKeyHandler "Ctrl+Backspace" BackwardKillWord
Set-PSReadlineKeyHandler "Ctrl+LeftArrow" BackwardWord
Set-PSReadlineKeyHandler "Ctrl+RightArrow" NextWord
Set-PSReadlineKeyHandler "Tab" MenuComplete
Import-Module PromptEd -EA Ignore
# Fallback: If PromptEd is not installed, render a legacy prompt.
If($null -eq (Get-Module PromptEd)) {
Write-Warning "Module PromptEd not found; falling back to legacy prompt."
Function Prompt {
$Host.UI.RawUI.WindowTitle = "PowerShell v" + (get-host).Version.Major + "." + (get-host).Version.Minor + " (" + $pwd.Provider.Name + ") " + $pwd.Path
Write-Host "(" -nonewline -foregroundcolor Blue
If($null -ne $Global:DevEnv -and $Global:DevEnv.Name) {
Write-Host $Global:DevEnv.Name -nonewline -foregroundcolor Green
Write-Host "\" -nonewline -foregroundcolor Blue
}
Write-Host $env:COMPUTERNAME.ToLower() -nonewline -foregroundcolor White
Write-Host ") " -nonewline -foregroundcolor Blue
# Show providername if you are outside FileSystem
if ($pwd.Provider.Name -ne "FileSystem") {
Write-Host $pwd.Provider.Name.ToLower() -nonewline -foregroundcolor DarkMagenta
if ($pwd.ProviderPath[0] -ne "\") {
Write-Host ":\" -nonewline -foregroundcolor DarkMagenta
} else {
Write-Host ":" -nonewline -foregroundcolor DarkMagenta
}
}
Write-Host $pwd.ProviderPath.Replace($env:USERPROFILE, "~") -nonewline -foregroundcolor Magenta
if((
New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Host " #" -nonewline -foregroundcolor Green
} else {
Write-Host " %" -nonewline -foregroundcolor Green
}
return " "
}
Function Set-Prompt([string]$prompt) {
}
} Else {
$script:Hostname = [System.Environment]::MachineName
Function pe_Hostname {
Write-Host "(" -NoNewLine -ForegroundColor (Get-PromptColor Brackets)
Write-Host $Script:HOSTNAME.ToLower() -NoNewLine -ForegroundColor (Get-PromptColor Hostname)
Write-Host ")" -NoNewLine -ForegroundColor (Get-PromptColor Brackets)
}
Function pe_DevEnvHostname {
Write-Host "(" -NoNewLine -ForegroundColor (Get-PromptColor Brackets)
If($null -ne $Global:DevEnv -and $Global:DevEnv.Name) {
Write-Host $Global:DevEnv.Name -NoNewLine -ForegroundColor (Get-PromptColor DevEnv)
Write-Host "\" -NoNewLine -ForegroundColor (Get-PromptColor Brackets)
}
Write-Host $Script:HOSTNAME.ToLower() -NoNewLine -ForegroundColor (Get-PromptColor Hostname)
Write-Host ")" -NoNewLine -ForegroundColor (Get-PromptColor Brackets)
}
Function pe_Provider {
if ($PWD.Provider.Name -ne "FileSystem") {
Write-Host $PWD.Provider.Name.ToLower() -NoNewLine -ForegroundColor (Get-PromptColor Provider)
if ($PWD.ProviderPath[0] -ne "\") {
Write-Host ":\" -NoNewLine -ForegroundColor (Get-PromptColor Provider)
} else {
Write-Host ":" -NoNewLine -ForegroundColor (Get-PromptColor Provider)
}
}
}
Function pe_ShortHomePath {
$p = $PWD.ProviderPath
If($p.StartsWith($HOME)) {
$p = "~" + $p.Substring($HOME.Length)
}
Write-Host $p -NoNewLine -ForegroundColor (Get-PromptColor Path)
}
Function pe_NestedPromptLevel {
If ($NestedPromptLevel -Gt 0) {
Write-Host "<" -NoNewLine -ForegroundColor DarkYellow
Write-Host "$NestedPromptLevel" -NoNewLine -ForegroundColor Yellow
Write-Host "> " -NoNewLine -ForegroundColor DarkYellow
}
}
Function pe_Sigil {
If($IsWindows) {
if((
New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Host "#" -NoNewLine -ForegroundColor (Get-PromptColor Sigil)
} else {
Write-Host "%" -NoNewLine -ForegroundColor (Get-PromptColor Sigil)
}
} Else {
Write-Host $(If($Env:UID -Eq 0) { "#" } Else { "%" }) -NoNewLine -ForegroundColor (Get-PromptColor Sigil)
}
}
Function pe_LastCommandRunTime {
If ($global:_LastCommandTimeSpan -And ($global:_LastCommandTimeSpan.TotalSeconds) -Ge 10) {
# Format buffer: Total Minutes, Seconds, Milliseconds
# We could use total seconds in the second position, but it's only ever used as a primary
# unit if it numbers less than 60.
$fmtBuff = ([int]$global:_LastCommandTimeSpan.TotalMinutes, [int]$global:_LastCommandTimeSpan.Seconds, [int]$global:_LastCommandTimeSpan.Milliseconds)
$fmt = "{1}.{2:D3}s"
If (($global:_LastCommandTimeSpan.TotalSeconds) -Ge 60) {
$fmt = "{0}m{1}s"
}
Write-Host "[" -NoNewLine -ForegroundColor (Get-PromptColor CommandTimeBrackets)
Write-Host ($fmt -f $fmtBuff) -NoNewLine -ForegroundColor (Get-PromptColor CommandTime)
Write-Host "] " -NoNewLine -ForegroundColor (Get-PromptColor CommandTimeBrackets)
}
}
Add-PromptTask CalculateCommandLength {
If ($global:_LastCommandLaunchDate) {
$global:_LastCommandTimeSpan = (Get-Date).Subtract($global:_LastCommandLaunchDate)
$global:_LastCommandLaunchDate = $null
Set-PromptColor CommandTimeBrackets DarkYellow
Set-PromptColor CommandTime Yellow
If (($global:_LastCommandTimeSpan.TotalSeconds) -Ge 30) {
Set-PromptColor CommandTimeBrackets DarkRed
Set-PromptColor CommandTime Red
}
}
}
Add-BuiltinPrompt DHowettPrompt @(
$function:pe_LastCommandRunTime,
$function:pe_NoSeparator,
$function:pe_DevEnvHostname,
$function:pe_Provider,
$function:pe_NoSeparator,
$function:pe_ShortHomePath,
$function:pe_NestedPromptLevel,
$function:pe_NoSeparator,
$function:pe_Sigil
)
Add-PromptTask WindowTitlePath {
$title = ""
If($null -ne $Global:DevEnv -and $Global:DevEnv.Name) {
$title += "(" + $Global:DevEnv.Name + ") ";
}
if ($PWD.Provider.Name -ne "FileSystem") {
$title += $PWD.Provider.Name.ToLower()
if ($PWD.ProviderPath[0] -ne "\") {
$title += ":\"
} else {
$title += ":"
}
}
$title += $PWD.ProviderPath.Replace($HOME, "~")
$Host.UI.RawUI.WindowTitle = $title
}
Set-PromptColor Provider DarkMagenta
Set-PromptColor Sigil Green
Set-PromptColor Hostname White
Set-PromptColor DevEnv Green
Set-PromptColor CommandTime Black
Set-PromptColor CommandTimeBrackets Black
# These are already defined by PromptEd
Set-PromptColor Path Magenta
Set-PromptColor Brackets Blue
Set-Prompt DHowettPrompt
Set-PSReadlineKeyHandler "Enter" -ScriptBlock {
$global:_LastCommandLaunchDate = Get-Date
[Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
}
ForEach($i In (Get-ChildItem (Join-Path (Split-Path $Profile.CurrentUserAllHosts) profile.d) -Filter:*.ps1 -EA:Ignore)) {
. ($i.FullName)
}
New-Alias -Name Eject -Value Eject-Disk
New-Alias -Name make -Value mingw32-make
......@@ -339,9 +150,6 @@ Function Add-MSHeader {
New-Alias -Name ms -Value Add-MSHeader
Import-Module DirColors
Update-DirColors "$(Split-Path $PROFILE)\dir_colors"
New-Alias -name vs -Value Import-VisualStudioVars
Function Take-TemporaryDirectory {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment