From f95ee54870e2259e9384c6f5641cb6d374526a65 Mon Sep 17 00:00:00 2001
From: Arun Prakash Jana <engineerarun@gmail.com>
Date: Thu, 12 Dec 2019 00:56:39 +0530
Subject: [PATCH] Add plugin to show notification

---
 README.md       |  3 +--
 plugins/.notify | 20 ++++++++++++++++++++
 src/nnn.c       | 11 +++++++----
 3 files changed, 28 insertions(+), 6 deletions(-)
 create mode 100755 plugins/.notify

diff --git a/README.md b/README.md
index 346e4e70..34ba51d1 100644
--- a/README.md
+++ b/README.md
@@ -56,12 +56,12 @@ Add to that an awesome [Wiki](https://github.com/jarun/nnn/wiki)!
   - Detailed file information
   - Media information (using plugin)
 - Convenience
-  - Notification on cp, mv, rm completion (needs ntfy)
   - Run plugins and commands with custom keybinds
   - FreeDesktop compliant trash (needs trash-cli)
   - Cross-dir file/all/range selection
   - Batch renamer (feature-limited) for selection or dir
   - Copy (as), move (as), delete, archive, link selection
+  - Notification on cp, mv, rm completion
   - Create (with parents), rename, duplicate (anywhere) files and dirs
   - Launch GUI apps, run commands, execute file, spawn a shell
   - Hovered file set as `$nnn` at prompt and spawned shell
@@ -101,7 +101,6 @@ A curses library with wide char support (e.g. ncursesw), libreadline (`make O_NO
 | archivemount, fusermount(3) | optional | mount, unmount archives |
 | sshfs, [rclone](https://rclone.org/), fusermount(3) | optional | mount, unmount remotes |
 | trash-cli | optional | trash files (default action: rm) |
-| [ntfy](https://github.com/dschep/ntfy) | optional | operation completion notification |
 | vlock (Linux), bashlock (macOS), lock(1) (BSD) | optional | terminal locker (fallback: [cmatrix](https://github.com/abishekvashok/cmatrix)) |
 | advcpmv (Linux) ([integration](https://github.com/jarun/nnn/wiki/Advanced-use-cases#show-cp-mv-progress)) | optional | copy, move progress |
 | `$VISUAL` (else `$EDITOR`), `$PAGER`, `$SHELL` | optional | fallback vi, less, sh |
diff --git a/plugins/.notify b/plugins/.notify
new file mode 100755
index 00000000..fe0e18a5
--- /dev/null
+++ b/plugins/.notify
@@ -0,0 +1,20 @@
+#!/usr/bin/env sh
+
+# Description: Show a notification
+#
+# Details: nnn invokes this plugin to show notification when a cp/mv/rm operation is complete.
+#
+# Requires: notify-send (Ubuntu)/ntfy (https://github.com/dschep/ntfy)/osascript (macOS)
+#
+# Shell: POSIX compliant
+# Author: Anna Arad
+
+OS="$(uname)"
+
+if which notify-send >/dev/null 2>&1; then
+    notify-send nnn "Done!"
+elif [ "$OS" = "Darwin" ]; then
+    osascript -e 'display notification "Done!" with title "nnn"'
+elif which ntfy >/dev/null 2>&1; then
+    ntfy -t nnn send "Done!"
+fi
diff --git a/src/nnn.c b/src/nnn.c
index 1e25bf8c..7500d01a 100644
--- a/src/nnn.c
+++ b/src/nnn.c
@@ -138,7 +138,7 @@
 #define CTX_MAX 4
 #define DOT_FILTER_LEN 7
 #define ASCII_MAX 128
-#define EXEC_ARGS_MAX 10
+#define EXEC_ARGS_MAX 8
 #define SCROLLOFF 3
 #define MIN_DISPLAY_COLS 10
 #define LONG_SIZE sizeof(ulong)
@@ -379,6 +379,7 @@ static bool g_plinit = FALSE;
 #define UTIL_SH 14
 #define UTIL_FZF 15
 #define UTIL_FZY 16
+#define UTIL_NOTIFY 17
 
 /* Utilities to open files, run actions */
 static char * const utils[] = {
@@ -411,6 +412,7 @@ static char * const utils[] = {
 	"sh",
 	"fzf",
 	"fzy",
+	".notify",
 };
 
 /* Common strings */
@@ -4445,7 +4447,7 @@ nochange:
 
 				/* If open file is disabled on right arrow or `l`, return */
 				if (cfg.nonavopen && sel == SEL_NAV_IN)
-					continue;
+					goto nochange;
 
 				/* Handle plugin selection mode */
 				if (cfg.runplugin) {
@@ -4972,8 +4974,9 @@ nochange:
 			if (!cpmvrm_selection(sel, path, &presel))
 				goto nochange;
 
-			spawn("ntfy -l CRITICAL -t nnn send Done!",
-			      NULL, NULL, NULL, F_NOWAIT | F_NOTRACE | F_MULTI);
+			/* Show notification on operation complete */
+			mkpath(plugindir, utils[UTIL_NOTIFY], newpath);
+			spawn(newpath, NULL, NULL, NULL, F_NOWAIT | F_NOTRACE);
 
 			if (ndents)
 				copycurname();
-- 
2.51.0