80 lines
2.3 KiB
Bash
80 lines
2.3 KiB
Bash
# ===== 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)
|