Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
57 lines (46 sloc) 1.45 KB
# Example prompt:
#
# 23:11:27 | 3s | (1) | %0 | !5232
# user@host:/tmp
# $
_prompt_timer_start () { : ${_prompt_timer=$SECONDS}; }
_prompt_timer_stop () {
local runtime=$(($SECONDS - $_prompt_timer))
unset _prompt_timer
# Convert seconds to dhms.
local d=$(($runtime > 86400 ? ($runtime / 84600) : 0))
local h=$(($runtime > 3600 ? ($runtime / 3600 % 24) : 0))
local m=$(($runtime > 60 ? ($runtime / 60 % 60) : 0))
local s=$(($runtime % 60))
_RUNTIME_FORMATTED=
for part in d h m s; do
eval val=\$$part
[[ $val -gt 0 ]] && _RUNTIME_FORMATTED+=$val$part
done
: ${_RUNTIME_FORMATTED:=0s}
}
trap '_prompt_timer_start' DEBUG
PROMPT_COMMAND='_exit_status_color; '$PROMPT_COMMAND' _prompt_timer_stop;'
_exit_status_color () {
if [[ 0 -eq $? ]]; then
_EXIT_STATUS_FORMATTED=${ANSI_BOLD}${ANSI_GREEN}
else
_EXIT_STATUS_FORMATTED=${ANSI_BOLD}${ANSI_RED}
fi
}
if [[ -n $SSH_CLIENT ]]; then
_HOST_FORMATTED='${ANSI_YELLOW}\h${ANSI_RESET}'
else
_HOST_FORMATTED='\h'
fi
PS1='\[${ANSI_RESET}\]'
# Set the terminal title.
PS1=$PS1'\[\e]0;\w\a\]'
# TODO: if $SHLVL > 1, highlight it in a different color.
PS1=$PS1'\n\t | ${_RUNTIME_FORMATTED} | ('$((${SHLVL}-1))') | %\j | !\!'
PS1=$PS1'\n\u@'${_HOST_FORMATTED}':\[${ANSI_CYAN}\]\w\[${ANSI_RESET}\]'
PS1=$PS1'\n\[${_EXIT_STATUS_FORMATTED}\]\$\[${ANSI_RESET}\] '
export PS1
for v in RUNTIME EXIT_STATUS HOST; do
unset _${v}_FORMATTED
done