Skip to content

Instantly share code, notes, and snippets.

@notthetup
Forked from aehlke/gist:992798
Created December 5, 2012 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save notthetup/4213687 to your computer and use it in GitHub Desktop.
Save notthetup/4213687 to your computer and use it in GitHub Desktop.
zsh hooks to growl completion of long-running commands
# long-running command growler
# hooks for zsh, built on bash version at http://hints.macworld.com/article.php?story=20071009124425468
preexec_functions+='save_preexec_time'
save_preexec_time() {
export PREEXEC_CMD="$(history $HISTCMD | tail -n 1| sed 's/ *[0-9]* *//')"
export PREEXEC_TIME=$(date +'%s')
}
precmd_functions+='growl_about_long_running_commands'
growl_about_long_running_commands() {
exitstatus=$?
if [ $exitstatus -eq 0 ]; then
img="$HOME/.zsh/images/pass.png"
else
img="$HOME/.zsh/images/fail.png"
fi
stop=$(date +'%s')
start=${PREEXEC_TIME:-$stop}
let elapsed=$stop-$start
max=${PREEXEC_MAX:-10}
if [ $elapsed -gt $max ]; then
growlnotify --image="$img" -n "LongRunningCommandGrowler" -m "exited with status $exitstatus after $elapsed secs" ${PREEXEC_CMD:-Some command}
fi
PREEXEC_TIME=
PREEXEC_CMD=
}
@notthetup
Copy link
Author

On OSX 10.7 with oh-my-zsh history $HISTCMD returns the last $HISTCMD lines of history starting from line #1. So to ensure the command that is being invoked is captured, history $HISTCMD has to be passed through tail n -1 to get the last command.

@Conceptron
Copy link

for it to work with the vs code terminal i had to replace the preexec_functions+='save_preexec_time' with add-zsh-hook preexec save_preexec_time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment