Skip to content

Commit

Permalink
Add time since last Git commit to prompt.
Browse files Browse the repository at this point in the history
  • Loading branch information
bnadlerjr committed Apr 7, 2012
1 parent 30de238 commit 4fca5bf
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion bash/config
Expand Up @@ -71,4 +71,4 @@ _vgem()
complete -F _vgem -o dirnames vgem

# prompt
PS1="\[$BLDBLK\]\n\u:\w \[$TXTBLU\](\$(vcprompt -f %b%m%u)\[$TXTRST\]) \n→ "
PS1="\[$BLDBLK\]\n\u:\w \[$TXTBLU\](\$(srb_git_prompt)\[$TXTBLU\])\[$TXTRST\] \n→ "
59 changes: 59 additions & 0 deletions bash/git-prompt
@@ -0,0 +1,59 @@
# Combine Git branch name and time since last commit.
# Taken from https://gist.github.com/926846 with some modifications.
#
# Example:
# PS1="(\h • ${YELLOW}\W${NORMAL}\$(srb_git_prompt))${BRIGHT_BLUE}\$${NORMAL} "
#
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=${TXTBLU}
elif [ "$UNIT" == "h" ]; then
local COLOR=${TXTYLW}
else
local COLOR=${TXTRED}
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
echo `__git_ps1 "%s ${COLOR}∆${DELTA}"`
fi
}
1 change: 1 addition & 0 deletions bashrc
@@ -1,5 +1,6 @@
. ~/bin/dotfiles/bash/env
. ~/bin/dotfiles/bash/git-completion
. ~/bin/dotfiles/bash/git-prompt
. ~/bin/dotfiles/bash/config
. ~/bin/dotfiles/bash/aliases
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"

0 comments on commit 4fca5bf

Please sign in to comment.