add command time duration plugin (#1683)

pull/1757/head
BarbUk 2020-12-27 17:48:17 +01:00 committed by GitHub
parent b119540ce7
commit 13e795c995
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 131 additions and 4 deletions

View File

@ -74,6 +74,9 @@ if [[ ! -z "${BASH_IT_THEME}" ]]; then
BASH_IT_LOG_PREFIX="themes: p4helpers: "
# shellcheck source=./themes/p4helpers.theme.bash
source "${BASH_IT}/themes/p4helpers.theme.bash"
BASH_IT_LOG_PREFIX="themes: command_duration: "
# shellcheck source=./themes/command_duration.theme.bash
source "${BASH_IT}/themes/command_duration.theme.bash"
BASH_IT_LOG_PREFIX="themes: base: "
# shellcheck source=./themes/base.theme.bash
source "${BASH_IT}/themes/base.theme.bash"

View File

@ -38,6 +38,7 @@ Default theme glyphs
BARBUK_SVN_CHAR='⑆ '
BARBUK_EXIT_CODE_ICON=' '
BARBUK_PYTHON_VENV_CHAR=' '
BARBUK_COMMAND_DURATION_ICON='  '
Customize glyphs
^^^^^^^^^^^^^^^^
@ -92,6 +93,11 @@ Please refer to the following documentation for more information:
EOF
chmod 400 /etc/sudoers.d/keepenv
Command duration
----------------
See :ref:`Command duration <command_duration>`.
Examples
--------
@ -122,3 +128,11 @@ Python venv
.. code-block:: bash
  flask ~/test on  master ✓
Command duration
^^^^^^^^^^^^^^^^
.. code-block:: bash
# sleep 3s
user@hostname in  ~/bash-it on  master ✓  3.2s

View File

@ -37,6 +37,29 @@ See :ref:`here <list_of_themes>`.
Theme Switches & Variables
^^^^^^^^^^^^^^^^^^^^^^^^^^
.. _command_duration:
Command duration
================
Prints last command duration
Usage
#####
Command duration can be enabled by exporting ``BASH_IT_COMMAND_DURATION``:
.. code-block:: bash
export BASH_IT_COMMAND_DURATION=true
The default configuration display last command duration for command lasting one second or more.
You can customize the minimum time in seconds before command duration is displayed in your ``.bashrc``:
.. code-block:: bash
export COMMAND_DURATION_MIN_SECONDS=5
Clock Related
=============

View File

@ -51,6 +51,13 @@ export SCM_CHECK=true
# Will otherwise fall back on $USER.
#export SHORT_USER=${USER:0:8}
# If your theme use command duration, uncomment this to
# enable display of last command duration.
#export BASH_IT_COMMAND_DURATION=true
# You can choose the minimum time in seconds before
# command duration is displayed.
#export COMMAND_DURATION_MIN_SECONDS=1
# Set Xterm/screen/Tmux title with shortened command and directory.
# Uncomment this to set.
#export SHORT_TERM_LINE=true

View File

@ -0,0 +1 @@
../../docs/themes-list/barbuk.rst

View File

@ -11,6 +11,11 @@ SCM_HG_CHAR=${BARBUK_HG_CHAR:='☿ '}
SCM_SVN_CHAR=${BARBUK_SVN_CHAR:='⑆ '}
EXIT_CODE_ICON=${BARBUK_EXIT_CODE_ICON:=' '}
PYTHON_VENV_CHAR=${BARBUK_PYTHON_VENV_CHAR:=' '}
COMMAND_DURATION_ICON=${BARBUK_COMMAND_DURATION_ICON:-"$bold_blue"}
# Command duration
COMMAND_DURATION_MIN_SECONDS=${COMMAND_DURATION_MIN_SECONDS:-1}
COMMAND_DURATION_COLOR="$normal"
# Ssh user and hostname display
SSH_INFO=${BARBUK_SSH_INFO:=true}
@ -67,7 +72,9 @@ function _exit-code {
}
function _prompt {
local exit_code="$?" wrap_char=' ' dir_color=$green ssh_info='' python_venv='' host
local exit_code="$?" wrap_char=' ' dir_color=$green ssh_info='' python_venv='' host command_duration=
command_duration=$(_command_duration)
_exit-code exit_code
_git-uptream-remote-logo
@ -96,9 +103,8 @@ function _prompt {
python_venv="$PYTHON_VENV_CHAR$(basename "${VIRTUAL_ENV}") "
fi
PS1="\\n${ssh_info} ${purple}$(scm_char)${python_venv}${dir_color}\\w${normal}$(scm_prompt_info)${exit_code}"
[[ ${#PS1} -gt $((COLUMNS*3)) ]] && wrap_char="\\n"
PS1="\\n${ssh_info} ${purple}$(scm_char)${python_venv}${dir_color}\\w${normal}$(scm_prompt_info)${command_duration}${exit_code}"
[[ ${#PS1} -gt $((COLUMNS*2)) ]] && wrap_char="\\n"
PS1="${PS1}${wrap_char}${normal} "
}

View File

@ -0,0 +1,73 @@
#!/usr/bin/env bash
if [ -z "$BASH_IT_COMMAND_DURATION" ] || [ "$BASH_IT_COMMAND_DURATION" != true ]; then
_command_duration() {
echo -n
}
return
fi
# Define tmp dir and file
COMMAND_DURATION_TMPDIR="${TMPDIR:-/tmp}"
COMMAND_DURATION_FILE="${COMMAND_DURATION_FILE:-$COMMAND_DURATION_TMPDIR/bashit_theme_execution_$BASHPID}"
COMMAND_DURATION_ICON=${COMMAND_DURATION_ICON:-'  '}
COMMAND_DURATION_MIN_SECONDS=${COMMAND_DURATION_MIN_SECONDS:-'1'}
trap _command_duration_delete_temp_file EXIT HUP INT TERM
_command_duration_delete_temp_file() {
if [[ -f "$COMMAND_DURATION_FILE" ]]; then
rm -f "$COMMAND_DURATION_FILE"
fi
}
_command_duration_pre_exec() {
date +%s.%1N > "$COMMAND_DURATION_FILE"
}
_command_duration() {
local command_duration command_start current_time
local minutes seconds deciseconds
local command_start_sseconds current_time_seconds command_start_deciseconds current_time_deciseconds
current_time=$(date +%s.%1N)
if [[ -f "$COMMAND_DURATION_FILE" ]]; then
command_start=$(< "$COMMAND_DURATION_FILE")
command_start_sseconds=${command_start%.*}
current_time_seconds=${current_time%.*}
command_start_deciseconds=$((10#${command_start#*.}))
current_time_deciseconds=$((10#${current_time#*.}))
# seconds
command_duration=$(( current_time_seconds - command_start_sseconds ))
if (( current_time_deciseconds >= command_start_deciseconds )); then
deciseconds=$(( (current_time_deciseconds - command_start_deciseconds) ))
else
((command_duration-=1))
deciseconds=$(( 10 - ( (command_start_deciseconds - current_time_deciseconds) ) ))
fi
command rm "$COMMAND_DURATION_FILE"
else
command_duration=0
fi
if (( command_duration > 0 )); then
minutes=$(( command_duration / 60 ))
seconds=$(( command_duration % 60 ))
fi
if (( minutes > 0 )); then
printf "%s%s%dm %ds" "$COMMAND_DURATION_ICON" "$COMMAND_DURATION_COLOR" "$minutes" "$seconds"
elif (( seconds >= COMMAND_DURATION_MIN_SECONDS )); then
printf "%s%s%d.%01ds" "$COMMAND_DURATION_ICON" "$COMMAND_DURATION_COLOR" "$seconds" "$deciseconds"
fi
}
preexec() (
_command_duration_pre_exec
)
preexec_install