Skip to content

Commit 13e795c

Browse files
authored
add command time duration plugin (#1683)
1 parent b119540 commit 13e795c

File tree

7 files changed

+131
-4
lines changed

7 files changed

+131
-4
lines changed

bash_it.sh

+3
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ if [[ ! -z "${BASH_IT_THEME}" ]]; then
7474
BASH_IT_LOG_PREFIX="themes: p4helpers: "
7575
# shellcheck source=./themes/p4helpers.theme.bash
7676
source "${BASH_IT}/themes/p4helpers.theme.bash"
77+
BASH_IT_LOG_PREFIX="themes: command_duration: "
78+
# shellcheck source=./themes/command_duration.theme.bash
79+
source "${BASH_IT}/themes/command_duration.theme.bash"
7780
BASH_IT_LOG_PREFIX="themes: base: "
7881
# shellcheck source=./themes/base.theme.bash
7982
source "${BASH_IT}/themes/base.theme.bash"

docs/themes-list/barbuk.rst

+14
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Default theme glyphs
3838
BARBUK_SVN_CHAR=''
3939
BARBUK_EXIT_CODE_ICON=''
4040
BARBUK_PYTHON_VENV_CHAR=''
41+
BARBUK_COMMAND_DURATION_ICON=''
4142
4243
Customize glyphs
4344
^^^^^^^^^^^^^^^^
@@ -92,6 +93,11 @@ Please refer to the following documentation for more information:
9293
EOF
9394
chmod 400 /etc/sudoers.d/keepenv
9495
96+
Command duration
97+
----------------
98+
99+
See :ref:`Command duration <command_duration>`.
100+
95101
Examples
96102
--------
97103
@@ -122,3 +128,11 @@ Python venv
122128
.. code-block:: bash
123129
124130
  flask ~/test on  master ✓ ❯
131+
132+
Command duration
133+
^^^^^^^^^^^^^^^^
134+
135+
.. code-block:: bash
136+
137+
# sleep 3s
138+
user@hostname in  ~/bash-it on  master ✓  3.2s ❯

docs/themes.rst

+23
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,29 @@ See :ref:`here <list_of_themes>`.
3737
Theme Switches & Variables
3838
^^^^^^^^^^^^^^^^^^^^^^^^^^
3939

40+
.. _command_duration:
41+
42+
Command duration
43+
================
44+
45+
Prints last command duration
46+
47+
Usage
48+
#####
49+
50+
Command duration can be enabled by exporting ``BASH_IT_COMMAND_DURATION``:
51+
52+
.. code-block:: bash
53+
54+
export BASH_IT_COMMAND_DURATION=true
55+
56+
The default configuration display last command duration for command lasting one second or more.
57+
You can customize the minimum time in seconds before command duration is displayed in your ``.bashrc``:
58+
59+
.. code-block:: bash
60+
61+
export COMMAND_DURATION_MIN_SECONDS=5
62+
4063
Clock Related
4164
=============
4265

template/bash_profile.template.bash

+7
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ export SCM_CHECK=true
5151
# Will otherwise fall back on $USER.
5252
#export SHORT_USER=${USER:0:8}
5353

54+
# If your theme use command duration, uncomment this to
55+
# enable display of last command duration.
56+
#export BASH_IT_COMMAND_DURATION=true
57+
# You can choose the minimum time in seconds before
58+
# command duration is displayed.
59+
#export COMMAND_DURATION_MIN_SECONDS=1
60+
5461
# Set Xterm/screen/Tmux title with shortened command and directory.
5562
# Uncomment this to set.
5663
#export SHORT_TERM_LINE=true

themes/barbuk/README.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../docs/themes-list/barbuk.rst

themes/barbuk/barbuk.theme.bash

+10-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ SCM_HG_CHAR=${BARBUK_HG_CHAR:='☿ '}
1111
SCM_SVN_CHAR=${BARBUK_SVN_CHAR:=''}
1212
EXIT_CODE_ICON=${BARBUK_EXIT_CODE_ICON:=''}
1313
PYTHON_VENV_CHAR=${BARBUK_PYTHON_VENV_CHAR:=''}
14+
COMMAND_DURATION_ICON=${BARBUK_COMMAND_DURATION_ICON:-"$bold_blue"}
15+
16+
# Command duration
17+
COMMAND_DURATION_MIN_SECONDS=${COMMAND_DURATION_MIN_SECONDS:-1}
18+
COMMAND_DURATION_COLOR="$normal"
1419

1520
# Ssh user and hostname display
1621
SSH_INFO=${BARBUK_SSH_INFO:=true}
@@ -67,7 +72,9 @@ function _exit-code {
6772
}
6873

6974
function _prompt {
70-
local exit_code="$?" wrap_char=' ' dir_color=$green ssh_info='' python_venv='' host
75+
local exit_code="$?" wrap_char=' ' dir_color=$green ssh_info='' python_venv='' host command_duration=
76+
77+
command_duration=$(_command_duration)
7178

7279
_exit-code exit_code
7380
_git-uptream-remote-logo
@@ -96,9 +103,8 @@ function _prompt {
96103
python_venv="$PYTHON_VENV_CHAR$(basename "${VIRTUAL_ENV}") "
97104
fi
98105

99-
PS1="\\n${ssh_info} ${purple}$(scm_char)${python_venv}${dir_color}\\w${normal}$(scm_prompt_info)${exit_code}"
100-
101-
[[ ${#PS1} -gt $((COLUMNS*3)) ]] && wrap_char="\\n"
106+
PS1="\\n${ssh_info} ${purple}$(scm_char)${python_venv}${dir_color}\\w${normal}$(scm_prompt_info)${command_duration}${exit_code}"
107+
[[ ${#PS1} -gt $((COLUMNS*2)) ]] && wrap_char="\\n"
102108
PS1="${PS1}${wrap_char}${normal} "
103109
}
104110

themes/command_duration.theme.bash

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env bash
2+
3+
if [ -z "$BASH_IT_COMMAND_DURATION" ] || [ "$BASH_IT_COMMAND_DURATION" != true ]; then
4+
_command_duration() {
5+
echo -n
6+
}
7+
return
8+
fi
9+
10+
# Define tmp dir and file
11+
COMMAND_DURATION_TMPDIR="${TMPDIR:-/tmp}"
12+
COMMAND_DURATION_FILE="${COMMAND_DURATION_FILE:-$COMMAND_DURATION_TMPDIR/bashit_theme_execution_$BASHPID}"
13+
14+
COMMAND_DURATION_ICON=${COMMAND_DURATION_ICON:-''}
15+
COMMAND_DURATION_MIN_SECONDS=${COMMAND_DURATION_MIN_SECONDS:-'1'}
16+
17+
trap _command_duration_delete_temp_file EXIT HUP INT TERM
18+
19+
_command_duration_delete_temp_file() {
20+
if [[ -f "$COMMAND_DURATION_FILE" ]]; then
21+
rm -f "$COMMAND_DURATION_FILE"
22+
fi
23+
}
24+
25+
_command_duration_pre_exec() {
26+
date +%s.%1N > "$COMMAND_DURATION_FILE"
27+
}
28+
29+
_command_duration() {
30+
local command_duration command_start current_time
31+
local minutes seconds deciseconds
32+
local command_start_sseconds current_time_seconds command_start_deciseconds current_time_deciseconds
33+
current_time=$(date +%s.%1N)
34+
35+
if [[ -f "$COMMAND_DURATION_FILE" ]]; then
36+
command_start=$(< "$COMMAND_DURATION_FILE")
37+
command_start_sseconds=${command_start%.*}
38+
current_time_seconds=${current_time%.*}
39+
40+
command_start_deciseconds=$((10#${command_start#*.}))
41+
current_time_deciseconds=$((10#${current_time#*.}))
42+
43+
# seconds
44+
command_duration=$(( current_time_seconds - command_start_sseconds ))
45+
46+
if (( current_time_deciseconds >= command_start_deciseconds )); then
47+
deciseconds=$(( (current_time_deciseconds - command_start_deciseconds) ))
48+
else
49+
((command_duration-=1))
50+
deciseconds=$(( 10 - ( (command_start_deciseconds - current_time_deciseconds) ) ))
51+
fi
52+
command rm "$COMMAND_DURATION_FILE"
53+
else
54+
command_duration=0
55+
fi
56+
57+
if (( command_duration > 0 )); then
58+
minutes=$(( command_duration / 60 ))
59+
seconds=$(( command_duration % 60 ))
60+
fi
61+
62+
if (( minutes > 0 )); then
63+
printf "%s%s%dm %ds" "$COMMAND_DURATION_ICON" "$COMMAND_DURATION_COLOR" "$minutes" "$seconds"
64+
elif (( seconds >= COMMAND_DURATION_MIN_SECONDS )); then
65+
printf "%s%s%d.%01ds" "$COMMAND_DURATION_ICON" "$COMMAND_DURATION_COLOR" "$seconds" "$deciseconds"
66+
fi
67+
}
68+
69+
preexec() (
70+
_command_duration_pre_exec
71+
)
72+
73+
preexec_install

0 commit comments

Comments
 (0)