Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for Fish Shell #246

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/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.

Expand Down Expand Up @@ -460,3 +462,4 @@ Contributors
- `webworxshop <https://github.com/webworxshop>`_ - Rocket.Chat support
- `rhabbachi <https://github.com/rhabbachi>`_ - transient option in Linux desktop notifications
- `Half-Shot <https://github.com/Half-Shot>`_ - Matrix support
- `Trafficone <https://github.com/trafficone>` - Fish shell support
35 changes: 23 additions & 12 deletions ntfy/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,28 @@ 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.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


Expand Down Expand Up @@ -274,7 +285,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',
Expand Down
2 changes: 1 addition & 1 deletion ntfy/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
30 changes: 30 additions & 0 deletions ntfy/shell_integration/auto-ntfy-done.fish
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down