12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- #! /usr/bin/env bash
-
- export __BASH_HOOKS_TIMEOUT__=60
-
- export __BASH_HOOKS_COMMAND__=
- export __BASH_HOOKS_TIME__=
-
- timestamp()
- {
- date "+%s"
- }
-
- preexec()
- {
- export __BASH_HOOKS_COMMAND__="$1"
- export __BASH_HOOKS_TIME__=$(timestamp)
-
- }
-
- precmd()
- {
- if [ -n "$__BASH_HOOKS_COMMAND__" ]
- then
- ts=$(timestamp)
- diff=$((ts - $__BASH_HOOKS_TIME__))
- __bash_hooks_command_finished "$__BASH_HOOKS_COMMAND__" $diff
- export __BASH_HOOKS_COMMAND__=
- export __BASH_HOOKS_TIME__=
- fi
- }
-
- displaytime()
- {
- local T=$1
- local D=$((T/60/60/24))
- local H=$((T/60/60%24))
- local M=$((T/60%60))
- local S=$((T%60))
- [[ $D > 0 ]] && printf '%d days ' $D
- [[ $H > 0 ]] && printf '%d hours ' $H
- [[ $M > 0 ]] && printf '%d minutes ' $M
- [[ $D > 0 || $H > 0 || $M > 0 ]] && printf 'and '
- printf '%d seconds\n' $S
- }
-
- __bash_hooks_command_finished()
- {
- if [ "$2" -ge "$__BASH_HOOKS_TIMEOUT__" ]
- then
- human=$(displaytime "$2")
- text="Command \"$1\" finished in $human ($2 seconds)"
- echo "$text"
- (nma $(hostname -f) "$1" "$text" >/dev/null 2>/dev/null &)
- fi
- }
|