Skip to content

Instantly share code, notes, and snippets.

@Bigous
Created June 3, 2020 15:36
Show Gist options
  • Save Bigous/eca6f10e560403da6b4e9960d9a51f6d to your computer and use it in GitHub Desktop.
Save Bigous/eca6f10e560403da6b4e9960d9a51f6d to your computer and use it in GitHub Desktop.
Custom Prompt for PowerShell which shows information on GIT repositories and Anaconda environmant if both are available.
#region Global Colors
$e=[char]27
$cReset="$e[0m"
# $e[(x)m -> 30-37,90-97 Terminal preset foreground colors; 38 Terminal custom foreground color; 40-47,100-107 Terminal preset background colors; 48 Terminal custom background color
# $e[38;2;(r);(g);(b)m -> RGB
# $e[38;5;(n)m -> 0-15 EGA colors; 16-231 6-bit table color; 232-255 Gray scale
# $cFgDarkBlack="$e[38;2;0;0;0m"
# $cFgDarkRed="$e[38;2;128;0;0m"
# $cFgDarkGreen="$e[38;2;0;128;0m"
# $cFgDarkYellow="$e[38;2;128;128;0m"
# $cFgDarkBlue="$e[38;2;0;0;128m"
# $cFgDarkMagenta="$e[38;2;128;0;128m"
# $cFgDarkCyan="$e[38;2;0;128;128m"
# $cFgDarkWhite="$e[38;2;128;128;128m"
# $cFgBrightBlack="$e[38;2;96;96;96m"
# $cFgBrightRed="$e[38;2;223;0;0m"
# $cFgBrightGreen="$e[38;2;0;223;0m"
# $cFgBrightYellow="$e[38;2;223;223;0m"
# $cFgBrightBlue="$e[38;2;0;0;223m"
# $cFgBrightMagenta="$e[38;2;223;0;223m"
# $cFgBrightCyan="$e[38;2;0;223;223m"
# $cFgBrightWhite="$e[38;2;223;223;223m"
# $cFgDarkBlack="$e[38;2;0;0;0m"
# $cFgDarkRed="$e[38;2;128;0;0m"
# $cFgDarkGreen="$e[38;2;0;128;0m"
# $cFgDarkYellow="$e[38;2;128;128;0m"
# $cFgDarkBlue="$e[38;2;0;0;128m"
# $cFgDarkMagenta="$e[38;2;128;0;128m"
# $cFgDarkCyan="$e[38;2;0;128;128m"
# $cFgDarkWhite="$e[38;2;128;128;128m"
# $cFgBrightBlack="$e[38;2;96;96;96m"
# $cFgBrightRed="$e[38;2;223;0;0m"
# $cFgBrightGreen="$e[38;2;0;223;0m"
# $cFgBrightYellow="$e[38;2;223;223;0m"
# $cFgBrightBlue="$e[38;2;0;0;223m"
# $cFgBrightMagenta="$e[38;2;223;0;223m"
# $cFgBrightCyan="$e[38;2;0;223;223m"
# $cFgBrightWhite="$e[38;2;223;223;223m"
$cFgDarkBlack="$e[38;5;0m"
$cFgDarkRed="$e[38;5;1m"
$cFgDarkGreen="$e[38;5;2m"
$cFgDarkYellow="$e[38;5;3m"
$cFgDarkBlue="$e[38;5;4m"
$cFgDarkMagenta="$e[38;5;5m"
$cFgDarkCyan="$e[38;5;6m"
$cFgDarkWhite="$e[38;5;7m"
$cFgBrightBlack="$e[38;5;8m"
$cFgBrightRed="$e[38;5;9m"
$cFgBrightGreen="$e[38;5;10m"
$cFgBrightYellow="$e[38;5;11m"
$cFgBrightBlue="$e[38;5;12m"
$cFgBrightMagenta="$e[38;5;13m"
$cFgBrightCyan="$e[38;5;14m"
$cFgBrightWhite="$e[38;5;15m"
$cBgDarkBlack="$e[48;5;0m"
$cBgDarkRed="$e[48;5;1m"
$cBgDarkGreen="$e[48;5;2m"
$cBgDarkYellow="$e[48;5;3m"
$cBgDarkBlue="$e[48;5;4m"
$cBgDarkMagenta="$e[48;5;5m"
$cBgDarkCyan="$e[48;5;6m"
$cBgDarkWhite="$e[48;5;7m"
$cBgBrightBlack="$e[48;5;8m"
$cBgBrightRed="$e[48;5;9m"
$cBgBrightGreen="$e[48;5;10m"
$cBgBrightYellow="$e[48;5;11m"
$cBgBrightBlue="$e[48;5;12m"
$cBgBrightMagenta="$e[48;5;13m"
$cBgBrightCyan="$e[48;5;14m"
$cBgBrightWhite="$e[48;5;15m"
$cFgPink="$e[38;5;197m"
$cBgPink="$e[48;5;197m"
$cFgOrange="$e[38;2;255;69;0m"
$cBgOrange="$e[48;2;255;69;0m"
$cFgBrown="$e[38;2;139;69;19m"
$cBgBrown="$e[48;2;139;69;19m"
#endregion
#region Custom Prompt
function Prompt {
# Verify if user is admin
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal( [Security.Principal.WindowsIdentity]::GetCurrent() )
$isAdmin = $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if( $isAdmin ) {
# Show user in red if admin.
$prompt = "$cFgDarkRed$env:username$cReset"
} else {
$prompt = "$cFgBrightBlack$env:username"
}
# show computer name.
$prompt = "$prompt@$env:COMPUTERNAME $cReset"
# Are we on a git folder?
$git_branch=(git rev-parse --abbrev-ref HEAD)
if( $git_branch ) {
# Show the branch (and [] in dark green - always)
$gs = (git status -s)
if($gs) {
# Yellow if we need to commit/add something
$branch_color = $cFgDarkYellow
} else {
# Green if no commit is needed
$branch_color = $cFgDarkGreen
}
$prompt = "$prompt$cFgDarkGreen[$branch_color$git_branch"
# Comparing with upstream (if available)
$gbs = (git status -b -s)
$gbs_ahead = ($gbs | select-string -pattern '\[ahead (\d+)\]')
$gbs_behind = ($gbs | select-string -pattern '\[behind (\d+)\]')
$gbs_add = ""
if($gbs_ahead.matches.success) {
# if we are ahead, show the number of commits in cyan
$gbs_add = "$cFgDarkCyan+" + $gbs_ahead.matches.groups[1].value
}
if($gbs_behind.matches.success) {
# if we are behind, show the number of commits in dark red
$gbs_add = "$cFgDarkRed-" + $gbs_behind.matches.groups[1].value
}
$prompt = "$prompt$gbs_add$cFgDarkGreen]$cReset"
}
# Do we have an Anaconda environment running
if($Env:CONDA_DEFAULT_ENV) {
# Show me which in dark Bright Black which is dark gray (and parenthesis in default color, always).
$prompt = "$prompt($cFgBrightBlack$Env:CONDA_DEFAULT_ENV$cReset)"
}
# Show current directory and the prompt on next line (if we have nested prompts, show how many using the same number of '>' before prompt)
"$prompt $($executionContext.SessionState.Path.CurrentLocation)$([char]10)$('>' * ($nestedPromptLevel + 1)) $cReset";
}
#endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment