Merge pull request #1214 from ChrisBaker97/next-refactor-command_execution_time

[Enhancement] Refactor command_execution_time.p9k segment
This commit is contained in:
Dominik Ritter 2019-03-29 08:40:03 +01:00 committed by GitHub
commit f7546cbd13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 27 deletions

View File

@ -30,30 +30,21 @@
# $3 boolean Whether the segment should be joined
##
prompt_command_execution_time() {
# 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 [[ "${P9K_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 ${P9K_COMMAND_EXECUTION_TIME_PRECISION} humanReadableDuration
fi
humanReadableDuration=$_P9K_COMMAND_DURATION
fi
if (( _P9K_COMMAND_DURATION >= P9K_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 %.${P9K_COMMAND_EXECUTION_TIME_PRECISION}f%s $_P9K_COMMAND_DURATION s)
fi
p9k::prepare_segment "$0" "" $1 "$2" $3 "${humanReadableDuration}"
fi
}

View File

@ -33,7 +33,7 @@ function testCommandExecutionTimeThresholdCouldBeChanged() {
local P9K_COMMAND_EXECUTION_TIME_THRESHOLD=1
local _P9K_COMMAND_DURATION=2.03
assertEquals "%K{001} %F{226}Dur %F{226}2.03 %k%F{001}%f " "$(__p9k_build_left_prompt)"
assertEquals "%K{001} %F{226}Dur %F{226}2.03s %k%F{001}%f " "$(__p9k_build_left_prompt)"
}
function testCommandExecutionTimeThresholdCouldBeSetToZero() {
@ -42,7 +42,7 @@ function testCommandExecutionTimeThresholdCouldBeSetToZero() {
local P9K_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 " "$(__p9k_build_left_prompt)"
assertEquals "%K{001} %F{226}Dur %F{226}0.03s %k%F{001}%f " "$(__p9k_build_left_prompt)"
}
function testCommandExecutionTimePrecisionCouldBeChanged() {
@ -52,7 +52,7 @@ function testCommandExecutionTimePrecisionCouldBeChanged() {
local P9K_COMMAND_EXECUTION_TIME_PRECISION=4
local _P9K_COMMAND_DURATION=0.0001
assertEquals "%K{001} %F{226}Dur %F{226}0.0001 %k%F{001}%f " "$(__p9k_build_left_prompt)"
assertEquals "%K{001} %F{226}Dur %F{226}0.0001s %k%F{001}%f " "$(__p9k_build_left_prompt)"
}
function testCommandExecutionTimePrecisionCouldBeSetToZero() {
@ -61,7 +61,7 @@ function testCommandExecutionTimePrecisionCouldBeSetToZero() {
local P9K_COMMAND_EXECUTION_TIME_PRECISION=0
local _P9K_COMMAND_DURATION=23.5001
assertEquals "%K{001} %F{226}Dur %F{226}23 %k%F{001}%f " "$(__p9k_build_left_prompt)"
assertEquals "%K{001} %F{226}Dur %F{226}24s %k%F{001}%f " "$(__p9k_build_left_prompt)"
}
function testCommandExecutionTimeIsFormattedHumandReadbleForMinuteLongCommand() {