Skip to content

Instantly share code, notes, and snippets.

@dustindortch
Forked from corbob/prompt.ps1
Created May 16, 2019 01:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dustindortch/e112098a3c295270f344fd6de8e47177 to your computer and use it in GitHub Desktop.
Save dustindortch/e112098a3c295270f344fd6de8e47177 to your computer and use it in GitHub Desktop.
prompt...
$forePromptColor = 0
[System.Collections.Generic.List[ScriptBlock]]$global: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]]$global:PromptLeft = @(
# left aligned
{ "$FG;${forePromptColor}m$BG;${global:plat}m{0}" -f $('{0:d4}' -f $MyInvocation.HistoryId) }
{ "$BG;22m$FG;${global: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]]$global: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" }
)
function global:prompt {
$global:SDEStats = Get-PTSdeStats
$global:LeftArrow = [char]0xe0b2
$global:RightArrow = [char]0xe0b0
$global:er = if ($?) {22}else {1}
$global:plat = if ($isWindows) {11}else {117}
$ESC = "$([char]27)"
$FG = "$ESC[38;5"
$BG = "$ESC[48;5"
$global: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 @($global:PromptLeft + $gitPrompt + {" "}).Invoke()
}
else {
$left = -join @($global:PromptLeft + { "$FG;14m{0}$ESC[0m" -f $($RightArrow) } + {" "}).Invoke()
}
$global:middle = -join ($global:PromptMiddle).invoke()
$global:right = -join ($global:promptRight).Invoke()
[int]$global:rightStartPoint = $global:host.UI.RawUI.BufferSize.Width - (stripControlChars $right).length
[int]$global:middleStartPoint = (($global: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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment