Permalink
Browse files

Add time since last Git commit to prompt.

  • Loading branch information...
1 parent 30de238 commit 4fca5bf863e41867972d7c45392625129a0b0791 @bnadlerjr committed Apr 7, 2012
Showing with 61 additions and 1 deletion.
  1. +1 −1 bash/config
  2. +59 −0 bash/git-prompt
  3. +1 −0 bashrc
View
@@ -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→ "
View
@@ -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
+}
View
1 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.