diff --git a/bash_it.sh b/bash_it.sh index 310aae42..a7960c75 100755 --- a/bash_it.sh +++ b/bash_it.sh @@ -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" diff --git a/docs/themes-list/barbuk.rst b/docs/themes-list/barbuk.rst index 5fe49048..15fb0ef5 100644 --- a/docs/themes-list/barbuk.rst +++ b/docs/themes-list/barbuk.rst @@ -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 `. + 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 ❯ diff --git a/docs/themes.rst b/docs/themes.rst index aa21a7e3..5b796389 100644 --- a/docs/themes.rst +++ b/docs/themes.rst @@ -37,6 +37,29 @@ See :ref:`here `. 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 ============= diff --git a/template/bash_profile.template.bash b/template/bash_profile.template.bash index 4a840df3..75febdab 100755 --- a/template/bash_profile.template.bash +++ b/template/bash_profile.template.bash @@ -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 diff --git a/themes/barbuk/README.rst b/themes/barbuk/README.rst new file mode 120000 index 00000000..ca150a95 --- /dev/null +++ b/themes/barbuk/README.rst @@ -0,0 +1 @@ +../../docs/themes-list/barbuk.rst \ No newline at end of file diff --git a/themes/barbuk/barbuk.theme.bash b/themes/barbuk/barbuk.theme.bash index e1e7f2c1..ce4c3016 100644 --- a/themes/barbuk/barbuk.theme.bash +++ b/themes/barbuk/barbuk.theme.bash @@ -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} " } diff --git a/themes/command_duration.theme.bash b/themes/command_duration.theme.bash new file mode 100644 index 00000000..c721003e --- /dev/null +++ b/themes/command_duration.theme.bash @@ -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