Last active
August 7, 2020 04:13
-
-
Save Ovyerus/139e227ea4c7e52036e54fa49379d77d to your computer and use it in GitHub Desktop.
Hot powershell prompt
This file contains hidden or 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
$asciiSymbols = $false # If your terminal doesn't display unicode properly | |
$esc = [char]27 | |
$r = "$esc[0m" | |
$symbols = @{ | |
pathOpen = if (!$asciiSymbols) { "⌈" } else { "[" }; | |
pathClose = if (!$asciiSymbols) { "⌋" } else { "]" }; | |
timeOpen = if (!$asciiSymbols) { "⌊" } else { "[" }; | |
timeClose = if (!$asciiSymbols) { "⌉" } else { "]" }; | |
admin = if (!$asciiSymbols) { "⚡" } else { "#" }; | |
prompt = if (!$asciiSymbols) { "〉" } else { ">" }; | |
gitBranchPrefix = if (!$asciiSymbols) { "∗" } else { "*" }; | |
gitUp = if (!$asciiSymbols) { "↑" } else { "^" }; | |
gitDown = if (!$asciiSymbols) { "↓" } else { "v" }; | |
gitDirty = if (!$asciiSymbols) { "✘" } else { "x" }; | |
gitUntracked = "?"; | |
gitUnmerged = if (!$asciiSymbols) { "‼" } else { "!!" }; | |
} | |
$colors = @{ | |
separator = "$esc[34m"; | |
location = "$esc[92m"; | |
time = "$esc[93m"; | |
admin = "$esc[93m"; | |
name = "$esc[95m"; | |
error = "$esc[1;31m"; | |
gitBranch = "$esc[36m"; | |
gitBranchChanges = "$esc[33m"; | |
gitBad = "$esc[91m"; | |
} | |
class GitResult { | |
[string]$CommitId | |
[string]$Head | |
[string]$Upstream | |
[bool]$Detached | |
[bool]$Unmerged | |
[bool]$Untracked | |
[bool]$Dirty | |
[int]$Up | |
[int]$Down | |
} | |
function Get-Git-Info { | |
$data = git status --porcelain=v2 --branch | |
$branchInfo = $data -match "^#" | |
$unmerged = !!($data -match "^u") | |
$untracked = !!($data -match "^\?") | |
$dirty = !!($data -match "^(1|2)") | |
$detached = $false | |
$commitId = ([string]($branchInfo -match "branch.oid") -split " ")[-1] | |
$head = ([string]($branchInfo -match "branch.head") -split " ")[-1] | |
$upstream = ([string]($branchInfo -match "branch.upstream") -split " ")[-1] | |
$changes = [string]($branchInfo -match "branch.ab") | |
if (!$changes) { | |
$up = 0 | |
$down = 0 | |
} else { | |
$changes -match "\+([0-9]+) -([0-9]+)$" | |
$up = $Matches[1] | |
$down = $Matches[2] | |
} | |
if ($commitId -eq "(initial)") { $commitId = "initial" } | |
if ($head -eq "(detached)") { | |
$head = "detached" | |
$detached = $true; | |
} | |
$result = [GitResult]::new() | |
$result.CommitId = $commitId | |
$result.Head = $head | |
$result.Upstream = $upstream | |
$result.Detached = $detached | |
$result.Unmerged = $unmerged | |
$result.Untracked = $untracked | |
$result.Dirty = $dirty | |
$result.Up = $up | |
$result.Down = $down | |
$result | |
} | |
function Generate-Git-Path { | |
$git = Get-Git-Info | |
$commit = $git.CommitId.Substring(0, 7) | |
if ($git.Dirty -or $git.Untracked -or $git.Unmerged -or $git.Up) { | |
$branchColor = $colors.gitBranchChanges | |
} elseif ($git.Down) { | |
$branchColor = $colors.gitBad | |
} else { | |
$branchColor = $colors.gitBranch | |
} | |
$branchPrefix = if (!$git.Upstream) { | |
$symbols.gitBranchPrefix | |
} else { '' } | |
$res = " $($branchColor)$branchPrefix$($git.Head)" | |
# if branch not on remote, go red | |
if ($git.Up -or $git.Down) { $res += " ($commit)" } | |
$res += $r | |
if ($git.Up) { $res += " $($git.Up)↑" } | |
if ($git.Down) { $res += " ↓$($git.Down)" } | |
$res += " $($colors.gitBad)" | |
if ($git.Dirty) { $res += $symbols.gitDirty } | |
if ($git.Untracked) { $res += $symbols.gitUntracked } | |
if ($git.Unmerged) { $res += $symbols.gitUnmerged } | |
$res | |
} | |
function prompt { | |
$lastCmdFailed = !$?; | |
# Cause $IsWindows and shit doesn't exist on current Windows Powershell version | |
$windows = $IsWindows -or $env:OS -eq 'Windows_NT' | |
if ($windows) { | |
$admin = [bool](([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match 'S-1-5-32-544') | |
$name = $env:UserName | |
} else { | |
$admin = (id -u) -eq 0 | |
$name = id -un | |
} | |
$isHome = (Get-Location).Path.StartsWith($HOME) | |
$time = (Get-Date).ToString('HH:mm') | |
$path = (Get-Location).Path | |
if ($isHome) { $path = $path.Replace($HOME, '~') } | |
$prompt = @( | |
"" | |
@( | |
"$($colors.separator)$($symbols.pathOpen) $($colors.location)$path $($colors.separator)$($symbols.pathClose)" | |
"$($symbols.timeOpen) $($colors.time)$time $($colors.separator)$($symbols.timeClose)" | |
if (Test-Path .git) { Generate-Git-Path } else { '' } | |
if ($admin) { " $($colors.admin)$($symbols.admin)" } else { '' } | |
) -join "" | |
"$($colors.name)$name $( | |
if ($lastCmdFailed) { $colors.error } else { $colors.separator } | |
)$($symbols.prompt)$r " | |
) | |
$prompt -join "`n" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment