Created
October 1, 2012 19:25
-
-
Save phansch/3813881 to your computer and use it in GitHub Desktop.
My custom 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
| 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 " " | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
shorten-path([string] $path) was taken from this article.