From 8a3278c696cab8ee279f1c66c4a40c37421be9d0 Mon Sep 17 00:00:00 2001 From: Chris Baker Date: Sat, 16 Mar 2019 21:49:47 -0400 Subject: [PATCH 1/4] simplify execution_time seconds formatting code and add s --- powerlevel9k.zsh-theme | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 4939cb354..932c24dc4 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -745,16 +745,8 @@ prompt_command_execution_time() { elif (( _P9K_COMMAND_DURATION > 60 )); then humanReadableDuration=$(TZ=GMT; strftime '%M:%S' $(( int(rint(_P9K_COMMAND_DURATION)) ))) else - # If the command executed in seconds, print as float. - # Convert to float - if [[ "${POWERLEVEL9K_COMMAND_EXECUTION_TIME_PRECISION}" == "0" ]]; then - # If user does not want microseconds, then we need to convert - # the duration to an integer. - typeset -i humanReadableDuration - else - typeset -F ${POWERLEVEL9K_COMMAND_EXECUTION_TIME_PRECISION} humanReadableDuration - fi - humanReadableDuration=$_P9K_COMMAND_DURATION + # If the command executed in seconds, round to desired precision and append "s" + humanReadableDuration=$(printf %.${POWERLEVEL9K_COMMAND_EXECUTION_TIME_PRECISION}f%s $_P9K_COMMAND_DURATION s) fi if (( _P9K_COMMAND_DURATION >= POWERLEVEL9K_COMMAND_EXECUTION_TIME_THRESHOLD )); then From 2d06c478031ce127962ee8766392843be59370d7 Mon Sep 17 00:00:00 2001 From: Chris Baker Date: Sat, 16 Mar 2019 21:52:14 -0400 Subject: [PATCH 2/4] skip the execution_time formatting work if nothing to display anyway Signed-off-by: Chris Baker --- powerlevel9k.zsh-theme | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 932c24dc4..a0ea1b63b 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -734,22 +734,21 @@ prompt_command_execution_time() { set_default POWERLEVEL9K_COMMAND_EXECUTION_TIME_THRESHOLD 3 set_default POWERLEVEL9K_COMMAND_EXECUTION_TIME_PRECISION 2 - # Print time in human readable format - # For that use `strftime` and convert - # the duration (float) to an seconds - # (integer). - # See http://unix.stackexchange.com/a/89748 - local humanReadableDuration - if (( _P9K_COMMAND_DURATION > 3600 )); then - humanReadableDuration=$(TZ=GMT; strftime '%H:%M:%S' $(( int(rint(_P9K_COMMAND_DURATION)) ))) - elif (( _P9K_COMMAND_DURATION > 60 )); then - humanReadableDuration=$(TZ=GMT; strftime '%M:%S' $(( int(rint(_P9K_COMMAND_DURATION)) ))) - else - # If the command executed in seconds, round to desired precision and append "s" - humanReadableDuration=$(printf %.${POWERLEVEL9K_COMMAND_EXECUTION_TIME_PRECISION}f%s $_P9K_COMMAND_DURATION s) - fi - if (( _P9K_COMMAND_DURATION >= POWERLEVEL9K_COMMAND_EXECUTION_TIME_THRESHOLD )); then + # Print time in human readable format + # For that use `strftime` and convert + # the duration (float) to an seconds + # (integer). + # See http://unix.stackexchange.com/a/89748 + local humanReadableDuration + if (( _P9K_COMMAND_DURATION > 3600 )); then + humanReadableDuration=$(TZ=GMT; strftime '%H:%M:%S' $(( int(rint(_P9K_COMMAND_DURATION)) ))) + elif (( _P9K_COMMAND_DURATION > 60 )); then + humanReadableDuration=$(TZ=GMT; strftime '%M:%S' $(( int(rint(_P9K_COMMAND_DURATION)) ))) + else + # If the command executed in seconds, round to desired precision and append "s" + humanReadableDuration=$(printf %.${POWERLEVEL9K_COMMAND_EXECUTION_TIME_PRECISION}f%s $_P9K_COMMAND_DURATION s) + fi "$1_prompt_segment" "$0" "$2" "red" "yellow1" "${humanReadableDuration}" 'EXECUTION_TIME_ICON' fi } From 66038a5537a4a5e755b66dcd58de914e5c285d17 Mon Sep 17 00:00:00 2001 From: Chris Baker Date: Sun, 17 Mar 2019 10:09:41 -0400 Subject: [PATCH 3/4] updates command_execution_time tests Signed-off-by: Chris Baker --- test/segments/command_execution_time.spec | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/segments/command_execution_time.spec b/test/segments/command_execution_time.spec index 6d2d7ad99..3c6cdfdbb 100755 --- a/test/segments/command_execution_time.spec +++ b/test/segments/command_execution_time.spec @@ -34,7 +34,7 @@ function testCommandExecutionTimeThresholdCouldBeChanged() { # Override payload local _P9K_COMMAND_DURATION=2.03 - assertEquals "%K{001} %F{226}Dur %F{226}2.03 %k%F{001}%f " "$(build_left_prompt)" + assertEquals "%K{001} %F{226}Dur %F{226}2.03s %k%F{001}%f " "$(build_left_prompt)" } function testCommandExecutionTimeThresholdCouldBeSetToZero() { @@ -43,7 +43,7 @@ function testCommandExecutionTimeThresholdCouldBeSetToZero() { local POWERLEVEL9K_COMMAND_EXECUTION_TIME_THRESHOLD=0 local _P9K_COMMAND_DURATION=0.03 - assertEquals "%K{001} %F{226}Dur %F{226}0.03 %k%F{001}%f " "$(build_left_prompt)" + assertEquals "%K{001} %F{226}Dur %F{226}0.03s %k%F{001}%f " "$(build_left_prompt)" } function testCommandExecutionTimePrecisionCouldBeChanged() { @@ -58,7 +58,7 @@ function testCommandExecutionTimePrecisionCouldBeChanged() { # Override payload local _P9K_COMMAND_DURATION=0.0001 - assertEquals "%K{001} %F{226}Dur %F{226}0.0001 %k%F{001}%f " "$(build_left_prompt)" + assertEquals "%K{001} %F{226}Dur %F{226}0.0001s %k%F{001}%f " "$(build_left_prompt)" } function testCommandExecutionTimePrecisionCouldBeSetToZero() { @@ -72,7 +72,7 @@ function testCommandExecutionTimePrecisionCouldBeSetToZero() { # Override payload local _P9K_COMMAND_DURATION=23.5001 - assertEquals "%K{001} %F{226}Dur %F{226}23 %k%F{001}%f " "$(build_left_prompt)" + assertEquals "%K{001} %F{226}Dur %F{226}23s %k%F{001}%f " "$(build_left_prompt)" } function testCommandExecutionTimeIsFormattedHumandReadbleForMinuteLongCommand() { From 700ad350bdbc141cfaa2ba5282e6342ebb994ab6 Mon Sep 17 00:00:00 2001 From: Chris Baker Date: Sun, 17 Mar 2019 17:27:20 -0400 Subject: [PATCH 4/4] fixes truncating vs rounding error in CET spec Signed-off-by: Chris Baker --- test/segments/command_execution_time.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/segments/command_execution_time.spec b/test/segments/command_execution_time.spec index 3c6cdfdbb..d7863ce90 100755 --- a/test/segments/command_execution_time.spec +++ b/test/segments/command_execution_time.spec @@ -72,7 +72,7 @@ function testCommandExecutionTimePrecisionCouldBeSetToZero() { # Override payload local _P9K_COMMAND_DURATION=23.5001 - assertEquals "%K{001} %F{226}Dur %F{226}23s %k%F{001}%f " "$(build_left_prompt)" + assertEquals "%K{001} %F{226}Dur %F{226}24s %k%F{001}%f " "$(build_left_prompt)" } function testCommandExecutionTimeIsFormattedHumandReadbleForMinuteLongCommand() {