zsh: general: output program runtime on exit if greater 60

This commit is contained in:
Thomas Preisner 2017-07-11 01:45:11 +02:00
parent 8e51223d86
commit a4ad32fe10
2 changed files with 81 additions and 0 deletions

80
zsh/general.zsh Normal file
View file

@ -0,0 +1,80 @@
# ===== helper functions
# Return the name of the program which is called in the foreground with `fg`.
# $1 is the name of the program (optional). If it's not 'fg' or 'fg *' it's
# returned unchanged.
zshrc_resolve_fg_to_resumed_job_name() {
# $REPLY is used by convention for scalars ($reply for arrays) to return
# values from functions. unset it here to prevent problems when REPLY is
# bound to an integer or similar. Thanks to Mikachu in #zsh on Freenode
# (2012-09-27 17:14 CEST) for this hint.
unset REPLY
# Replace fg with the resumed job name.
if [[ $1 == fg ]]; then
REPLY=${jobtexts[%+]}
elif [[ $1 == fg\ * ]]; then
REPLY=${jobtexts[${1#fg }]}
# Normal program, return as is.
else
REPLY=$1
fi
}
# ===== runtime display
# necessary for $EPOCHSECONDS, the UNIX time.
zmodload zsh/datetime
typeset -a zshrc_longrun_data
zshrc_longrun_data=()
# Display runtime in seconds for long running programs (> 60 seconds) and send
# a bell to notify me.
zshrc_longrun_preexec() {
local program=$3
# Handle fg.
local REPLY
zshrc_resolve_fg_to_resumed_job_name $program
program=$REPLY
# No background process found.
if [[ -z $program ]]; then
return
fi
# Don't track the time for certain (possible) long running processes which
# need no automatic notification.
local ignore
for ignore in elinks man mutt vim ssh; do
case $program in
$ignore | $ignore\ *)
zshrc_longrun_data=()
return
;;
esac
done
zshrc_longrun_data=($program $EPOCHSECONDS)
}
zshrc_longrun_precmd() {
# No previous timestamp available or disabled for this command, ignore.
if [[ -z $zshrc_longrun_data ]]; then
return
fi
local difference=$(( EPOCHSECONDS - zshrc_longrun_data[2] ))
if [[ $difference -gt 60 ]]; then
echo
echo -n "${fg[yellow]}"
echo -n "~> ${(V)zshrc_longrun_data[1]} took $difference seconds."
echo -n "${fg[default]}"
echo "\a" # send bell
fi
# Clear status. Prevents displaying old status information when pressing
# enter with an empty command line.
zshrc_longrun_data=()
}
preexec_functions+=(zshrc_longrun_preexec)
precmd_functions+=(zshrc_longrun_precmd)

1
zshrc
View file

@ -1,5 +1,6 @@
# Loading keybindings first because activating Vi-bindings resets all keybinds. # Loading keybindings first because activating Vi-bindings resets all keybinds.
source ~/.zsh/keybindings.zsh source ~/.zsh/keybindings.zsh
source ~/.zsh/general.zsh
source ~/.zsh/setopt.zsh source ~/.zsh/setopt.zsh
source ~/.zsh/checks.zsh source ~/.zsh/checks.zsh
source ~/.zsh/colors.zsh source ~/.zsh/colors.zsh