Skip to content

Instantly share code, notes, and snippets.

@NithinChintala
Last active February 16, 2022 18:11
Show Gist options
  • Save NithinChintala/9e7e7d1d2909be34ea75211b2ed2a905 to your computer and use it in GitHub Desktop.
Save NithinChintala/9e7e7d1d2909be34ea75211b2ed2a905 to your computer and use it in GitHub Desktop.
A Simple Unix Style Prompt in PowerShell
# ===Prompt Synopsis===
# - Inspired by https://github.com/rwxrob
# - A simple Unix style prompt in PowerShell
# - Requires git to be installed
# - "user@host:dir(branch)$ "
# ===Color Mapping===
# user - yellow
# @ - black
# host - blue
# dir - magenta
# ( - black
# branch - cyan|red
# ) - black
# $ - yellow
# ===Explanation===
# - The "user" is defined by $env:USERNAME
# - The "host" is defined by $env:COMPUTERNAME
# - If you don't like how these look you can always define these as
# hard coded strings below, i.e. $u = "bob" and $h = "windows"
#
# - The "dir" is the basename of the current working directory. If the
# current directory is the same as $HOME "dir" will be a "~"
#
# - The "branch" is the current git branch checked out. If the current
# directory is not in a git repository then the entire "(branch)" segment
# will be omitted. If the current branch is "master" or "main" the color
# will be red. Otherwise the branch color is cyan.
#
# - The "@", ":", "(", ")" and "$" are those literal symbols
#
# - If you want to change any of the colors just replace the color variables
# in the "WriteOutput" command to whatever you like.
# ===Color Abbreviation===
# x = reset
# k = blacK r = Red
# g = Green y = Yellow
# b = Blue m = Magenta
# c = Cyan w = White
function prompt {
$esc = [char]0x1b
$x = "$esc[0m"
$k = "$esc[90m"
$r = "$esc[91m"
$g = "$esc[92m"
$y = "$esc[93m"
$b = "$esc[94m"
$m = "$esc[95m"
$c = "$esc[96m"
$w = "$esc[97m"
$sep = [regex]::Escape([IO.Path]::DirectorySeparatorChar)
$dir = $PWD.Path -replace ".*$sep"
if ( $PWD.Path -eq $HOME ) {
$dir = "~"
}
$br = git branch --show-current 2> $null
if ( $br ) {
$brc = $c
if ( $br -eq "main" -or $br -eq "master" ) {
$brc = $r
}
$br = "$k($brc$br$k)"
}
$u = $env:USERNAME
$h = $env:COMPUTERNAME
$end = "$"
Write-Output "$y$u$k@$b$h${k}:$m$dir$br$y$end$x "
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment