kovidgoyal / kitty

Cross-platform, fast, feature-rich, GPU based terminal
https://sw.kovidgoyal.net/kitty/
GNU General Public License v3.0
24.41k stars 980 forks source link

Notify when a long running command is finished #1892

Closed gak closed 5 years ago

gak commented 5 years ago

There's a handy feature I've used in several terminals before, where a long running command can notify me when it's finished.

An example implementation is in iTerm where you press cmd+alt+a and an eye appears on the top right. When the currently running command finishes, a notification pops up. This functionality in iTerm needs their shell integration tools installed in your system.

Is there anything similar to this implemented in kitty?

kovidgoyal commented 5 years ago

Press ctrl-z, type

fg; some-command-that-performs-whatever notification you like

job control is the job of the shell, not the terminal.

kovidgoyal commented 5 years ago

Oh and you can automate all of that by using send_text in kitty.conf to map a single keypress to send the ctrl-zfg ; command to the shell directly

gak commented 5 years ago

send_text in kitty.conf

Ah great, I will check that out. Thanks.

alok commented 3 years ago

@gak did you ever ended up automating it? care to share the script?

gak commented 3 years ago

Sorry @alok I gave up on kitty.

I'm now using Alacritty+zsh with https://github.com/marzocchi/zsh-notify. If you're using zsh that should work for you in kitty.

kovidgoyal commented 3 years ago

You dont need a script or a zsh plugin

map f1 send_text all \x1afg;printf done\r

in kitty.conf, replace the printf with whatever command you like

EgZvor commented 2 years ago

Here's a solution I made for fish following Kovid's advice.

Put this in $HOME/.config/fish/conf.d/somename.fish

# This function allows you to switch to a different task
# when an interactive command takes too long
# by notifying you when it is finished.
#
# It is invoked by the fish shell automatically using its event system.
function __postexec_notify_on_long_running_commands --on-event fish_postexec
    set --function interactive_commands 'vim' 'vlc' 'zathura' 'gitk' 'man' 'less'
    set --function command (string split ' ' $argv[1])
    if contains $command $interactive_commands
        # We quit interactive commands manually,
        # no need for a notification.
        return
    end

    if test $CMD_DURATION -gt 5000
        notify-send 'command finished' "$argv"
    end
end
vaivaswatha commented 1 year ago

I discovered this "trick" by accident. It works on Ubuntu.

Whenever I have a command still running, I press alt+a (this prints ^[a on that terminal), and then focus out of that kitty window. When the command finishes, I get a notification from kitty that it's ready (and I can click on that notification to get back focus to the terminal I was in).

illfygli commented 1 year ago

You can also use kitten @ ls to check if the focused window is self, and notify-send if not, e.g. in your prompt.

Then you don't get notifications for something you're already looking at, or when you quit vim or other interactive commands.

jinliu commented 1 year ago

Here's the code implementing @illfygli 's idea above:

kitty @ ls | jq -e ".[].tabs.[] | select(any(.windows.[]; .is_self)) | .is_focused" >/dev/null

It evaluates to true only when the os window is focused AND the current tab is active.

0xMH commented 6 months ago

I created a MacOs version of @EgZvor script.

# This function allows you to switch to a different task
# when an interactive command takes too long
# by notifying you when it is finished.
#
# It is invoked by the fish shell automatically using its event system.
function __postexec_notify_on_long_running_commands --on-event fish_postexec
    set --function interactive_commands vim vlc zathura gitk man less
    set --function command (string split ' ' $argv[1])
    if contains $command $interactive_commands
        # We quit interactive commands manually,
        # no need for a notification.
        return
    end

    if test $CMD_DURATION -gt 5000
        # Enhance notification message to include the command and its duration
        set --function duration_in_seconds (math $CMD_DURATION / 1000)
        set --function notification_message "Completed command: '$command[1]' after $duration_in_seconds seconds."
        osascript -e "display notification \"$notification_message\" with title \"Command Notification\""
    end
end
EgZvor commented 5 months ago

@0xMH I myself switched from tracking execution time to just checking if the window containing the terminal is active with xdotool https://gitlab.com/egzvor/configrc/-/blob/687c4cdd433b5d755520da14cfe14b99aae6297b/config/fish/conf.d/notify_after_long_running_commands.fish . I had too much false positives with the interactvie_commands blacklist.