Skip to content
This repository has been archived by the owner on Apr 24, 2020. It is now read-only.

[Enhancement] Refactor command_execution_time segment #1215

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 13 additions & 22 deletions powerlevel9k.zsh-theme
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions test/segments/command_execution_time.spec
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand Down