Skip to content

Instantly share code, notes, and snippets.

@dstufft
Created June 24, 2011 13:08
Show Gist options
  • Save dstufft/1044731 to your computer and use it in GitHub Desktop.
Save dstufft/1044731 to your computer and use it in GitHub Desktop.
Windows PowerShell 2.0 Profile that adds a descriptive prompt suitable for working with git (or hg) and virtualenvs
# Written by Donald Stufft
#
# My Windows PowerShell Profile. It gives a prompt that looks like
# UserName at MachineName in ~/path on master^1?!
# (virtualenvname) ±
#
# Explanation:
# If you are in a git repo the prompt character will be ±, ¥ for mercurial and » otherwise.
# If you are in a git repo it will include the "on ...." bit.
# It will replace master with the name of the current branch
# It will include ^# where # is how many commits ahead of origin you are
# It will include ? if you have untracked files and ! if you have uncommited changes
# If you are using virtualenvs then the current
# virtualenv name will appear in front of the prompt character inside of ()
$GIT_PROMPT_PREFIX = " on "
$GIT_PROMPT_SUFFIX = ""
$GIT_PROMPT_DIRTY = "!"
$GIT_PROMPT_UNTRACKED = "?"
$GIT_PROMPT_CLEAN = ""
$GIT_PROMPT_AHEAD = "^"
$THEME_COLOR_USERNAME = [ConsoleColor]::Magenta
$THEME_COLOR_HOST = [ConsoleColor]::Yellow
$THEME_COLOR_PATH = [ConsoleColor]::Green
$THEME_COLOR_BRANCH = [ConsoleColor]::Magenta
$THEME_COLOR_AHEAD_PREFIX = [ConsoleColor]::White
$THEME_COLOR_AHEAD_TEXT = [ConsoleColor]::Magenta
$THEME_COLOR_DIRTY = [ConsoleColor]::Red
$THEME_COLOR_UNTRACKED = [ConsoleColor]::Blue
$THEME_COLOR_CLEAN = [ConsoleColor]::White
$THEME_COLOR_VIRTUALENV_INFO_WRAP = [ConsoleColor]::White
$THEME_COLOR_VIRTUALENV_INFO_TEXT = [ConsoleColor]::White
Import-Module virtualenvwrapper
function shorten-path([string] $path)
{
return $path.Replace($HOME, '~').Replace("\", "/")
}
function git-prompt-info
{
if (Get-Command "git" -errorAction SilentlyContinue)
{
$ret = ""
$ref=$(git symbolic-ref HEAD)
if (!$ref) { return "" }
Write-Host($GIT_PROMPT_PREFIX) -n
Write-Host($ref -replace "refs/heads/", "") -n -f $THEME_COLOR_BRANCH
Write-Host($(git-prompt-ahead)) -n
Write-Host($(git-prompt-status)) -n
Write-Host($GIT_PROMPT_SUFFIX) -n
}
return ""
}
function git-prompt-ahead
{
if (Get-Command "git" -errorAction SilentlyContinue)
{
$GITSTATUS = $(git status)
if ($GITSTATUS)
{
$AHEAD = [string]$($GITSTATUS | select-string "^# Your branch is ahead of")
if ($AHEAD)
{
$AHEAD = $AHEAD.SubString(0, $AHEAD.LastIndexOf(" "))
$AHEAD = $AHEAD.SubString($AHEAD.LastIndexOf(" ") + 1)
Write-Host($GIT_PROMPT_AHEAD) -n -f $THEME_COLOR_AHEAD_PREFIX
Write-Host($AHEAD) -n -f $THEME_COLOR_AHEAD_TEXT
}
}
}
return ""
}
function git-prompt-status
{
if (Get-Command "git" -errorAction SilentlyContinue)
{
$GITSTATUS = $(git status --porcelain)
if (!$GITSTATUS)
{
Write-Host($GIT_PROMPT_CLEAN) -n -f $THEME_COLOR_CLEAN
}
else
{
if ($GITSTATUS | select-string ^??)
{
Write-Host($GIT_PROMPT_UNTRACKED) -n -f $THEME_COLOR_UNTRACKED
}
if ($GITSTATUS | select-string "^ ?[MADRCU]")
{
Write-Host($GIT_PROMPT_DIRTY) -n -f $THEME_COLOR_DIRTY
}
}
}
return ""
}
function virtualenv-info
{
if($env:VIRTUAL_ENV)
{
Write-Host("(") -n -f $THEME_COLOR_VIRTUALENV_INFO_WRAP
Write-Host($(split-path $env:VIRTUAL_ENV -leaf)) -n -f $THEME_COLOR_VIRTUALENV_INFO_TEXT
Write-Host(") ") -n -f $THEME_COLOR_VIRTUALENV_INFO_WRAP
}
return ""
}
function prompt-char
{
if (Get-Command "git" -errorAction SilentlyContinue)
{
git branch > $null
if ($?) { return "± " }
}
if (Get-Command "hg" -errorAction SilentlyContinue)
{
hg root > $null
if ($?) { return "¥ " }
}
return "» "
}
function prompt
{
Write-Host ("")
Write-Host ([Environment]::UserName) -n -f $THEME_COLOR_USERNAME
Write-Host (" at ") -n
Write-Host ([Environment]::MachineName) -n -f $THEME_COLOR_HOST
Write-Host (" in ") -n
Write-Host (shorten-path (pwd).PATH) -n -f $THEME_COLOR_PATH
Write-Host (git-prompt-info)
Write-Host (virtualenv-info) -n
return $(prompt-char)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment