Last active
August 29, 2015 14:04
-
-
Save dschuetz/0aa7fb2fa185d2942b98 to your computer and use it in GitHub Desktop.
Pretty-print prompt when in a git repo
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
# | |
# 25 jul 2014 | |
# | |
# display the current git repository and some important stats in the shell prompt | |
# | |
# stick this in bashrc or wherever such tweaks are executed. | |
# | |
# Most code taken from http://vvv.tobiassjosten.net/bash/dynamic-prompt-with-git-and-ansi-colors/ | |
# | |
# tweaked to put on a couple of lines, along with an edited directory path prompt | |
# (taken, naturally enough, from stackoverflow: http://stackoverflow.com/questions/5687446/bash-custom-ps1-with-nice-working-directory-path) | |
# (couldn't use PROMPT_DIRTRIM on OS X as bash is too old) | |
# | |
# Also shows: | |
# user.email (so I can remember whether I'm signing commits as me, or as me @ work) | |
# unpushed commits (so that I remember to actually share my work) | |
# | |
# final prompt looks something like this: | |
# | |
# ~/Work/.../Current/some-test-in-progress | |
# [[email protected]|master:1] | |
# $ | |
# Dirty bits ("master" and ":1") are in red. well, magenta. | |
# (where "dirty" means things that aren't committed or pushed). | |
# | |
# If the current folder isn't a .git repo, then the entire git subline simply isn't shown. | |
# | |
# I also have a leading blank line and put the $ prompt at the left margin to make | |
# things easier to read, and to give me lots of space for entering long commands. | |
# | |
# Most people will probably hate that. | |
# | |
# Configure colors, if available. | |
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then | |
c_reset='\[\e[0m\]' | |
c_user='\[\e[1;33m\]' | |
c_path='\[\e[0;34m\]' | |
c_git_cleancleann='\[\e[0;36m\]' | |
c_git_dirty='\[\e[0;35m\]' | |
else | |
c_reset= | |
c_user= | |
c_git_cleancleann_path= | |
c_git_clean= | |
c_git_dirty= | |
fi | |
# Function to assemble the Git parsingart of our prompt. | |
git_prompt () | |
{ | |
if ! git rev-parse --git-dir > /dev/null 2>&1; then | |
return 0 | |
fi | |
git_branch=$(git branch 2>/dev/null | sed -n '/^\*/s/^\* //p') | |
git_user=$(git config user.email 2>/dev/null) | |
if git diff --quiet 2>/dev/null >&2; then | |
git_color="$c_git_clean" | |
else | |
git_color="$c_git_dirty" | |
fi | |
unpushed=`git status | grep 'ahead of' | awk '{printf("%s",$(NF-1))}' 2>/dev/null` | |
if [ ! -z $unpushed ]; then | |
git_unpushed=${c_git_dirty}:$unpushed${c_reset} | |
else | |
git_unpushed='' | |
fi | |
echo "\n[${c_user}$git_user${c_reset}|$git_color$git_branch${c_reset}$git_unpushed]" | |
} | |
# | |
# trim prompt folder list | |
# | |
export MYPS='$(echo -n "${PWD/#$HOME/~}" | awk -F "/" '"'"'{ | |
if (length($0) > 14) { if (NF>3) print $1 "/" $2 "/.../" $(NF-1) "/" $NF; | |
else if (NF>3) print $1 "/" $2 "/.../" $NF; | |
else print $1 "/.../" $NF; } | |
else print $0;}'"'"')' | |
# Thy holy prompt. | |
PROMPT_COMMAND='PS1="\n${c_path}${MYPS}${c_reset}$(git_prompt)\n\$ "' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment