Created
October 10, 2017 13:16
-
-
Save dariuszparys/c47adfbdd52a30d3f9dacdb348461234 to your computer and use it in GitHub Desktop.
Integrating Git into Powershell prompt
This file contains 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 Prompt | |
# { | |
# $PromptData="PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) " | |
# $host.ui.RawUI.WindowTitle=$PromptData+'-'+(Get-Date).tostring() | |
# } | |
$Global:CurrentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent() | |
$UserType = "User" | |
$CurrentUser.Groups | foreach { | |
if ($_.value -eq "S-1-5-32-544") { | |
$UserType = "Admin" } | |
} | |
function prompt { | |
# Fun stuff if using the standard PowerShell prompt; not useful for Console2. | |
# This, and the variables above, could be commented out. | |
if($UserType -eq "Admin") { | |
$host.UI.RawUI.WindowTitle = "" + $(get-location) + " : Admin" | |
$host.UI.RawUI.ForegroundColor = "white" | |
} | |
else { | |
$host.ui.rawui.WindowTitle = $(get-location) | |
} | |
$status_string = "" | |
$symbolicref = git symbolic-ref HEAD | |
if($symbolicref -ne $NULL) { | |
$status_string += "[" + $symbolicref.substring($symbolicref.LastIndexOf("/") +1) + "] " | |
$differences = (git diff-index --name-status HEAD) | |
$git_update_count = [regex]::matches($differences, "M`t").count | |
$git_create_count = [regex]::matches($differences, "A`t").count | |
$git_delete_count = [regex]::matches($differences, "D`t").count | |
$status_string += "c:" + $git_create_count + " u:" + $git_update_count + " d:" + $git_delete_count | |
} | |
else { | |
$status_string = "-> " | |
} | |
if ($status_string.StartsWith("[")) { | |
Write-Host ("-> " + $(get-location) + " " + $status_string) -nonewline -foregroundcolor yellow | |
} | |
else { | |
Write-Host ($status_string + $(get-location)) -nonewline -foregroundcolor green | |
} | |
return " " | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment