diff options
-rw-r--r-- | .travis.yml | 1 | ||||
-rw-r--r-- | CHANGELOG.md | 5 | ||||
-rw-r--r-- | README.md | 6 | ||||
-rw-r--r-- | functions/icons.zsh | 3 | ||||
-rwxr-xr-x | powerlevel9k.zsh-theme | 51 | ||||
-rwxr-xr-x | test/segments/command_execution_time.spec | 96 |
6 files changed, 162 insertions, 0 deletions
diff --git a/.travis.yml b/.travis.yml index 4f2c7a39..ad4eb8c6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,6 +21,7 @@ script: - test/powerlevel9k.spec - test/functions/utilities.spec - test/functions/colors.spec + - test/segments/command_execution_time.spec - test/segments/dir.spec - test/segments/rust_version.spec - test/segments/go_version.spec diff --git a/CHANGELOG.md b/CHANGELOG.md index 5db1f6e5..4b27a514 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,11 @@ Added an option to configure the path separator. If you want something else than an ordinary slash, you could set `POWERLEVEL9K_DIR_PATH_SEPARATOR` to whatever you want. +### New segment 'command_execution_time' added + +Shows the duration a command needed to run. By default only durations over 3 seconds +are shown (can be adjusted by setting POWERLEVEL9K_COMMAND_EXECUTION_TIME_THRESHOLD). + ### New segment 'dir_writable' added This segment displays a lock icon if your user has no write permissions in the current folder. @@ -140,6 +140,7 @@ The segments that are currently available are: **Other:** * [`custom_command`](#custom_command) - Create a custom segment to display the output of an arbitrary command. +* [`command_execution_time`] - Display the time the current command took to execute. * [`todo`](http://todotxt.com/) - Shows the number of tasks in your todo.txt tasks file. * `detect_virt` - Virtualization detection with systemd @@ -200,6 +201,11 @@ Note that you can [modify the `_FOREGROUND` color](https://github.com/bhilburn/powerlevel9k/wiki/Stylizing-Your-Prompt#segment-color-customization) without affecting the icon color. +##### command_execution_time + +Display the time the current command took to execute if the time is above +`POWERLEVEL9K_COMMAND_EXECUTION_TIME_THRESHOLD` (defaults to 3 seconds). + ##### custom_command The `custom_...` segment allows you to turn the output of a custom command into diff --git a/functions/icons.zsh b/functions/icons.zsh index 3000f4a6..f0aec351 100644 --- a/functions/icons.zsh +++ b/functions/icons.zsh @@ -79,6 +79,7 @@ case $POWERLEVEL9K_MODE in SWIFT_ICON '' PUBLIC_IP_ICON '' LOCK_ICON $'\UE138' # + EXECUTION_TIME_ICON $'\UE89C' # SSH_ICON '(ssh)' ) ;; @@ -141,6 +142,7 @@ case $POWERLEVEL9K_MODE in SWIFT_ICON '' PUBLIC_IP_ICON '' LOCK_ICON $'\UE138' # + EXECUTION_TIME_ICON $'\uF253' SSH_ICON '(ssh)' ) ;; @@ -203,6 +205,7 @@ case $POWERLEVEL9K_MODE in SWIFT_ICON 'Swift' PUBLIC_IP_ICON '' LOCK_ICON $'\UE0A2' + EXECUTION_TIME_ICON 'Dur' SSH_ICON '(ssh)' ) ;; diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 5960dbe8..39454b59 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -578,6 +578,39 @@ prompt_custom() { fi } +# Display the duration the command needed to run. +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 + else + typeset -F ${POWERLEVEL9K_COMMAND_EXECUTION_TIME_PRECISION} humanReadableDuration + fi + humanReadableDuration=$_P9K_COMMAND_DURATION + fi + + if (( _P9K_COMMAND_DURATION >= POWERLEVEL9K_COMMAND_EXECUTION_TIME_THRESHOLD )); then + "$1_prompt_segment" "$0" "$2" "red" "226" "${humanReadableDuration}" 'EXECUTION_TIME_ICON' + fi +} + # Dir: current working directory set_default POWERLEVEL9K_DIR_PATH_SEPARATOR "/" prompt_dir() { @@ -1222,9 +1255,17 @@ build_right_prompt() { done } +powerlevel9k_preexec() { + _P9K_TIMER_START=$EPOCHREALTIME +} + powerlevel9k_prepare_prompts() { RETVAL=$? + _P9K_COMMAND_DURATION=$((EPOCHREALTIME - _P9K_TIMER_START)) + # Reset start time + _P9K_TIMER_START=99999999999 + if [[ "$POWERLEVEL9K_PROMPT_ON_NEWLINE" == true ]]; then PROMPT="$(print_icon 'MULTILINE_FIRST_PROMPT_PREFIX')%f%b%k$(build_left_prompt) $(print_icon 'MULTILINE_SECOND_PROMPT_PREFIX')" @@ -1253,6 +1294,9 @@ $(print_icon 'MULTILINE_SECOND_PROMPT_PREFIX')" } prompt_powerlevel9k_setup() { + # Disable false display of command execution time + _P9K_TIMER_START=99999999999 + # Display a warning if the terminal does not support 256 colors local term_colors term_colors=$(echotc Co) @@ -1297,11 +1341,18 @@ prompt_powerlevel9k_setup() { powerlevel9k_vcs_init fi + # initialize timing functions + zmodload zsh/datetime + + # Initialize math functions + zmodload zsh/mathfunc + # initialize hooks autoload -Uz add-zsh-hook # prepare prompts add-zsh-hook precmd powerlevel9k_prepare_prompts + add-zsh-hook preexec powerlevel9k_preexec } prompt_powerlevel9k_setup "$@" diff --git a/test/segments/command_execution_time.spec b/test/segments/command_execution_time.spec new file mode 100755 index 00000000..09738859 --- /dev/null +++ b/test/segments/command_execution_time.spec @@ -0,0 +1,96 @@ +#!/usr/bin/env zsh +#vim:ft=zsh ts=2 sw=2 sts=2 et fenc=utf-8 + +# Required for shunit2 to run correctly +setopt shwordsplit +SHUNIT_PARENT=$0 + +function setUp() { + export TERM="xterm-256color" + # Load Powerlevel9k + source powerlevel9k.zsh-theme +} + +function testCommandExecutionTimeIsNotShownIfTimeIsBelowThreshold() { + POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(custom_world command_execution_time) + POWERLEVEL9K_CUSTOM_WORLD='echo world' + _P9K_COMMAND_DURATION=2 + + assertEquals "%K{white} %F{black}world %k%F{white}%f " "$(build_left_prompt)" + + unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS + unset POWERLEVEL9K_CUSTOM_WORLD + unset _P9K_COMMAND_DURATION +} + +function testCommandExecutionTimeThresholdCouldBeChanged() { + POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(command_execution_time) + POWERLEVEL9K_COMMAND_EXECUTION_TIME_THRESHOLD=1 + _P9K_COMMAND_DURATION=2.03 + + assertEquals "%K{red} %F{226%}Dur%f %F{226}2.03 %k%F{red}%f " "$(build_left_prompt)" + + unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS + unset _P9K_COMMAND_DURATION + unset POWERLEVEL9K_COMMAND_EXECUTION_TIME_THRESHOLD +} + +function testCommandExecutionTimeThresholdCouldBeSetToZero() { + POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(command_execution_time) + POWERLEVEL9K_COMMAND_EXECUTION_TIME_THRESHOLD=0 + _P9K_COMMAND_DURATION=0.03 + + assertEquals "%K{red} %F{226%}Dur%f %F{226}0.03 %k%F{red}%f " "$(build_left_prompt)" + + unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS + unset _P9K_COMMAND_DURATION + unset POWERLEVEL9K_COMMAND_EXECUTION_TIME_THRESHOLD +} + +function testCommandExecutionTimePrecisionCouldBeChanged() { + POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(command_execution_time) + POWERLEVEL9K_COMMAND_EXECUTION_TIME_THRESHOLD=0 + POWERLEVEL9K_COMMAND_EXECUTION_TIME_PRECISION=4 + _P9K_COMMAND_DURATION=0.0001 + + assertEquals "%K{red} %F{226%}Dur%f %F{226}0.0001 %k%F{red}%f " "$(build_left_prompt)" + + unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS + unset _P9K_COMMAND_DURATION + unset POWERLEVEL9K_COMMAND_EXECUTION_TIME_PRECISION + unset POWERLEVEL9K_COMMAND_EXECUTION_TIME_THRESHOLD +} + +function testCommandExecutionTimePrecisionCouldBeSetToZero() { + POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(command_execution_time) + POWERLEVEL9K_COMMAND_EXECUTION_TIME_PRECISION=0 + _P9K_COMMAND_DURATION=23.5001 + + assertEquals "%K{red} %F{226%}Dur%f %F{226}23 %k%F{red}%f " "$(build_left_prompt)" + + unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS + unset _P9K_COMMAND_DURATION + unset POWERLEVEL9K_COMMAND_EXECUTION_TIME_PRECISION +} + +function testCommandExecutionTimeIsFormattedHumandReadbleForMinuteLongCommand() { + POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(command_execution_time) + _P9K_COMMAND_DURATION=180 + + assertEquals "%K{red} %F{226%}Dur%f %F{226}03:00 %k%F{red}%f " "$(build_left_prompt)" + + unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS + unset _P9K_COMMAND_DURATION +} + +function testCommandExecutionTimeIsFormattedHumandReadbleForHourLongCommand() { + POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(command_execution_time) + _P9K_COMMAND_DURATION=7200 + + assertEquals "%K{red} %F{226%}Dur%f %F{226}02:00:00 %k%F{red}%f " "$(build_left_prompt)" + + unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS + unset _P9K_COMMAND_DURATION +} + +source shunit2/source/2.1/src/shunit2
\ No newline at end of file |