Skip to content

Instantly share code, notes, and snippets.

@wwoods
Last active May 30, 2016 22:04
Show Gist options
  • Save wwoods/7920366 to your computer and use it in GitHub Desktop.
Save wwoods/7920366 to your computer and use it in GitHub Desktop.
My bash prompt settings
####
# Bash prompt w/ runtime of last series of commands (e.g. sleep 1 && sleep 1 correctly represented as 2s)
####
smiley () {
printf :\\"$(($??50:51))";
}
print_elapsed () {
NOW=`date +%s%N`
DIFF=$(expr $NOW - $PT_LAST_TIME)
DIV=1
SUFFIX="ns"
DIFFLEN=$(expr length $DIFF)
if [[ $DIFFLEN -gt 10 ]]; then
DIV=1000000000
SUFFIX="s"
elif [[ $DIFFLEN -gt 7 ]]; then
DIV=1000000
SUFFIX="m"
elif [[ $DIFFLEN -gt 4 ]]; then
DIV=1000
SUFFIX="u"
fi
# expr prints a warning when result is float, so...
DIFF=$(echo "$DIFF $DIV" | awk '{ print $1 / $2 - ($1 / $2 % 1); }')
printf %4d $DIFF
echo -e $SUFFIX
}
print_path () {
pwd | awk '{print substr($0, length($0) - 59)}'
# Update our command number....
echo `expr \`cat $PT_CMD_FILE 2>/dev/null\` + 1` > $PT_CMD_FILE
}
maybeUpdate () {
VAL=$1
if [ "$2" != "$3" ]; then
VAL=`date +%s%N`
# echo -e "SET: \n $2\n $3" 1>&2
fi
printf $VAL
}
export PT_DIR=/run/shm/prompt
mkdir -p $PT_DIR
export PT_CMD_FILE="$PT_DIR/prompt-cmd-no-$$"
export PT_LAST_TIME=`date +%s%N`
export PT_CMD_LAST=0
trap 'rm -f $PT_CMD_FILE' EXIT
trap 'PT_LAST_TIME=`maybeUpdate $PT_LAST_TIME $PT_CMD_LAST "\`cat $PT_CMD_FILE 2>/dev/null\`"` PT_CMD_LAST=`cat $PT_CMD_FILE 2>/dev/null`' DEBUG
export PS1="\h\$(smiley)\$(print_elapsed) \e[30;1m\$(print_path)\e[0m\n\$ "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment