# This file should be sourced from either /etc/fish/config.fish
# or ~/.config/fish/config.fish in order to enable 
# process completion notifications in Pantheon Terminal.
# Like this:
#source /usr/share/pantheon-terminal/enable-fish-completion-notifications 2>/dev/null; or true

# If you come up with a way to inject this without modifying those files,
# please let me know at <sergey@elementaryos.org>

function pantheon-terminal-process-completion-callback --on-event fish_postexec --description "Notify Pantheon Terminal about task completion"
    if status --is-interactive; and set --query PANTHEON_TERMINAL_ID
        dbus-send --type=method_call --session --dest=net.launchpad.pantheon-terminal /net/launchpad/pantheon_terminal org.pantheon.terminal.ProcessFinished string:$PANTHEON_TERMINAL_ID string:"$argv[1]";
    end
end

# Some shells (e.g. BASH) lack the post-execution hook,
# so we insert a callback into their pre-prompt hook instead.
# This results in a bogus callback on first prompt, which has to be ignored.
# I've tried some clever focus-based suppression but it was prone to race conditions.
# The only reliable way to work that around that we've found
# is always ignoring the first callback in each tab on the terminal side
# and deliberately issuing a fake callback from shells with a proper
# post-execution hook, such zsh and fish.
if status --is-interactive; and set --query PANTHEON_TERMINAL_ID
    pantheon-terminal-process-completion-callback "You should not have seen this, please report the incident to Pantheon Terminal developers."
end
