diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 4939cb354..a0ea1b63b 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -734,30 +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, 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 + 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 - typeset -F ${POWERLEVEL9K_COMMAND_EXECUTION_TIME_PRECISION} humanReadableDuration + # 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 - humanReadableDuration=$_P9K_COMMAND_DURATION - fi - - if (( _P9K_COMMAND_DURATION >= POWERLEVEL9K_COMMAND_EXECUTION_TIME_THRESHOLD )); then "$1_prompt_segment" "$0" "$2" "red" "yellow1" "${humanReadableDuration}" 'EXECUTION_TIME_ICON' fi } diff --git a/test/segments/command_execution_time.spec b/test/segments/command_execution_time.spec index 6d2d7ad99..d7863ce90 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}24s %k%F{001}%f " "$(build_left_prompt)" } function testCommandExecutionTimeIsFormattedHumandReadbleForMinuteLongCommand() {