You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Cooperate with VTE to provide process completion notifications #1382
I'm looking to re-introduce the feature, cleanly this time. This requires the virtual terminal emulator and the shell to cooperate, because job completion detection in terminal without shell cooperation is unreliable, and the shell without terminal cooperation would not know to which window or tab the completed process belongs, whether the relevant window is focused (and thus show superfluous notifications), etc.
I have a basic idea on how to hack this into bash via example variables. Being an avid fish user, I'd love if fish supported that as well. However, the bash approach would not work for fish (according to built-in help page anyway), and besides I'd very much prefer it to support the feature natively.
I understand there's an architectural separation between the terminal program and the VTE, which makes it annoying to get data from the shell to the terminal program.
Using escape sequences seems cleaner than just IPC and could potentially work over SSH as well, so I'm all for it! Thanks for pointing out the exact protocol, I've added that to the spec.
However, implementing communications via escape sequences would require patching libvte (and I'm already writing the patch), but it may take a while to get it reviewed and accepted upstream so for now we've implemented the same via asynchronous D-bus callbacks.
For BASH we just set an environment variable that makes it tell the terminal the last executed command every time it shows a command prompt, which is not exactly what we need, but seems to be as close as we can get.
For FISH I see two ways of implementing this:
An environment variable defining the escape sequence code for process completion callbacks. 1-4 are occupied by Xterm and since our extension is not standardized it seems a good idea to define the number in an environment variable. If none is set process completion callbacks are disabled. The command that has just completed should be passed as the content so that the terminal can e.g. display it in a notification.
An environment variable defining the command to be executed on process completion. This should be more flexible and easier to implement in the shell at first glance, and would also work with our current hack until the escape-sequence-based method is accepted into libvte (if it's ever accepted). However, it would require a way to reliably get the command that's just finished, and this is where it gets wonky; is getting last item from history reliable enough - what about background jobs? Or is using a special variable containing the command just for this acceptable?
Just a heads-up: my implementation of this for BASH was merged and I'm now working on supporting ZSH.
I'd very much like to support FISH as well, but that will require modifications in FISH, as described above. I'd be glad to handle the FISH support in Pantheon Terminal, but I'm no good at C++ to hack on FISH itself.
I did not modify BASH, I simply prepend custom code to $PROMPT_COMMAND environment variable, the contents of which BASH executes before every interactive prompt. Here's the code: dbus-send --type=method_call --session --dest=net.launchpad.pantheon-terminal /net/launchpad/pantheon_terminal org.pantheon.terminal.ProcessFinished string:$PANTHEON_TERMINAL_ID string:"$(history 1 | cut -c 8-)";
(Fine print on the above: In BASH this is kind of a hack because it doesn't take background jobs into account, e.g. if the user types something ending with "&" the job will go into background and the command above will fire off before the background job completes. However, the terminal ignores completion reports for the currently focused tab (which is pretty natural, see video above) and background jobs complete instantly after uses presses enter, which requires the terminal to be focused, and the false positives about background jobs are ultimately ignored.)
Thus all the code for BASH and ZSH support resides in Pantheon Terminal and works with unmodified bash, libvte, etc.
It would probably be most convenient from the terminal's perspective if it could set an environment variable with the code for FISH to execute after completion of every user-initiated command. Ideally that code should receive the command that's just completed as an argument, but looking up history works too.
In this case I could eventually make notifications work over ssh using ssh's environment variable pass-through and reporting process completion via the "in-band" mechanism you described that's currently used for setting tab titles. "Eventually" because in-band reporting means either a performance penalty or patching VTE, so I use async D-bus callbacks for now.
I'm not sure how this aligns with the architecture or design goals of FISH, so of course I'm open for other options.
I'm also unsure whether completion of background jobs should be reported because I'm not sure what the use cases for them are. Personally I only run GUI programs in the background, which I then proceed to manually close, so a notification about their completion would be superfluous.
It would probably be most convenient from the terminal's perspective if it could set an environment variable with the code for FISH to execute after completion of every user-initiated command. Ideally that code should receive the command that's just completed as an argument, but looking up history works too.
This seems like a prime usecase for an eventhandler, i.e.
function pantheon_notify --on-event fish_postexec
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
Though note that this is executed after every command, not just long-running ones.
Do also note that it actually gets the entire commandline of the completed command as argv.
The post-exec hook is definitely the right way to go, but how should the terminal communicate to the shell that it's required? I'm nervous about importing functions from the environment - see e.g. shellshock. Is there a better way?
There's two easy ways I can think of: Either let supporting terminals add the function as a file or to config.fish or add it upstream (i.e. here) and add a commandline option or special env var (e.g. "NOTIFY=1" - nothing with arbitrary code, just a value to activate the already included function) to fish. Neither are going to win beauty contests, though.
I've tried entering the above function definition in interactive shell, and it worked! Thanks!
However, I could not get it to work from ~/.config/fish/config.fish file - it works if I explicitly source it from interactive shell, but not on its own. I've run strace on the file and it is indeed read by the shell, but for whatever reason the callback is not executed until I explicitly source it.
Same thing with function definition - I've put that in a function file and the function does not get executed on task completion. It is displayed correctly in fish_config web interface, but is not hooked up to fish_postexec unless I explicitly source it.
Sooo close! But for the love of all that's fishy, what am I doing wrong?!
Neither are going to win beauty contests, though.
Adding a function as a file and hooking it up to command completion event without breaking anything else is way more than other shells currently offer.
For BASH we have to do a lot of hackery on a environment variable to inject a callback without breaking anything, but we still broke something that arch ships out of the box regardless and there's nothing we can do to fix that. And don't even get me started on the way ZSH handles this.
So fish's function files win both beauty and reliability contests here. If I could get them to work, that is.
That worked for me, thanks! Looks like conditional definition of that function was the culprit. For example,
if status --is-interactive
function somefunc --on-event fish_postexec
echo BEEP
end
end
does not work, but this works:
function somefunc --on-event fish_postexec
if status --is-interactive
echo BEEP
end
end
One last question remains, then. How do I inject an event handler without breaking anything? Something similar to a function file would be ideal because I could easily update or patch the code therein and also cleanly remove it in case our terminal gets uninstalled. Via something like config.fish.d, perhaps?
@tennox: The idea in this issue is that you wouldn't call notify-send - which sends a dbus message to the general notification service - but you'd send a dbus message to the terminal.
This feature is awesome!
For the record, I just added
function pantheon_notify --on-event fish_postexec
if status --is-interactive
dbus-send --type=method_call --session --dest=org.pantheon.terminal /org/pantheon/terminal org.pantheon.terminal.ProcessFinished string:$PANTHEON_TERMINAL_ID string:"$argv[1]";
end
end
in my ~/.config/fish/config.fish.
Is it still ok to put that code in share/functions/__fish_config_interactive.fish?
Would it be ok to check the content of COMMAND_PROMPT — which is already set by the pantheon terminal — in order to activate that function?
Activity
ridiculousfish commentedon Mar 29, 2014
Slick demo :)
I understand there's an architectural separation between the terminal program and the VTE, which makes it annoying to get data from the shell to the terminal program.
What seems natural to me is to use the same mechanism as reporting the tab title (CSI blah blah BEL), as documented here http://www.tldp.org/HOWTO/text/Xterm-Title . That requires there already be a channel from the shell to the terminal program; maybe you can build on that.
I like the idea of an environment variable to enable this behavior. Sure beats trying to infer it from $TERM like we do with the title.
Shnatsel commentedon Mar 31, 2014
Using escape sequences seems cleaner than just IPC and could potentially work over SSH as well, so I'm all for it! Thanks for pointing out the exact protocol, I've added that to the spec.
However, implementing communications via escape sequences would require patching libvte (and I'm already writing the patch), but it may take a while to get it reviewed and accepted upstream so for now we've implemented the same via asynchronous D-bus callbacks.
For BASH we just set an environment variable that makes it tell the terminal the last executed command every time it shows a command prompt, which is not exactly what we need, but seems to be as close as we can get.
For FISH I see two ways of implementing this:
Which approach do you prefer, if any?
Shnatsel commentedon Apr 2, 2014
Just FYI, here's how the up-to-date implementation looks: https://www.youtube.com/watch?v=WLhTmnifAro
Shnatsel commentedon Jul 22, 2014
Just a heads-up: my implementation of this for BASH was merged and I'm now working on supporting ZSH.
I'd very much like to support FISH as well, but that will require modifications in FISH, as described above. I'd be glad to handle the FISH support in Pantheon Terminal, but I'm no good at C++ to hack on FISH itself.
ridiculousfish commentedon Jul 22, 2014
Slick! Maybe iTerm would be interested too.
ridiculousfish commentedon Jul 22, 2014
Do you have a link to the bash changes? The link above looks like it's for the pantheon terminal app.
Shnatsel commentedon Jul 22, 2014
http://finalterm.org/ would probably be interested as well.
I did not modify BASH, I simply prepend custom code to $PROMPT_COMMAND environment variable, the contents of which BASH executes before every interactive prompt. Here's the code:
dbus-send --type=method_call --session --dest=net.launchpad.pantheon-terminal /net/launchpad/pantheon_terminal org.pantheon.terminal.ProcessFinished string:$PANTHEON_TERMINAL_ID string:"$(history 1 | cut -c 8-)";(Fine print on the above: In BASH this is kind of a hack because it doesn't take background jobs into account, e.g. if the user types something ending with "&" the job will go into background and the command above will fire off before the background job completes. However, the terminal ignores completion reports for the currently focused tab (which is pretty natural, see video above) and background jobs complete instantly after uses presses enter, which requires the terminal to be focused, and the false positives about background jobs are ultimately ignored.)
Thus all the code for BASH and ZSH support resides in Pantheon Terminal and works with unmodified bash, libvte, etc.
It would probably be most convenient from the terminal's perspective if it could set an environment variable with the code for FISH to execute after completion of every user-initiated command. Ideally that code should receive the command that's just completed as an argument, but looking up history works too.
In this case I could eventually make notifications work over ssh using ssh's environment variable pass-through and reporting process completion via the "in-band" mechanism you described that's currently used for setting tab titles. "Eventually" because in-band reporting means either a performance penalty or patching VTE, so I use async D-bus callbacks for now.
I'm not sure how this aligns with the architecture or design goals of FISH, so of course I'm open for other options.
I'm also unsure whether completion of background jobs should be reported because I'm not sure what the use cases for them are. Personally I only run GUI programs in the background, which I then proceed to manually close, so a notification about their completion would be superfluous.
faho commentedon Aug 5, 2015
This seems like a prime usecase for an eventhandler, i.e.
Though note that this is executed after every command, not just long-running ones.
Do also note that it actually gets the entire commandline of the completed command as argv.
zanchey commentedon Aug 6, 2015
The post-exec hook is definitely the right way to go, but how should the terminal communicate to the shell that it's required? I'm nervous about importing functions from the environment - see e.g. shellshock. Is there a better way?
faho commentedon Aug 6, 2015
There's two easy ways I can think of: Either let supporting terminals add the function as a file or to config.fish or add it upstream (i.e. here) and add a commandline option or special env var (e.g. "NOTIFY=1" - nothing with arbitrary code, just a value to activate the already included function) to fish. Neither are going to win beauty contests, though.
Shnatsel commentedon Aug 16, 2015
I've tried entering the above function definition in interactive shell, and it worked! Thanks!
However, I could not get it to work from ~/.config/fish/config.fish file - it works if I explicitly source it from interactive shell, but not on its own. I've run strace on the file and it is indeed read by the shell, but for whatever reason the callback is not executed until I explicitly source it.
Same thing with function definition - I've put that in a function file and the function does not get executed on task completion. It is displayed correctly in fish_config web interface, but is not hooked up to fish_postexec unless I explicitly source it.
Sooo close! But for the love of all that's fishy, what am I doing wrong?!
Adding a function as a file and hooking it up to command completion event without breaking anything else is way more than other shells currently offer.
For BASH we have to do a lot of hackery on a environment variable to inject a callback without breaking anything, but we still broke something that arch ships out of the box regardless and there's nothing we can do to fix that. And don't even get me started on the way ZSH handles this.
So fish's function files win both beauty and reliability contests here. If I could get them to work, that is.
ridiculousfish commentedon Aug 16, 2015
Autoloading functions cannot be event handlers until they get run once. It's a chicken and egg problem - see #845.
However it's surprising that it fails in
config.fish. Can you check if the function is defined after?functions myfuncname.I tried putting this in
config.fish:and it did indeed echo BEEP after every command.
Shnatsel commentedon Aug 16, 2015
That worked for me, thanks! Looks like conditional definition of that function was the culprit. For example,
does not work, but this works:
One last question remains, then. How do I inject an event handler without breaking anything? Something similar to a function file would be ideal because I could easily update or patch the code therein and also cleanly remove it in case our terminal gets uninstalled. Via something like config.fish.d, perhaps?
ridiculousfish commentedon Aug 16, 2015
Unfortunately there's no mechanism yet for autoloading event handling functions. Maybe you can think of one :)
Until then we can just put it in
share/functions/__fish_config_interactive.fish, that's where platform-specific initialization lives.zanchey commentedon Jan 3, 2016
Hopefully the solutions to #1956 will help solve this problem.
tennox commentedon Sep 21, 2016
It works like that (see https://gist.github.com/TeNNoX/e027e5daabe4997093a49873411f5c0e), but the problem with not knowing if the terminal is in foreground is still there. (I solved it by using an xdotool to check the currently active window - beatiful ^^)
Any progress on how that communication could happen?
faho commentedon Sep 21, 2016
@tennox: The idea in this issue is that you wouldn't call
notify-send- which sends a dbus message to the general notification service - but you'd send a dbus message to the terminal.See earlier comments:
(getting the correct value for that last variable is left as an exercise to the reader)
glehmann commentedon Apr 29, 2017
This feature is awesome!
For the record, I just added
in my
~/.config/fish/config.fish.Is it still ok to put that code in
share/functions/__fish_config_interactive.fish?Would it be ok to check the content of
COMMAND_PROMPT— which is already set by the pantheon terminal — in order to activate that function?faho commentedon May 13, 2018
With the conf.d snippet stuff that landed in 2.3.0, this is now something that can easily be done with local customization.
I kinda don't think this needs to be in fish proper.
Shnatsel commentedon May 13, 2018
The addition of vendor config is great news! I can finally get pantheon-terminal to integrate with fish out of the box. Thanks!
Edit: done in elementary/terminal#243