Skip to content

Commit

Permalink
for elapsed time- display days, don't display parts with 0 value
Browse files Browse the repository at this point in the history
  • Loading branch information
gray committed Sep 15, 2012
1 parent f97fc77 commit cec6817
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions .bash_prompt
Expand Up @@ -4,22 +4,27 @@
# user@host:/tmp
# $

timer_start () { : ${timer=$SECONDS}; }
_prompt_timer_start () { : ${_prompt_timer=$SECONDS}; }

timer_stop () {
local _RUNTIME=$(($SECONDS - $timer))
unset timer
_prompt_timer_stop () {
local runtime=$(($SECONDS - $_prompt_timer))
unset _prompt_timer

# Convert seconds to hms.
# 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=
[[ $_RUNTIME -gt 3600 ]] && _RUNTIME_FORMATTED+=$(($_RUNTIME / 3600))h
[[ $_RUNTIME -gt 60 ]] && _RUNTIME_FORMATTED+=$(($_RUNTIME % 3600 / 60))m
[[ $_RUNTIME -gt 0 ]] && _RUNTIME_FORMATTED+=$(($_RUNTIME % 60))s
for part in d h m s; do
eval val=\$$part
[[ $val -gt 0 ]] && _RUNTIME_FORMATTED+=$val$part
done
: ${_RUNTIME_FORMATTED:=0s}
}

trap 'timer_start' DEBUG
PROMPT_COMMAND='_exit_status_color; '$PROMPT_COMMAND' timer_stop;'
trap '_prompt_timer_start' DEBUG
PROMPT_COMMAND='_exit_status_color; '$PROMPT_COMMAND' _prompt_timer_stop;'

_exit_status_color () {
if [[ 0 -eq $? ]]; then
Expand Down

0 comments on commit cec6817

Please sign in to comment.