Skip to content

Instantly share code, notes, and snippets.

@phansch
Created October 1, 2012 19:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phansch/3813881 to your computer and use it in GitHub Desktop.
Save phansch/3813881 to your computer and use it in GitHub Desktop.
My custom PowerShell prompt
function shorten-path([string] $path) {
$loc = $path.Replace($HOME, '~')
# remove prefix for UNC paths
$loc = $loc -replace '^[^:]+::', ''
# make path shorter like tabs in Vim,
# handle paths starting with \\ and . correctly
return ($loc -replace '\\(\.?)([^\\])[^\\]*(?=\\)','\$1$2')
}
function prompt {
if(Test-Path .git) {
# retrieve branch name
$symbolicref = (git symbolic-ref HEAD)
$branch = $symbolicref.substring($symbolicref.LastIndexOf("/") +1)
# retrieve branch status
$differences = (git diff-index HEAD --name-status)
$git_mod_count = [regex]::matches($differences, 'M\s').count
$git_add_count = [regex]::matches($differences, 'A\s').count
$git_del_count = [regex]::matches($differences, 'D\s').count
$branchData = " +$git_add_count ~$git_mod_count -$git_del_count"
# write status string (-n : NoNewLine; -f : ForegroundColor)
write-host 'GIT' -n -f White
write-host ' {' -n -f Yellow
write-host (shorten-path (pwd).Path) -n -f White
write-host ' [' -n -f Yellow
write-host $branch -n -f Cyan
write-host $branchData -n -f Red
write-host ']' -n -f Yellow
write-host ">" -n -f Yellow
}
else {
# write status string
write-host 'PS' -n -f White
write-host ' {' -n -f Yellow
write-host (shorten-path (pwd).Path) -n -f White
write-host ">" -n -f Yellow
}
return " "
}
@phansch
Copy link
Author

phansch commented Oct 1, 2012

shorten-path([string] $path) was taken from this article.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment