Skip to content

Instantly share code, notes, and snippets.

@tgmayfield
Created August 21, 2012 12:16
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 tgmayfield/3415004 to your computer and use it in GitHub Desktop.
Save tgmayfield/3415004 to your computer and use it in GitHub Desktop.
if (test-path function:\prompt) {
$oldPrompt = ls function: | ? {$_.Name -eq "prompt"}
remove-item -force function:\prompt
}
function prompt {
$color = "Green"
$markers = "DarkGreen"
$important = "Red"
function outputNormal([string]$message)
{
Write-Host ($message) -NoNewLine -ForegroundColor $color
}
function outputMarker([string]$message)
{
Write-Host ($message) -NoNewLine -ForegroundColor $markers
}
function outputImportant([string]$message)
{
Write-Host ($message) -NoNewLine -ForegroundColor $important
}
function writeGitStatus {
$branch = git branch -v -v --color=never 2>&1
if ($branch.Exception -ne $null) {
return $false
}
outputMarker " ("
$branch | foreach {
if ($_ -match "^\*\s*([^ ]*)\s*") {
$branch = $matches[1].trim()
outputNormal $branch
}
if ($_ -match "ahead ([0-9]+)") {
outputImportant ("+" + $matches[1])
}
if ($_ -match "behind ([0-9]+)") {
outputImportant ("-" + $matches[1])
}
}
$untracked = 0
$updates = 0
$deletes = 0
$addsIndex = 0
$deletesIndex = 0
$updatesIndex = 0
$renamesIndex = 0
git status --porcelain | foreach {
if ($_ -match "^([\w?]|\s)([\w?])?\s*") {
switch ($matches[1]) {
"A" { $addsIndex++ }
"M" { $updatesIndex++ }
"D" { $deletesIndex++ }
"R" { $renamesIndex++ }
"?" { $untracked++ }
}
switch ($matches[2]) {
"M" { $updates++ }
"D" { $deletes++ }
}
}
}
# Treat renames as modifications
$updatesIndex += $renamesIndex
if ($addsIndex -gt 0) {
outputNormal " a:$addsIndex"
}
if (($updates + $updatesIndex) -gt 0) {
outputNormal " m:$updatesIndex"
if ($updates -gt 0) {
outputNormal "/$updates"
}
}
if (($deletes + $deletesIndex) -gt 0) {
outputNormal " d:$deletesIndex"
if ($deletes -gt 0) {
outputNormal "/$deletes"
}
}
if ($untracked -gt 0) {
outputNormal " ?:$untracked"
}
outputMarker ")"
return $true
}
function writeHgStatus {
$summary = hg summary 2>&1
if ($summary.Exception -eq $null) {
$regex = "(?si)(parent:(?<parent>.*?)(\n|\r)+.*?)(branch:(?<branch>.*)\s)(commit:(?<commit>.*)\s)(update:(?<update>.*))";
$summary = [System.String]::Join([System.Environment]::NewLine,$summary)
$res = $summary -match $regex
$status = "hg {0} c:{1}" -f $matches["branch"].Trim(), $matches["commit"].Trim()
outputNormal $status
return $true
}
else {
return $false
}
}
& $oldPrompt
writeGitStatus
writeHgStatus
return " "
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment