-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1906 from gaelicWizard/command_duration
Revamp command duration helper/plugin
- Loading branch information
Showing
7 changed files
with
93 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# shellcheck shell=bash | ||
# | ||
# Functions for measuring and reporting how long a command takes to run. | ||
|
||
: "${COMMAND_DURATION_START_SECONDS:=${EPOCHREALTIME:-$SECONDS}}" | ||
: "${COMMAND_DURATION_ICON:=🕘}" | ||
: "${COMMAND_DURATION_MIN_SECONDS:=1}" | ||
|
||
function _command_duration_pre_exec() { | ||
COMMAND_DURATION_START_SECONDS="${EPOCHREALTIME:-$SECONDS}" | ||
} | ||
|
||
function _dynamic_clock_icon { | ||
local -i clock_hand=$(((${1:-${SECONDS}} % 12) + 90)) | ||
printf -v 'COMMAND_DURATION_ICON' '%b' "\xf0\x9f\x95\x$clock_hand" | ||
} | ||
|
||
function _command_duration() { | ||
[[ -n "${BASH_IT_COMMAND_DURATION:-}" ]] || return | ||
|
||
local command_duration=0 command_start="${COMMAND_DURATION_START_SECONDS:-0}" | ||
local -i minutes=0 seconds=0 deciseconds=0 | ||
local -i command_start_seconds="${command_start%.*}" | ||
local -i command_start_deciseconds=$((10#${command_start##*.})) | ||
local current_time="${EPOCHREALTIME:-$SECONDS}" | ||
local -i current_time_seconds="${current_time%.*}" | ||
local -i current_time_deciseconds="$((10#${current_time##*.}))" | ||
|
||
if [[ "${command_start_seconds:-0}" -gt 0 ]]; then | ||
# seconds | ||
command_duration="$((current_time_seconds - command_start_seconds))" | ||
|
||
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 | ||
else | ||
command_duration=0 | ||
fi | ||
|
||
if ((command_duration > 0)); then | ||
minutes=$((command_duration / 60)) | ||
seconds=$((command_duration % 60)) | ||
fi | ||
|
||
_dynamic_clock_icon "${command_duration}" | ||
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 | ||
} | ||
|
||
_bash_it_library_finalize_hook+=("safe_append_preexec '_command_duration_pre_exec'") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.