Skip to content

Instantly share code, notes, and snippets.

@gnmerritt
Last active September 7, 2018 16:50
Show Gist options
  • Save gnmerritt/c3a996d7767d6be457f2df0d952be97b to your computer and use it in GitHub Desktop.
Save gnmerritt/c3a996d7767d6be457f2df0d952be97b to your computer and use it in GitHub Desktop.
multi-line PS1 with git branch, last command status & last command timer
# colors with control characters already in place, no need to echo them again when using
# adapted from: https://stackoverflow.com/a/28115284
RESTORE=$(echo -en '\001\033[0m\002')
RED=$(echo -en '\001\033[00;31m\002')
GREEN=$(echo -en '\001\033[00;32m\002')
YELLOW=$(echo -en '\001\033[00;33m\002')
BLUE=$(echo -en '\001\033[00;34m\002')
MAGENTA=$(echo -en '\001\033[00;35m\002')
PURPLE=$(echo -en '\001\033[00;35m\002')
CYAN=$(echo -en '\001\033[00;36m\002')
LIGHTGRAY=$(echo -en '\001\033[00;37m\002')
LRED=$(echo -en '\001\033[01;31m\002')
LGREEN=$(echo -en '\001\033[01;32m\002')
LYELLOW=$(echo -en '\001\033[01;33m\002')
LBLUE=$(echo -en '\001\033[01;34m\002')
LMAGENTA=$(echo -en '\001\033[01;35m\002')
LPURPLE=$(echo -en '\001\033[01;35m\002')
LCYAN=$(echo -en '\001\033[01;36m\002')
WHITE=$(echo -en '\001\033[01;37m\002')
# timer functions adapted from https://jakemccrary.com/blog/2015/05/03/put-the-last-commands-run-time-in-your-bash-prompt/
function timer_start() {
timer=${timer:-$SECONDS}
}
# sets $timer_show with the runtime of the last command
# empty if the command ran in < 1s
# units switch at 1m31s and 1hr31m0s
function timer_stop() {
secs=$(($SECONDS - $timer))
unset timer
if [ $secs -lt 1 ]; then
timer_show=" "
return 0
fi
hours=$(($secs / 3600))
mins=$(($secs / 60))
if [ $secs -gt 90 ]; then
secs=$(($secs % 60))
if [ $mins -gt 90 ]; then
mins=$(($mins % 60))
timer_show="${hours}hr${mins}m${secs}s"
else
timer_show="${mins}m${secs}s"
fi
else
timer_show="${secs}s"
fi
timer_show=" $timer_show "
}
trap 'timer_start' DEBUG
# sets $cmd_face with a colored emoticon representing the exit status of the last command
function smile() {
if [ $? -eq 0 ]; then
cmd_face="${GREEN}:-)${RESTORE}"
else
cmd_face="${RED}8-(${RESTORE}"
fi
}
# sets $vc_branch with the current git branch
function vc_branch() {
vc_branch=`git rev-parse --abbrev-ref HEAD 2>/dev/null`
if [ "$vc_branch" != "" ]; then
vc_branch="${LPURPLE}[${vc_branch}]${RESTORE} "
fi
}
# order matters here: smile needs to be first to check the last command and timer_stop
# needs to be last in order to time the next command rather than the prompt commands
if [ "$PROMPT_COMMAND" == "" ]; then
PROMPT_COMMAND="smile; vc_branch; timer_stop"
else
PROMPT_COMMAND="smile; vc_branch; $PROMPT_COMMAND; timer_stop"
fi
if [ "$PS1" ]; then
# bash 3.0 and line-wrapping compatible version:
# gives the following (where smiley indicates last cmd sucessfully ran)
# powered by the functions above and $PROMPT_COMMAND
#
# [HH:MM AM/PM] user@host: [current branch (if any)] $PWD
# :-) / 8-( {last command runtime (s)} $
PS1="${BOLD}[\@]${RESTORE} ${YELLOW}\u${RESTORE}@${GREEN}\h \${vc_branch}${BLUE}\w/${RESTORE} \n \${cmd_face}\${timer_show}\$ "
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment