Skip to content

Cooperate with VTE to provide process completion notifications #1382

Closed

Description

@Shnatsel

Pantheon Terminal, part of elementary OS and Pantheon DE, once supported process completion notifications as seen in https://www.youtube.com/watch?v=SyG1-Szs0YY
The implementation was a dirty hack so it was eventually removed.

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.

My implementation notes can be found at https://docs.google.com/document/d/1Tgf8WpKrSpZnDRXGJhY3JHNbmsCaJpnTkApmdFC92No/edit
I'd appreciate any comments or feedback.

Activity

  1. ridiculousfish commented on Mar 29, 2014

    @ridiculousfish
    Member

    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.

  2. Shnatsel commented on Mar 31, 2014

    @Shnatsel
    Author

    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?

    Which approach do you prefer, if any?

  3. Shnatsel commented on Apr 2, 2014

    @Shnatsel
    Author
  4. Shnatsel commented on Jul 22, 2014

    @Shnatsel
    Author

    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.

  5. ridiculousfish commented on Jul 22, 2014

    @ridiculousfish
    Member

    Slick! Maybe iTerm would be interested too.

  6. ridiculousfish commented on Jul 22, 2014

    @ridiculousfish
    Member

    Do you have a link to the bash changes? The link above looks like it's for the pantheon terminal app.

  7. Shnatsel commented on Jul 22, 2014

    @Shnatsel
    Author

    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.

  8. added this to the fish-future milestone on Nov 2, 2014
  9. faho commented on Aug 5, 2015

    @faho
    Member

    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.

  10. zanchey commented on Aug 6, 2015

    @zanchey
    Member

    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?

  11. faho commented on Aug 6, 2015

    @faho
    Member

    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.

  12. Shnatsel commented on Aug 16, 2015

    @Shnatsel
    Author

    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.

  13. ridiculousfish commented on Aug 16, 2015

    @ridiculousfish
    Member

    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:

    function somefunc --on-event fish_postexec
       echo BEEP
    end
    

    and it did indeed echo BEEP after every command.

  14. Shnatsel commented on Aug 16, 2015

    @Shnatsel
    Author

    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?

  15. ridiculousfish commented on Aug 16, 2015

    @ridiculousfish
    Member

    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.

  16. zanchey commented on Jan 3, 2016

    @zanchey
    Member

    Hopefully the solutions to #1956 will help solve this problem.

  17. tennox commented on Sep 21, 2016

    @tennox

    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?

  18. faho commented on Sep 21, 2016

    @faho
    Member

    @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:

    dbus-send --type=method_call --session --dest=net.launchpad.pantheon-terminal /net/launchpad/pantheon_terminal org.pantheon.terminal.ProcessFinished string:$PANTHEON_TERMINAL_ID string:"$THECOMMANDTHATFINISHED";

    (getting the correct value for that last variable is left as an exercise to the reader)

  19. glehmann commented on Apr 29, 2017

    @glehmann

    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?

  20. faho commented on May 13, 2018

    @faho
    Member

    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.

  21. modified the milestones: fish-future, fish 2.3.0 on May 13, 2018
  22. Shnatsel commented on May 13, 2018

    @Shnatsel
    Author

    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

  23. locked as resolved and limited conversation to collaborators on Apr 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions