function postexec_notify --arg cmd --on-event fish_postexec
set -l s $pipestatus
if test "$CMD_DURATION" -ge 25000 && ! string match -rq '(^man|\s--help)(\s|$)' $cmd
if string match "$status" -qv 0 $s
terminal-notifier -title $cmd -message 'Exited with '(string join '|' $s)' after '(math $CMD_DURATION / 1000)s
else
terminal-notifier -title $cmd -message 'Finished in '(math $CMD_DURATION / 1000)s
end
end
end
As a bonus, this includes the command line in the notification.I found that this can be somewhat annoying when reading man pages or interactive content though. To remedy this, I tried detecting whether the terminal is focused at the time of command completion, but it doesn't seem possible to get the terminal's pid from fish. The pid of the topmost application can be retrieved with:
osascript -e 'tell application "System Events" to return unix id of (first process whose its frontmost is true)'