Skip to content

Commit

Permalink
lib/command_duration
Browse files Browse the repository at this point in the history
Remove use of temporary files.
  • Loading branch information
gaelicWizard committed Jul 26, 2021
1 parent e13f7d1 commit a70f425
Showing 1 changed file with 11 additions and 19 deletions.
30 changes: 11 additions & 19 deletions themes/command_duration.theme.bash
Original file line number Diff line number Diff line change
@@ -1,41 +1,34 @@
# 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() {
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_START_TIME=

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"
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() {
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)
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
command_start=$(< "$COMMAND_DURATION_FILE")
command_start_sseconds=${command_start%.*}
current_time_seconds=${current_time%.*}
if [[ -n "${COMMAND_DURATION_START_TIME:-}" ]]; then
command_start_sseconds=${COMMAND_DURATION_START_TIME%.*}
current_time_seconds=${COMMAND_DURATION_START_TIME%.*}

command_start_deciseconds=$((10#${command_start#*.}))
current_time_deciseconds=$((10#${current_time#*.}))
Expand All @@ -49,7 +42,6 @@ _command_duration() {
((command_duration -= 1))
deciseconds=$((10 - ((command_start_deciseconds - current_time_deciseconds))))
fi
command rm "$COMMAND_DURATION_FILE"
else
command_duration=0
fi
Expand Down

0 comments on commit a70f425

Please sign in to comment.