From 86e579799b3fff5aa181cf0ff843979751a60f19 Mon Sep 17 00:00:00 2001 From: lvgx Date: Mon, 4 May 2020 06:05:13 +0200 Subject: Add an option to print hovered files to a FIFO (#548) * Add an option to print hovered files to a FIFO This adds an env variable, `NNN_FIFO`, that can be set to a path that `nnn` will open/create as a FIFO, and where every hovered file's path is printed. This allows creating external perview/quick open plugins, ... It can be compiled out with the make variable `O_NOFIFO`. * Check filename ptr instead of full path (for FIFO) * Add documentation to use NNN_FIFO in plugins * Fix path sent to FIFO in empty dirs --- plugins/README.md | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) (limited to 'plugins') diff --git a/plugins/README.md b/plugins/README.md index e8f8958..40386bb 100644 --- a/plugins/README.md +++ b/plugins/README.md @@ -190,7 +190,7 @@ Usage examples can be found in the Examples section below. There are many plugins provided by `nnn` which can be used as examples. Here are a few simple selected examples. - Show the git log of changes to the particular file along with the code for a quick and easy review. - ```sh + ```sh #!/usr/bin/env sh git log -p -- "$1" ``` @@ -220,6 +220,40 @@ There are many plugins provided by `nnn` which can be used as examples. Here are printf "%s" "0c$dir" > "$NNN_PIPE" ``` +#### Get notified on file hover + +If `NNN_FIFO` is set, `nnn` will open it and write every hovered files. +This can be used in plugins, e.g. to implement file previews. + +If a `NNN_FIFO` is set globally, each `nnn` instance will write to it, and a process reading from the pipe will get hovered path from every instance, interleaved. + +If you want to prevent this and be sure to have a private pipe to one `nnn` instance, you can unlink (remove) the FIFO file. +If you opened the FIFO before and you have read from it (so that `nnn` have it opened too), you can still read from it while you don't close it. +But new `nnn` instances will recreate a new FIFO not linked to the previous one. + +Don't forget to fork in the background to avoid blocking `nnn`. + +Example (send every hovered file to X selection): + +```sh +#!/usr/bin/env sh +if [ -z "$NNN_FIFO" ] ; then + exit 1 +fi + +while read FILE ; do + if [ -n "$NNN_FIFO" ] ; then + # If you want to remove the FIFO, + # don't do it before first read, + # nnn won't have it opened yet + rm "$NNN_FIFO" + NNN_FIFO= + fi + printf "%s" "$FILE" | xsel +done < "$NNN_FIFO" & +disown +``` + ## Contributing plugins 1. Add informative sections like _Description_, _Notes_, _Dependencies_, _Shell_, _Author_ etc. in the plugin. -- cgit v1.2.3-70-g09d2