Skip to content

Instantly share code, notes, and snippets.

@yltsrc
Last active October 5, 2015 19:27
Show Gist options
  • Save yltsrc/2862984 to your computer and use it in GitHub Desktop.
Save yltsrc/2862984 to your computer and use it in GitHub Desktop.
bash git prompt
# git prompt
DULL=0
FG_BLACK=30
FG_RED=31
FG_GREEN=32
FG_YELLOW=33
FG_BLUE=34
FG_CYAN=36
FG_WHITE=37
BG_NULL=00
ESC="\033"
NORMAL="$ESC[m"
RESET="$ESC[${DULL};${FG_WHITE};${BG_NULL}m"
RED="$ESC[${DULL};${FG_RED}m"
GREEN="$ESC[${DULL};${FG_GREEN}m"
YELLOW="$ESC[${DULL};${FG_YELLOW}m"
BLUE="$ESC[${DULL};${FG_BLUE}m"
CYAN="$ESC[${DULL};${FG_CYAN}m"
WHITE="$ESC[${DULL};${FG_WHITE}m"
git_prompt() {
local leader="$(prompt_leader)"
local g="$(__gitdir)"
if [ -n "$g" ]; then
local MINUTES_SINCE_LAST_COMMIT=`minutes_since_last_commit`
if [ "$MINUTES_SINCE_LAST_COMMIT" -gt 30 ]; then
local COLOR=${RED}
elif [ "$MINUTES_SINCE_LAST_COMMIT" -gt 10 ]; then
local COLOR=${YELLOW}
else
local COLOR=${GREEN}
fi
local SINCE_LAST_COMMIT="${COLOR}$(minutes_since_last_commit)m${NORMAL}"
# The __git_ps1 function inserts the current git branch where %s is
local GIT_PROMPT=`__git_ps1 "(${CYAN}%s${NORMAL}|${SINCE_LAST_COMMIT}) ${leader}"`
echo ${GIT_PROMPT}
else
echo `printf -- "${leader}"`
fi
}
function minutes_since_last_commit {
now=`date +%s`
last_commit=`git log --pretty=format:'%at' -1`
seconds_since_last_commit=$((now-last_commit))
minutes_since_last_commit=$((seconds_since_last_commit/60))
echo $minutes_since_last_commit
}
function prompt_leader {
local CODE=$?
if [[ $CODE -eq 0 ]] ; then
local COLOR=${GREEN}
else
local COLOR=${RED}
fi
echo "${COLOR}→${NORMAL}"
}
PS1="\h:\W \$(git_prompt) "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment