Skip to content

Instantly share code, notes, and snippets.

@vexx32
Last active October 6, 2019 21:39
Show Gist options
  • Save vexx32/94ac39486e84a3ea28ef0a9dda6c5a3c to your computer and use it in GitHub Desktop.
Save vexx32/94ac39486e84a3ea28ef0a9dda6c5a3c to your computer and use it in GitHub Desktop.
function prompt {
$Success = $?
$ConsoleWidth = ($host.UI.RawUI.WindowSize.Width, $Host.UI.RawUI.BufferSize.Width -gt 0)[0]
$Escape = "`e["
$EndEscape = "m"
$Slash = [IO.Path]::DirectorySeparatorChar
$Path = $ExecutionContext.SessionState.Path.CurrentLocation
$CurrentFolder = Split-Path -Path $Path -Leaf
$Host.UI.RawUI.WindowTitle = "PS $Path"
$LastCommand = Get-History -Count 1
$ChangeFg = "${Escape}38;2;"
$ChangeBg = "${Escape}48;2;"
$SwapColors = "${Escape}7${EndEscape}"
$TotalReset = "${Escape}0${EndEscape}"
$PathColor = "150;150;255"
$DateBgColor = "180;200;232"
$PromptColor = "55;120;200"
$PromptStrings = @(
#region: DateTime & Path
{
$Now = (Get-Date).ToString() -replace '^|$', ' '
$ExecTime = if ($LastCommand) { " $($LastCommand.Duration.TotalMilliseconds.ToString('G15')) ms " }
$TimeColor = if ($Success) { "200;180;60" } else { "235;20;20" }
$ParentLocation = (Split-Path $Path -Parent) -replace "^$([regex]::Escape($HOME))", '~' -replace '^|$', ' '
$Space = " " * ($ConsoleWidth - ($Now.Length + $ExecTime.Length + $ParentLocation.Length))
"{0}{1}{2}{3}" -f @(
"${ChangeFg}${PathColor}${EndEscape}${ParentLocation}${TotalReset}"
$Space
"${ChangeFg}${TimeColor}${EndEscape}${ExecTime}${TotalReset}"
"${ChangeFg}${DateBgColor}${EndEscape}${SwapColors}${Now}${TotalReset}"
) |
Write-Host
}
#endregion: DateTime
{
$Prompt = @{
LineNumber = @{
Content = $LastCommand.Id + 1
Color = $DateBgColor
}
Prefix = @{
Content = 'PS'
Color = $PromptColor
}
Drive = @{
Color = $PathColor
Content = "$($Path.Drive):"
}
Path = @{
Content = if ($CurrentFolder -notmatch "^$($Path.Drive):?\\?$") { $CurrentFolder -replace "^|$", $Slash } else { "Root" }
Color = $PromptColor
}
PostFix = @{
Content = "PS$(">" * ($NestedPromptLevel + 1))"
Color = switch ($true) {
$IsWindows { '200;168;80' }
$IsMacOS { '220;220;220' }
$IsLinux { '200;80;80' }
default { '40;80;180' }
}
}
}
"{1}{0}{2}{0}{3}{0}{4}{0} " -f @(
$TotalReset
"{0} {1}{2} " -f "${ChangeFg}$($Prompt.LineNumber.Color)${EndEscape}${SwapColors}", $Prompt.LineNumber.Content, "${ChangeBg}$($Prompt.Prefix.Color)${EndEscape}"
"{0} {1}{2} " -f "${ChangeFg}$($Prompt.Drive.Color)${EndEscape}${SwapColors}", $Prompt.Drive.Content, "${ChangeBg}$($Prompt.Path.Color)${EndEscape}"
"{0} {1}{2} " -f "${ChangeFg}$($Prompt.Path.Color)${EndEscape}${SwapColors}", $Prompt.Path.Content, "${ChangeBg}$($Prompt.PostFix.Color)${EndEscape}"
"{0} {1} " -f "${ChangeFg}$($Prompt.PostFix.Color)${EndEscape}${SwapColors}", $Prompt.PostFix.Content
)
}
)
$PromptStrings.ForEach{ $_.Invoke() }
}
$script:OldPromptFunction = $function:Prompt
function Set-PresenterPrompt {
$script:OldPromptFunction = $function:prompt
$function:prompt = {
Write-Host
$Location = $ExecutionContext.SessionState.Path
Write-Host -NoNewLine -ForegroundColor Green " PSPowerHour"
Write-Host -NoNewline " :: "
Write-Host "Presenter - Joel Sallow " -ForegroundColor Cyan
$Location = $ExecutionContext.SessionState.Path.CurrentLocation
$LeafLocation = $Location | Split-Path -Leaf
$ParentLocation = ($Location | Split-Path -Parent) -replace "^$([regex]::Escape($HOME))", '~'
Write-Host -BackgroundColor Blue -ForegroundColor Cyan " [$ParentLocation] "
Write-Host -NoNewline -BackgroundColor Blue -ForegroundColor Yellow " PS "
Write-Host -NoNewline -BackgroundColor Cyan " .\$LeafLocation> " -ForegroundColor Black
" "
}
}
function Restore-Prompt {
$function:prompt = $script:OldPromptFunction
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment