Skip to content

Instantly share code, notes, and snippets.

@mgpb
Created April 19, 2011 05:03
Show Gist options
  • Save mgpb/926846 to your computer and use it in GitHub Desktop.
Save mgpb/926846 to your computer and use it in GitHub Desktop.
bash prompt with git branch name and time since commit, based on what @garybernhardt did.
# It looks like this but with color and stuff:
# (macbot • sample_app ┣master ∆68m)$
# (macbot • autumn ┣master ∆2yr8mo)$
function relative_time_since_last_commit {
last_commit=`git log --pretty=format:'%ar' -1`
echo ${last_commit}
}
function format_unit {
local UNIT=$1
case "$UNIT" in
seconds) UNIT="s" ;;
minutes) UNIT="m" ;;
hours) UNIT="h" ;;
days) UNIT="d" ;;
weeks) UNIT="w" ;;
months) UNIT="mo" ;;
years) UNIT="yr" ;;
*);;
esac
echo ${UNIT}
}
function color_based_on_unit {
local UNIT=$1
if [ "$UNIT" == "s" ] || [ "$UNIT" == "m" ]; then
local COLOR=${GREEN}
elif [ "$UNIT" == "h" ]; then
local COLOR=${YELLOW}
else
local COLOR=${RED}
fi
echo ${COLOR}
}
srb_git_prompt() {
local g="$(__gitdir)"
if [ -n "$g" ]; then
local SINCE_LAST_COMMIT=$(relative_time_since_last_commit)
SINCE_LAST_COMMIT=(${SINCE_LAST_COMMIT// / })
local VALUE=${SINCE_LAST_COMMIT[0]}
local UNIT=$(format_unit ${SINCE_LAST_COMMIT[1]/,/})
# for old projects, git reports years and months
if [ ${SINCE_LAST_COMMIT[2]} != "ago" ]; then
local EXTRA_VALUE=${SINCE_LAST_COMMIT[2]}
local EXTRA_UNIT=$(format_unit ${SINCE_LAST_COMMIT[3]/,/})
fi
local COLOR=$(color_based_on_unit $UNIT)
local DELTA="${VALUE}${UNIT}${EXTRA_VALUE}${EXTRA_UNIT}"
# The __git_ps1 function inserts the current git branch where %s is
local GIT_PROMPT=`__git_ps1 "┣ %s ∆${COLOR}${DELTA}${NORMAL}"`
echo " ${GIT_PROMPT}"
fi
}
PS1="(\h • ${YELLOW}\W${NORMAL}\$(srb_git_prompt))${BRIGHT_BLUE}\$${NORMAL} "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment