Last active
May 18, 2019 05:40
-
-
Save corbob/a390ccef3921c0caf64c830c194d72ff to your computer and use it in GitHub Desktop.
prompt...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#region prompt | |
$forePromptColor = 0 | |
[System.Collections.Generic.List[ScriptBlock]]$PromptRight = @( | |
# right aligned | |
{ "$FG;${er}m{0}" -f $LeftArrow } | |
{ "$FG;${forePromptColor}m$BG;${er}m{0}" -f $(if (@(get-history).Count -gt 0) {(get-history)[-1] | % { "{0:c}" -f (new-timespan $_.StartExecutionTime $_.EndExecutionTime)}}else {'00:00:00.0000000'}) } | |
{ "$FG;7m$BG;${er}m{0}" -f $LeftArrow } | |
{ "$FG;0m$BG;7m{0}" -f $(get-date -format "hh:mm:ss tt") } | |
) | |
[System.Collections.Generic.List[ScriptBlock]]$PromptLeft = @( | |
# left aligned | |
{ "$FG;${forePromptColor}m$BG;${plat}m{0}" -f $('{0:d4}' -f $MyInvocation.HistoryId) } | |
{ "$BG;22m$FG;${plat}m{0}" -f $($RightArrow) } | |
{ "$BG;22m$FG;${forePromptColor}m{0}" -f $(if ($pushd = (Get-Location -Stack).count) { "$([char]187)" + $pushd }) } | |
{ "$FG;22m$BG;5m{0}" -f $($RightArrow) } | |
{ "$BG;5m$FG;${forePromptColor}m{0}" -f $($pwd.Drive.Name) } | |
{ "$BG;14m$FG;5m{0}" -f $($RightArrow) } | |
{ "$BG;14m$FG;${forePromptColor}m{0}$ESC[0m" -f $(Split-Path $pwd -leaf) } | |
) | |
[System.Collections.Generic.List[ScriptBlock]]$PromptMiddle = @( | |
{ "$LeftArrow" } | |
{ "$BG;1m$FG;0m {0:d2} " -f $($SDEStats.pastDue) } | |
{ "$BG;11m {0:d2} " -f $($SDEStats.dueToday) } | |
{ "$BG;2m {0:d2} " -f $($SDEStats.dueFuture) } | |
{ "$BG;17m$FG;7m {0:d2} $ESC[0m" -f $($SDEStats.total) } | |
{ "$RightArrow " } | |
{ "$LeftArrow" } | |
{ "$BG;4m$FG;0m {0} " -f $($SDEClosed.Monday) } | |
{ "$BG;5m$FG;0m {0} " -f $($SDEClosed.Tuesday) } | |
{ "$BG;6m$FG;0m {0} " -f $($SDEClosed.Wednesday) } | |
{ "$BG;5m$FG;0m {0} " -f $($SDEClosed.Thursday) } | |
{ "$BG;4m$FG;0m {0} " -f $($SDEClosed.Friday) } | |
# { "$BG;4m$FG;0m {0} " -f $($SDEClosed.day) } | |
{ "$ESC[0m$RightArrow" } | |
) | |
function prompt { | |
# SDE is a ticketing system, you probably don't want this part, but left in because I'm too lazy to extract it... | |
$SDEStats = Get-PTSdeStats | |
$SDEClosed = Get-PTSdeStats -WeekToDate | |
$LeftArrow = [char]0xe0b2 | |
$RightArrow = [char]0xe0b0 | |
$er = if ($?) {22}else {1} | |
$plat = if ($isWindows) {11}else {117} | |
$ESC = "$([char]27)" | |
$FG = "$ESC[38;5" | |
$BG = "$ESC[48;5" | |
$left = '' | |
$gitTest = $(git config -l) -match 'branch\.' | |
if (-not [string]::IsNullOrEmpty($gitTest)) { | |
$branch = git symbolic-ref --short -q HEAD | |
$aheadbehind = git status -sb | |
$distance = '' | |
if (-not [string]::IsNullOrEmpty($(git diff --staged))) { $branchbg = 3 } | |
else { $branchbg = 5 } | |
if (-not [string]::IsNullOrEmpty($(git status -s))) { $arrowfg = 3 } | |
else { $arrowfg = 5 } | |
if ($aheadbehind -match '\[\w+.*\w+\]$') { | |
$ahead = [regex]::matches($aheadbehind, '(?<=ahead\s)\d+').value | |
$behind = [regex]::matches($aheadbehind, '(?<=behind\s)\d+').value | |
$distance = "$BG;15m$FG;${arrowfg}m{0}$ESC[0m" -f $($RightArrow) | |
if ($ahead) {$distance += "$BG;15m$FG;${forePromptColor}m{0}$ESC[0m" -f "a$ahead"} | |
if ($behind) {$distance += "$BG;15m$FG;${forePromptColor}m{0}$ESC[0m" -f "b$behind"} | |
$distance += "$FG;15m{0}$ESC[0m" -f $($RightArrow) | |
} | |
else { | |
$distance = "$FG;${arrowfg}m{0}$ESC[0m" -f $($RightArrow) | |
} | |
[System.Collections.Generic.List[ScriptBlock]]$gitPrompt = @( | |
{ "$BG;${branchbg}m$FG;14m{0}$ESC[0m" -f $($RightArrow) } | |
{ "$BG;${branchbg}m$FG;${forePromptColor}m{0}$ESC[0m" -f $branch } | |
{ "{0}$ESC[0m" -f $distance } | |
) | |
$left = -join @($PromptLeft + $gitPrompt + {" "}).Invoke() | |
} | |
else { | |
$left = -join @($PromptLeft + { "$FG;14m{0}$ESC[0m" -f $($RightArrow) } + {" "}).Invoke() | |
} | |
$middle = -join ($PromptMiddle).invoke() | |
$right = -join ($promptRight).Invoke() | |
[int]$rightStartPoint = $host.UI.RawUI.BufferSize.Width - ((stripControlChars $right).length - 1) | |
[int]$middleStartPoint = (($host.UI.RawUI.BufferSize.Width / 2) - ((stripControlChars $middle).length / 2)) | |
$r = -join @($left, "$ESC[${middleStartPoint}G", $middle, "$ESC[${rightStartPoint}G", $right, "$ESC[0m" + "`n`r`> ") | |
$r | |
$null = "$SDEStats $SDEClosed $LeftArrow $er $plat" | |
} | |
function stripControlChars { | |
param( | |
[string] | |
$String | |
) | |
$string -replace "$([char]27).*?m" | |
} | |
#endregion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment