add command time duration plugin (#1683)
parent
b119540ce7
commit
13e795c995
|
|
@ -74,6 +74,9 @@ if [[ ! -z "${BASH_IT_THEME}" ]]; then
|
||||||
BASH_IT_LOG_PREFIX="themes: p4helpers: "
|
BASH_IT_LOG_PREFIX="themes: p4helpers: "
|
||||||
# shellcheck source=./themes/p4helpers.theme.bash
|
# shellcheck source=./themes/p4helpers.theme.bash
|
||||||
source "${BASH_IT}/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: "
|
BASH_IT_LOG_PREFIX="themes: base: "
|
||||||
# shellcheck source=./themes/base.theme.bash
|
# shellcheck source=./themes/base.theme.bash
|
||||||
source "${BASH_IT}/themes/base.theme.bash"
|
source "${BASH_IT}/themes/base.theme.bash"
|
||||||
|
|
|
||||||
|
|
@ -38,6 +38,7 @@ Default theme glyphs
|
||||||
BARBUK_SVN_CHAR='⑆ '
|
BARBUK_SVN_CHAR='⑆ '
|
||||||
BARBUK_EXIT_CODE_ICON=' '
|
BARBUK_EXIT_CODE_ICON=' '
|
||||||
BARBUK_PYTHON_VENV_CHAR=' '
|
BARBUK_PYTHON_VENV_CHAR=' '
|
||||||
|
BARBUK_COMMAND_DURATION_ICON=' '
|
||||||
|
|
||||||
Customize glyphs
|
Customize glyphs
|
||||||
^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^
|
||||||
|
|
@ -92,6 +93,11 @@ Please refer to the following documentation for more information:
|
||||||
EOF
|
EOF
|
||||||
chmod 400 /etc/sudoers.d/keepenv
|
chmod 400 /etc/sudoers.d/keepenv
|
||||||
|
|
||||||
|
Command duration
|
||||||
|
----------------
|
||||||
|
|
||||||
|
See :ref:`Command duration <command_duration>`.
|
||||||
|
|
||||||
Examples
|
Examples
|
||||||
--------
|
--------
|
||||||
|
|
||||||
|
|
@ -122,3 +128,11 @@ Python venv
|
||||||
.. code-block:: bash
|
.. code-block:: bash
|
||||||
|
|
||||||
flask ~/test on master ✓ ❯
|
flask ~/test on master ✓ ❯
|
||||||
|
|
||||||
|
Command duration
|
||||||
|
^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
.. code-block:: bash
|
||||||
|
|
||||||
|
# sleep 3s
|
||||||
|
user@hostname in ~/bash-it on master ✓ 3.2s ❯
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,29 @@ See :ref:`here <list_of_themes>`.
|
||||||
Theme Switches & Variables
|
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
|
Clock Related
|
||||||
=============
|
=============
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -51,6 +51,13 @@ export SCM_CHECK=true
|
||||||
# Will otherwise fall back on $USER.
|
# Will otherwise fall back on $USER.
|
||||||
#export SHORT_USER=${USER:0:8}
|
#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.
|
# Set Xterm/screen/Tmux title with shortened command and directory.
|
||||||
# Uncomment this to set.
|
# Uncomment this to set.
|
||||||
#export SHORT_TERM_LINE=true
|
#export SHORT_TERM_LINE=true
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
../../docs/themes-list/barbuk.rst
|
||||||
|
|
@ -11,6 +11,11 @@ SCM_HG_CHAR=${BARBUK_HG_CHAR:='☿ '}
|
||||||
SCM_SVN_CHAR=${BARBUK_SVN_CHAR:='⑆ '}
|
SCM_SVN_CHAR=${BARBUK_SVN_CHAR:='⑆ '}
|
||||||
EXIT_CODE_ICON=${BARBUK_EXIT_CODE_ICON:=' '}
|
EXIT_CODE_ICON=${BARBUK_EXIT_CODE_ICON:=' '}
|
||||||
PYTHON_VENV_CHAR=${BARBUK_PYTHON_VENV_CHAR:=' '}
|
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 user and hostname display
|
||||||
SSH_INFO=${BARBUK_SSH_INFO:=true}
|
SSH_INFO=${BARBUK_SSH_INFO:=true}
|
||||||
|
|
@ -67,7 +72,9 @@ function _exit-code {
|
||||||
}
|
}
|
||||||
|
|
||||||
function _prompt {
|
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
|
_exit-code exit_code
|
||||||
_git-uptream-remote-logo
|
_git-uptream-remote-logo
|
||||||
|
|
@ -96,9 +103,8 @@ function _prompt {
|
||||||
python_venv="$PYTHON_VENV_CHAR$(basename "${VIRTUAL_ENV}") "
|
python_venv="$PYTHON_VENV_CHAR$(basename "${VIRTUAL_ENV}") "
|
||||||
fi
|
fi
|
||||||
|
|
||||||
PS1="\\n${ssh_info} ${purple}$(scm_char)${python_venv}${dir_color}\\w${normal}$(scm_prompt_info)${exit_code}"
|
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} -gt $((COLUMNS*3)) ]] && wrap_char="\\n"
|
|
||||||
PS1="${PS1}${wrap_char}❯${normal} "
|
PS1="${PS1}${wrap_char}❯${normal} "
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
Loading…
Reference in New Issue