lib/command_duration: remove temporary files

pull/1906/head
John D Pell 2021-07-26 14:26:38 -07:00
parent ca8101b34a
commit ad1d73aaa1
1 changed files with 13 additions and 18 deletions

View File

@ -1,40 +1,34 @@
# shellcheck shell=bash # shellcheck shell=bash
if [ -z "$BASH_IT_COMMAND_DURATION" ] || [ "$BASH_IT_COMMAND_DURATION" != true ]; then if [[ "${BASH_IT_COMMAND_DURATION:-false}" != true ]]; then
_command_duration() { _command_duration() {
echo -n echo -n
} }
return return
fi fi
# Define tmp dir and file COMMAND_DURATION_START_TIME=
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_ICON=${COMMAND_DURATION_ICON:-'  '}
COMMAND_DURATION_MIN_SECONDS=${COMMAND_DURATION_MIN_SECONDS:-'1'} 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() { _command_duration_pre_exec() {
date +%s.%1N > "$COMMAND_DURATION_FILE" local command_nano_now="$(date +%1N)"
[[ "$command_nano_now" == "1N" ]] && command_nano_now=1
COMMAND_DURATION_START_TIME="$(date "+%s").${command_nano_now}"
} }
_command_duration() { _command_duration() {
local command_duration command_start current_time local command_duration command_start current_time
local minutes seconds deciseconds local minutes seconds deciseconds
local command_start_sseconds current_time_seconds command_start_deciseconds current_time_deciseconds local command_start_sseconds current_time_seconds command_start_deciseconds current_time_deciseconds
current_time=$(date +%s.%1N) local command_nano_now="$(date +%1N)"
[[ "$command_nano_now" == "1N" ]] && command_nano_now=1
current_time="$(date "+%s").${command_nano_now}"
if [[ -f "$COMMAND_DURATION_FILE" ]]; then if [[ -n "${COMMAND_DURATION_START_TIME:-}" ]]; then
command_start=$(< "$COMMAND_DURATION_FILE") _bash_it_log_section="command_duration" _log_debug "calculating start time"
command_start_sseconds=${command_start%.*} command_start_sseconds=${COMMAND_DURATION_START_TIME%.*}
current_time_seconds=${current_time%.*} current_time_seconds=${current_time%.*}
command_start_deciseconds=$((10#${command_start#*.})) command_start_deciseconds=$((10#${command_start#*.}))
@ -42,6 +36,7 @@ _command_duration() {
# seconds # seconds
command_duration=$((current_time_seconds - command_start_sseconds)) command_duration=$((current_time_seconds - command_start_sseconds))
_bash_it_log_section="command_duration" _log_debug "duration: $command_duration (from $COMMAND_DURATION_START_TIME to $current_time)"
if ((current_time_deciseconds >= command_start_deciseconds)); then if ((current_time_deciseconds >= command_start_deciseconds)); then
deciseconds=$(((current_time_deciseconds - command_start_deciseconds))) deciseconds=$(((current_time_deciseconds - command_start_deciseconds)))
@ -49,12 +44,12 @@ _command_duration() {
((command_duration -= 1)) ((command_duration -= 1))
deciseconds=$((10 - ((command_start_deciseconds - current_time_deciseconds)))) deciseconds=$((10 - ((command_start_deciseconds - current_time_deciseconds))))
fi fi
command rm "$COMMAND_DURATION_FILE"
else else
command_duration=0 command_duration=0
fi fi
if ((command_duration > 0)); then if ((command_duration > 0)); then
_bash_it_log_section="command_duration" _log_debug "calculating minutes and seconds"
minutes=$((command_duration / 60)) minutes=$((command_duration / 60))
seconds=$((command_duration % 60)) seconds=$((command_duration % 60))
fi fi