From b6ec7a5cd9626062f51f88437b7b4ee4323e85a2 Mon Sep 17 00:00:00 2001 From: Simon Ruderich Date: Thu, 20 Dec 2012 17:54:04 +0100 Subject: [PATCH] zsh/rc: Display running time for long running processes. Also send a bell to the terminal when the process is done. --- zsh/rc | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/zsh/rc b/zsh/rc index aa195f2..c3fd79f 100644 --- a/zsh/rc +++ b/zsh/rc @@ -265,6 +265,46 @@ else RUN_VCS_INFO= fi +typeset -a longrun_data +longrun_data=() +# Display runtime in seconds for long running programs (> 60 seconds) and send +# a bell to notify me. +longrun_preexec() { + # Don't track the time for certain (possible) long running processes which + # need no automatic notification. + for ignore in elinks man mutt vim; do + case $3 in + $ignore | $ignore\ *) + longrun_data=() + return + ;; + esac + done + + longrun_data=("$3" $EPOCHSECONDS) +} +longrun_precmd() { + # No previous timestamp available or disabled for this command, ignore. + if [[ -z $longrun_data ]]; then + return + fi + + local difference=$(( EPOCHSECONDS - longrun_data[2] )) + if [[ $difference -gt 60 ]]; then + echo + echo -n "${fg[yellow]}" + echo -n "~> ${(V)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. + longrun_data=() +} +preexec_functions+=(longrun_preexec) +precmd_functions+=(longrun_precmd) + # Set the prompt. A two line prompt is used. On the top left the current # working directory is displayed, on the right vcs_info (if available) and the # current time in hex. On the bottom left current user name and host is shown, -- 2.45.2