Fish: notify me when you finish

Have you ever been in a situation when you called git fetch, stared into screen for several seconds and then switched to browser to read something ‘useful’ while git fetches updates? And in five minutes you’re like ‘Oh wait, I was doing something important, no?’. Rings the bell, doesn’t it?

At some point in my life1 I decided to fix it. The idea is to send notification whenever fish completes to execute long-runing command. It is part of my configurations for a long time and I decided to share it more openly only now. Shame!

Fish already exposes variable CMD_DURATION which returns duration of previous command execution in milliseconds. So it’s easy to hack a solution that we can put into fish_prompt function.

    function __d12_prompt__check_duration
      if test $CMD_DURATION
        if test $CMD_DURATION -ge $cmd_notification_threshold
          __d12_prompt__on_duration_exceeded $CMD_DURATION
          __d12_prompt__notify_completion $CMD_DURATION
      set CMD_DURATION 0

    function __d12_prompt__on_duration_exceeded -a duration
      set_color $fish_color_command
      echo -esn '  ~> duration: '
      set_color $fish_color_param
      echo -es $duration ' ms'
      set_color normal

    function __d12_prompt__notify_completion -a duration
      if command -v terminal-notifier > /dev/null
        echo -es 'Finished in ' $duration ' ms' | terminal-notifier

Just call __d12_prompt__check_duration at the very beginning2 of fish_promt. Then set value of cmd_notification_threshold to the minimal amount of milliseconds before notification is sent. Good place for setting that value is

Good luck, and don’t procrastinate too much.

P. S. Current implementation of __d12_prompt__notify_completion targets macOS users. Make sure you modify it so it works good on your system as well.

  1. Just right after I realised that I have failed to master meditation techniques that would help me to stare into terminal window until command execution completes without being distracted by other things.

  2. but after you cache $status value.

Posted on June 13, 2017 by Boris Buliga

Source: Fish: notify me when you finish