From 7e5328994a6c7c70d94381014dbabe9d8cbcf10d Mon Sep 17 00:00:00 2001 From: "Trafficone@Mg" Date: Tue, 15 Mar 2022 16:31:35 +0900 Subject: [PATCH 1/3] feat: added support for Fish shell --- README.rst | 11 ++++--- ntfy/cli.py | 36 ++++++++++++++-------- ntfy/shell_integration/auto-ntfy-done.fish | 30 ++++++++++++++++++ 3 files changed, 61 insertions(+), 16 deletions(-) create mode 100644 ntfy/shell_integration/auto-ntfy-done.fish diff --git a/README.rst b/README.rst index ee8e189..9b8982c 100644 --- a/README.rst +++ b/README.rst @@ -57,16 +57,18 @@ require Python DBUS bindings. See `here <#linux-desktop-notifications---linux>`_ Shell integration ~~~~~~~~~~~~~~~~~ ``ntfy`` has support for **automatically** sending notifications when long -running commands finish in bash and zsh. In bash it emulates zsh's preexec and +running commands finish in bash, zsh, and fish. In bash it emulates zsh's preexec and precmd functionality with `rcaloras/bash-preexec `_. -To enable it add the following to your ``.bashrc`` or ``.zshrc``: +In fish it hooks into the `fish_preexec` and `fish_postexec` function events. +To enable it add the following to your ``.bashrc``, ``.zshrc``, or ``config.fish``: .. code:: shell - eval "$(ntfy shell-integration)" + eval "$(ntfy shell-integration)" # omit $ in case of fish By default it will only send notifications for commands lasting longer than 10 -seconds and if the terminal is focused. Terminal focus works on X11(Linux) and +seconds and if the terminal is focused. This can be changed using the ``-L`` +option when invoking ``shell integration``. Terminal focus works on X11(Linux) and with Terminal.app and iTerm2 on MacOS. Both options can be configured via the ``--longer-than`` and ``--foreground-too`` options. @@ -460,3 +462,4 @@ Contributors - `webworxshop `_ - Rocket.Chat support - `rhabbachi `_ - transient option in Linux desktop notifications - `Half-Shot `_ - Matrix support +- `Trafficone ` - Fish shell support diff --git a/ntfy/cli.py b/ntfy/cli.py index 7dad0b2..d30fe8c 100644 --- a/ntfy/cli.py +++ b/ntfy/cli.py @@ -123,17 +123,29 @@ def watch_pid(args): def auto_done(args): - if args.longer_than: - print('export AUTO_NTFY_DONE_LONGER_THAN=-L{}'.format( - args.longer_than)) - if args.unfocused_only: - print('export AUTO_NTFY_DONE_UNFOCUSED_ONLY=-b') - if args.shell == 'bash': - print('source {}'.format(sh_quote(scripts['bash-preexec.sh']))) - print('source {}'.format(sh_quote(scripts['auto-ntfy-done.sh']))) - print("# To use ntfy's shell integration, run " - "this and add it to your shell's rc file:") - print('# eval "$(ntfy shell-integration)"') + if args.shell in ['bash','zsh']: + if args.longer_than: + print('export AUTO_NTFY_DONE_LONGER_THAN=-L{}'.format( + args.longer_than)) + if args.unfocused_only: + print('export AUTO_NTFY_DONE_UNFOCUSED_ONLY=-b') + if args.shell == 'bash': + print('source {}'.format(sh_quote(scripts['bash-preexec.sh']))) + print('source {}'.format(sh_quote(scripts['auto-ntfy-done.sh']))) + print("# To use ntfy's shell integration, run " + "this and add it to your shell's rc file:") + print('# eval "$(ntfy shell-integration)"') + else: + if args.longer_than: + print('set -x AUTO_NTFY_DONE_LONGER_THAN -L{}'.format( + args.longer_than)) + if args.unfocused_only: + print('set -x AUTO_NTFY_DONE_UNFOCUSED_ONLY -b') + print('source {}'.format(sh_quote(scripts['auto-ntfy-done.sh']))) + print("# To use ntfy's shell integration, run " + "this and add it to your shell's config file:") + print('# eval "(ntfy shell-integration)"') + return None, None @@ -274,7 +286,7 @@ def default_sender(args): '-s', '--shell', default=path.split(environ.get('SHELL', ''))[1], - choices=['bash', 'zsh'], + choices=['bash', 'zsh','fish'], help='The shell to integrate ntfy with (default: $SHELL)') shell_integration_parser.add_argument( '-L', diff --git a/ntfy/shell_integration/auto-ntfy-done.fish b/ntfy/shell_integration/auto-ntfy-done.fish new file mode 100644 index 0000000..c778a97 --- /dev/null +++ b/ntfy/shell_integration/auto-ntfy-done.fish @@ -0,0 +1,30 @@ +# In bash this requires https://github.com/rcaloras/bash-preexec +# If sourcing this via ntfy auto-done, it is sourced for you. + +# Default to ignoring some well known interactive programs +set -x AUTO_NTFY_DONE_IGNORE $AUTO_NTFY_DONE_IGNORE ntfy emacs htop info less mail man meld most mutt nano screen ssh tail tmux top vi vim watch +# Bash option example +#AUTO_NTFY_DONE_OPTS='-b default' +# Zsh option example +#AUTO_NTFY_DONE_OPTS=(-b default) +# notify for unfocused only (Used by ntfy internally) +#AUTO_NTFY_DONE_UNFOCUSED_ONLY=-b +# notify for commands runing longer than N sec only (Used by ntfy internally) +#set -x AUTO_NTFY_DONE_LONGER_THAN -L60 + +function _ntfy_precmd --on-event fish_postexec + set -l ret_value $status + test $ntfy_start_time || return + set -l appname (basename (string split ' ' $argv)[1]) + contains $appname $AUTO_NTFY_DONE_IGNORE && return + set -l ntfy_command $argv + set -l duration (math (date +%s) - $ntfy_start_time) + + ntfy $AUTO_NTFY_DONE_OPTS done \ + $AUTO_NTFY_DONE_UNFOCUSED_ONLY $AUTO_NTFY_DONE_LONGER_THAN \ + --formatter $ntfy_command $ret_value $duration +end + +function _ntfy_preexec --on-event fish_preexec + set -g ntfy_start_time (date +%s) +end From 28dcd5ec26f2bc5626b2ca9ae74fdc52617aaadc Mon Sep 17 00:00:00 2001 From: "Trafficone@Mg" Date: Tue, 15 Mar 2022 17:07:05 +0900 Subject: [PATCH 2/3] fix: fish shell integration script name --- ntfy/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ntfy/cli.py b/ntfy/cli.py index d30fe8c..7d83ec1 100644 --- a/ntfy/cli.py +++ b/ntfy/cli.py @@ -141,7 +141,7 @@ def auto_done(args): args.longer_than)) if args.unfocused_only: print('set -x AUTO_NTFY_DONE_UNFOCUSED_ONLY -b') - print('source {}'.format(sh_quote(scripts['auto-ntfy-done.sh']))) + print('source {}'.format(sh_quote(scripts['auto-ntfy-done.fish']))) print("# To use ntfy's shell integration, run " "this and add it to your shell's config file:") print('# eval "(ntfy shell-integration)"') From 00a44b0133d6cb87f3e554e74a2f7cd2c8a6c2af Mon Sep 17 00:00:00 2001 From: Jason Schlesinger Date: Tue, 22 Mar 2022 12:23:23 +0900 Subject: [PATCH 3/3] Fixed Fish Integration Install Process --- ntfy/cli.py | 7 +++---- ntfy/data.py | 2 +- ntfy/shell_integration/auto-ntfy-done.fish | 2 +- setup.py | 2 +- 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/ntfy/cli.py b/ntfy/cli.py index 7d83ec1..d36c76e 100644 --- a/ntfy/cli.py +++ b/ntfy/cli.py @@ -137,15 +137,14 @@ def auto_done(args): print('# eval "$(ntfy shell-integration)"') else: if args.longer_than: - print('set -x AUTO_NTFY_DONE_LONGER_THAN -L{}'.format( + print('set -x AUTO_NTFY_DONE_LONGER_THAN -L{};'.format( args.longer_than)) if args.unfocused_only: - print('set -x AUTO_NTFY_DONE_UNFOCUSED_ONLY -b') - print('source {}'.format(sh_quote(scripts['auto-ntfy-done.fish']))) + print('set -x AUTO_NTFY_DONE_UNFOCUSED_ONLY -b;') + print('source {};'.format(sh_quote(scripts['auto-ntfy-done.fish']))) print("# To use ntfy's shell integration, run " "this and add it to your shell's config file:") print('# eval "(ntfy shell-integration)"') - return None, None diff --git a/ntfy/data.py b/ntfy/data.py index 03809c7..75acc8d 100644 --- a/ntfy/data.py +++ b/ntfy/data.py @@ -23,7 +23,7 @@ class icon(object): icon_file.write(get_data('ntfy', 'icon.' + fmt)) scripts = {} -for script in ['auto-ntfy-done.sh', 'bash-preexec.sh']: +for script in ['auto-ntfy-done.sh', 'bash-preexec.sh', 'auto-ntfy-done.fish']: script_path = path.abspath(path.join(ntfy_data_dir, script)) scripts[script] = script_path if not path.isfile(script_path) or progmtime > path.getmtime(script_path): diff --git a/ntfy/shell_integration/auto-ntfy-done.fish b/ntfy/shell_integration/auto-ntfy-done.fish index c778a97..f0b738b 100644 --- a/ntfy/shell_integration/auto-ntfy-done.fish +++ b/ntfy/shell_integration/auto-ntfy-done.fish @@ -22,7 +22,7 @@ function _ntfy_precmd --on-event fish_postexec ntfy $AUTO_NTFY_DONE_OPTS done \ $AUTO_NTFY_DONE_UNFOCUSED_ONLY $AUTO_NTFY_DONE_LONGER_THAN \ - --formatter $ntfy_command $ret_value $duration + --formatter $ntfy_command $ret_value $duration & end function _ntfy_preexec --on-event fish_preexec diff --git a/setup.py b/setup.py index 1008f5f..f13aaca 100644 --- a/setup.py +++ b/setup.py @@ -59,7 +59,7 @@ keywords='push notification', packages=find_packages(exclude=['tests', 'tests.*']), - package_data={'ntfy': ['icon.png', 'icon.ico', 'shell_integration/*.sh']}, + package_data={'ntfy': ['icon.png', 'icon.ico', 'shell_integration/*.sh', 'shell_integration/*.fish']}, install_requires=deps,