From d99a94f34ee7bd0ea1c6a741a697a2af497d10ae Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Sat, 21 Dec 2013 23:33:32 +0100 Subject: [PATCH] Humanize the execution time. Fixes #46 --- pure.zsh | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/pure.zsh b/pure.zsh index 7a5eb23..acda5f6 100644 --- a/pure.zsh +++ b/pure.zsh @@ -16,6 +16,21 @@ # %m => shortname host # %(?..) => prompt conditional - %(condition.true.false) + +# turns seconds into human readable time +# 165392 => 1d 21h 56h 32s +prompt_pure_human_time() { + local tmp=$1 + local days=$(( tmp / 60 / 60 / 24 )) + local hours=$(( tmp / 60 / 60 % 24 )) + local minutes=$(( tmp / 60 % 60 )) + local seconds=$(( tmp % 60 )) + (( $days > 0 )) && echo -n "${days}d " + (( $hours > 0 )) && echo -n "${hours}h " + (( $minutes > 0 )) && echo -n "${minutes}h " + echo "${seconds}s" +} + # fastest possible way to check if repo is dirty prompt_pure_git_dirty() { # check if we're in a git repo @@ -31,7 +46,7 @@ prompt_pure_cmd_exec_time() { local stop=$(date +%s) local start=${cmd_timestamp:-$stop} integer elapsed=$stop-$start - (($elapsed > ${PURE_CMD_MAX_EXEC_TIME:=5})) && echo ${elapsed}s + (($elapsed > ${PURE_CMD_MAX_EXEC_TIME:=5})) && prompt_pure_human_time $elapsed } prompt_pure_preexec() {