How to get the full current command in Fish Shell

Viewed 135

I want to build a notification with info about the current command using terminal-notifier but I fail to find the way to get the current command, marked as (????) below.

alias notify=terminal-notifier -message "Done with [(????)]" -title (if test $status = 0; echo "$_ suceeded"; else; echo "$_ exited with error [$status]"; end) -sound default -ignoreDnD

Where I'd use it as echo 1; notify and the message would be Done with [echo 1;notify]

I have tried $_, status current-command and commandline but neither include the parameters or what's after the pipe. history | head -n1 doesn't have the command until after it's executed.

1 Answers

To make sure you capture the entire commandline (not just the command), I think I'd be doing something more like:

function notify
    eval "$argv"
    set s $status
    if test $s = 0
        set message Succeeded
    else
        set message "Exited with error [$s]"
    end
    echo "Done with '$argv' - $message"
end

> notify echo 1
Done with 'echo 1' - Succeeded

> notify false
Done with 'false' - Exited with error [1]

# Put multiple commands in quotes, if needed
> notify "echo 1; echo 2"
Done with 'echo 1; echo 2' - Succeeded

For simplicity, I replaced your terminal-notifier with a basic echo since I'm not on a Mac.

While commandline seems promising based on the manpage/doc, iirc it's really only useful in a keybinding and/or completion function.

I'm guessing you know this already, but in case not (or for other readers), put the notify function in ~/.config/fish/functions/notify.fish to make it persistent (and lazy-loaded on demand).

Related