Fix #917: enhance plugin framework to clear sel on request

With this enhancement, plugins which operate on selection
can explicitly request the program to clear the selection.
This commit is contained in:
Arun Prakash Jana 2021-03-27 17:07:58 +05:30
parent 7c63dc8d73
commit 7f5dbd11e3
No known key found for this signature in database
GPG key ID: A75979F35C080412
14 changed files with 136 additions and 72 deletions

View file

@ -14,64 +14,68 @@ Plugins extend the capabilities of `nnn`. They are _executable_ scripts (or bina
## List of plugins
| Plugin (a-z) | Description | Lang | Dependencies |
| Plugin (a-z) | [Clears selection]<sup>1</sup> Description | Lang | Dependencies |
| --- | --- | --- | --- |
| [autojump](autojump) | Navigate to dir/path | sh | [jump](https://github.com/gsamokovarov/jump)/autojump/zoxide |
| [bookmarks](bookmarks) | Use named bookmarks managed with symlinks | sh | fzf |
| [boom](boom) | Play random music from dir | sh | [moc](http://moc.daper.net/) |
| [bulknew](bulknew) | Create multiple files/dirs at once | bash | sed, xargs, mktemp |
| [chksum](chksum) | Create and verify checksums | sh | md5sum,<br>sha256sum |
| [cleanfilename](cleanfilename) | Clean filename to be more shell-friendly | sh | sed |
| [diffs](diffs) | Diff for selection (limited to 2 for directories) | sh | vimdiff, mktemp |
| [dragdrop](dragdrop) | Drag/drop files from/into nnn | sh | [dragon](https://github.com/mwh/dragon) |
| [dups](dups) | List non-empty duplicate files in current dir | bash | find, md5sum,<br>sort uniq xargs |
| [finder](finder) | Run custom find command and list | sh | - |
| [fzcd](fzcd) | Change to the directory of a fuzzy-selected file/dir | sh | fzf |
| [fzhist](fzhist) | Fuzzy-select a cmd from history, edit in `$EDITOR` and run | sh | fzf, mktemp |
| [fzopen](fzopen) | Fuzzy find a file in dir subtree and edit or open | sh | fzf, xdg-open |
| [fzz](fzz) | Change to any directory in the z database with fzf | sh | fzf, z |
| [getplugs](getplugs) | Update plugins to installed `nnn` version | sh | curl |
| [gpg\*](gpg\*) | Encrypt/decrypt files using GPG | sh | gpg |
| [gutenread](gutenread) | Browse, download, read from Project Gutenberg | sh | curl, unzip, w3m<br>[epr](https://github.com/wustho/epr) (optional) |
| [hexview](hexview) | View a file in hex in `$PAGER` | sh | [hx](https://github.com/krpors/hx)/xxd |
| [imgresize](imgresize) | Resize images in dir to screen resolution | sh | [imgp](https://github.com/jarun/imgp) |
| [imgthumb](imgthumb) | View thumbnail of an image or dir of images | sh | [lsix](https://github.com/hackerb9/lsix) |
| [imgur](imgur) | Upload an image to imgur (from [imgur-screenshot](https://github.com/jomo/imgur-screenshot)) | bash | - |
| [imgview](imgview) | Browse images, set wallpaper, copy path ([config](https://wiki.archlinux.org/index.php/Sxiv#Assigning_keyboard_shortcuts)), [rename](https://github.com/jarun/nnn/wiki/Basic-use-cases#browse-rename-images)| sh | [imv](https://github.com/eXeC64/imv)/[sxiv](https://github.com/muennich/sxiv)/[viu](https://github.com/atanunq/viu), less|
| [ipinfo](ipinfo) | Fetch external IP address and whois information | sh | curl, whois |
| [kdeconnect](kdeconnect) | Send selected files to an Android device | sh | kdeconnect-cli |
| [launch](launch) | GUI application launcher | sh | fzf |
| [mediainf](mediainf) | Show media information | sh | mediainfo |
| [mimelist](mimelist) | List files by mime in subtree | sh | - |
| [moclyrics](moclyrics) | Show lyrics of the track playing in moc | sh | [ddgr](https://github.com/jarun/ddgr), [moc](http://moc.daper.net/) |
| [mocplay](mocplay) | Append (and/or play) selection/dir/file in moc | sh | [moc](http://moc.daper.net/) |
| [mp3conv](mp3conv) | Extract audio from multimedia as mp3 | sh | ffmpeg |
| [mtpmount](mtpmount) | Toggle mount of MTP device (eg. Android) | sh | gvfs-mtp |
| [nbak](nbak) | Backs up `nnn` config | sh | tar, awk, mktemp |
| [nmount](nmount) | Toggle mount status of a device as normal user | sh | pmount, udisks2 |
| [nuke](nuke) | Sample file opener (CLI-only by default) | sh | _see in-file docs_ |
| [oldbigfile](oldbigfile) | List large files by access time | sh | find, sort |
| [organize](organize) | Auto-organize files in directories by file type | sh | file |
| [pdfread](pdfread) | Read a PDF or text file aloud | sh | pdftotext, mpv,<br>pico2wave |
| [pdfview](pdfview) | View PDF file in `$PAGER` | sh | pdftotext/<br>mupdf-tools |
| [picker](picker) | Pick files and list one per line (to pipe) | sh | nnn |
| [preview-tabbed](preview-tabbed) | Tabbed/xembed based file previewer | bash | _see in-file docs_ |
| [preview-tui](preview-tui) | Tmux/kitty/xterm/`$TERMINAL` based file previewer | sh | _see in-file docs_ |
| [preview-tui-ext](preview-tui-ext) | Meant to be an exhaustive version of [preview-tui](preview-tui) | sh | _see in-file docs_ |
| [pskill](pskill) | Fuzzy list by name and kill process or zombie | sh | fzf, ps, sudo/doas |
| [renamer](renamer) | Batch rename selection or files in dir | sh | [qmv](https://www.nongnu.org/renameutils/)/[vidir](https://joeyh.name/code/moreutils/) |
| [ringtone](ringtone) | Create a variable bitrate mp3 ringtone from file | sh | date, ffmpeg |
| [rsynccp](rsynccp) | Gives copy-paste verbose progress percentage | sh | rsync |
| [splitjoin](splitjoin) | Split file or join selection | sh | split, cat |
| [suedit](suedit) | Edit file using superuser permissions | sh | sudoedit/sudo/doas |
| [togglex](togglex) | Toggle executable mode for selection | sh | chmod |
| [treeview](treeview) | Informative tree output in `$EDITOR` | sh | tree |
| [uidgid](uidgid) | List user and group of all files in dir | sh | ls, less |
| [upgrade](upgrade) | Upgrade nnn manually on Debian 9 Stretch | sh | curl |
| [upload](upload) | Upload to Firefox Send or ix.io (text) or file.io (bin) | sh | [ffsend](https://github.com/timvisee/ffsend), curl, jq, tr |
| [vidthumb](vidthumb) | Show video thumbnails in terminal | sh | [ffmpegthumbnailer](https://github.com/dirkvdb/ffmpegthumbnailer),<br>[lsix](https://github.com/hackerb9/lsix) |
| [wall](wall) | Set wallpaper or change colorscheme | sh | nitrogen/pywal |
| [x2sel](x2sel) | Copy `\n`-separated file list from system clipboard to sel | sh | _see in-file docs_ |
| [autojump](autojump) | [ ] Navigate to dir/path | sh | [jump](https://github.com/gsamokovarov/jump)/autojump/zoxide |
| [bookmarks](bookmarks) | [ ] Use named bookmarks managed with symlinks | sh | fzf |
| [boom](boom) | [ ] Play random music from dir | sh | [moc](http://moc.daper.net/) |
| [bulknew](bulknew) | [ ] Create multiple files/dirs at once | bash | sed, xargs, mktemp |
| [chksum](chksum) | [x] Create and verify checksums | sh | md5sum,<br>sha256sum |
| [cleanfilename](cleanfilename) | [x] Clean filename to be more shell-friendly | sh | sed |
| [diffs](diffs) | [x] Diff for selection (limited to 2 for directories) | sh | vimdiff, mktemp |
| [dragdrop](dragdrop) | [ ] Drag/drop files from/into nnn | sh | [dragon](https://github.com/mwh/dragon) |
| [dups](dups) | [ ] List non-empty duplicate files in current dir | bash | find, md5sum,<br>sort uniq xargs |
| [finder](finder) | [ ] Run custom find command and list | sh | - |
| [fzcd](fzcd) | [ ] Change to the directory of a fuzzy-selected file/dir | sh | fzf |
| [fzhist](fzhist) | [ ] Fuzzy-select a cmd from history, edit in `$EDITOR` and run | sh | fzf, mktemp |
| [fzopen](fzopen) | [ ] Fuzzy find a file in dir subtree and edit or open | sh | fzf, xdg-open |
| [fzz](fzz) | [ ] Change to any directory in the z database with fzf | sh | fzf, z |
| [getplugs](getplugs) | [ ] Update plugins to installed `nnn` version | sh | curl |
| [gpg\*](gpg\*) | [x] Encrypt/decrypt files using GPG | sh | gpg |
| [gutenread](gutenread) | [ ] Browse, download, read from Project Gutenberg | sh | curl, unzip, w3m<br>[epr](https://github.com/wustho/epr) (optional) |
| [hexview](hexview) | [ ] View a file in hex in `$PAGER` | sh | [hx](https://github.com/krpors/hx)/xxd |
| [imgresize](imgresize) | [ ] Resize images in dir to screen resolution | sh | [imgp](https://github.com/jarun/imgp) |
| [imgthumb](imgthumb) | [ ] View thumbnail of an image or dir of images | sh | [lsix](https://github.com/hackerb9/lsix) |
| [imgur](imgur) | [ ] Upload an image to imgur (from [imgur-screenshot](https://github.com/jomo/imgur-screenshot)) | bash | - |
| [imgview](imgview) | [ ] Browse images, set wallpaper, copy path ([config](https://wiki.archlinux.org/index.php/Sxiv#Assigning_keyboard_shortcuts)), [rename](https://github.com/jarun/nnn/wiki/Basic-use-cases#browse-rename-images)| sh | [imv](https://github.com/eXeC64/imv)/[sxiv](https://github.com/muennich/sxiv)/[viu](https://github.com/atanunq/viu), less|
| [ipinfo](ipinfo) | [ ] Fetch external IP address and whois information | sh | curl, whois |
| [kdeconnect](kdeconnect) | [x] Send selected files to an Android device | sh | kdeconnect-cli |
| [launch](launch) | [ ] GUI application launcher | sh | fzf |
| [mediainf](mediainf) | [ ] Show media information | sh | mediainfo |
| [mimelist](mimelist) | [ ] List files by mime in subtree | sh | - |
| [moclyrics](moclyrics) | [ ] Show lyrics of the track playing in moc | sh | [ddgr](https://github.com/jarun/ddgr), [moc](http://moc.daper.net/) |
| [mocplay](mocplay) | [x] Append (and/or play) selection/dir/file in moc | sh | [moc](http://moc.daper.net/) |
| [mp3conv](mp3conv) | [ ] Extract audio from multimedia as mp3 | sh | ffmpeg |
| [mtpmount](mtpmount) | [ ] Toggle mount of MTP device (eg. Android) | sh | gvfs-mtp |
| [nbak](nbak) | [ ] Backs up `nnn` config | sh | tar, awk, mktemp |
| [nmount](nmount) | [ ] Toggle mount status of a device as normal user | sh | pmount, udisks2 |
| [nuke](nuke) | [ ] Sample file opener (CLI-only by default) | sh | _see in-file docs_ |
| [oldbigfile](oldbigfile) | [ ] List large files by access time | sh | find, sort |
| [organize](organize) | [x] Auto-organize files in directories by file type | sh | file |
| [pdfread](pdfread) | [ ] Read a PDF or text file aloud | sh | pdftotext, mpv,<br>pico2wave |
| [pdfview](pdfview) | [ ] View PDF file in `$PAGER` | sh | pdftotext/<br>mupdf-tools |
| [picker](picker) | [ ] Pick files and list one per line (to pipe) | sh | nnn |
| [preview-tabbed](preview-tabbed) | [ ] Tabbed/xembed based file previewer | bash | _see in-file docs_ |
| [preview-tui](preview-tui) | [ ] Tmux/kitty/xterm/`$TERMINAL` based file previewer | sh | _see in-file docs_ |
| [preview-tui-ext](preview-tui-ext) | [ ] Meant to be an exhaustive version of [preview-tui](preview-tui) | sh | _see in-file docs_ |
| [pskill](pskill) | [ ] Fuzzy list by name and kill process or zombie | sh | fzf, ps, sudo/doas |
| [renamer](renamer) | [x] Batch rename selection or files in dir | sh | [qmv](https://www.nongnu.org/renameutils/)/[vidir](https://joeyh.name/code/moreutils/) |
| [ringtone](ringtone) | [ ] Create a variable bitrate mp3 ringtone from file | sh | date, ffmpeg |
| [rsynccp](rsynccp) | [x] Gives copy-paste verbose progress percentage | sh | rsync |
| [splitjoin](splitjoin) | [x] Split file or join selection | sh | split, cat |
| [suedit](suedit) | [ ] Edit file using superuser permissions | sh | sudoedit/sudo/doas |
| [togglex](togglex) | [x] Toggle executable mode for selection | sh | chmod |
| [treeview](treeview) | [ ] Informative tree output in `$EDITOR` | sh | tree |
| [uidgid](uidgid) | [ ] List user and group of all files in dir | sh | ls, less |
| [upgrade](upgrade) | [ ] Upgrade nnn manually on Debian 9 Stretch | sh | curl |
| [upload](upload) | [ ] Upload to Firefox Send or ix.io (text) or file.io (bin) | sh | [ffsend](https://github.com/timvisee/ffsend), curl, jq, tr |
| [vidthumb](vidthumb) | [ ] Show video thumbnails in terminal | sh | [ffmpegthumbnailer](https://github.com/dirkvdb/ffmpegthumbnailer),<br>[lsix](https://github.com/hackerb9/lsix) |
| [wall](wall) | [ ] Set wallpaper or change colorscheme | sh | nitrogen/pywal |
| [x2sel](x2sel) | [ ] Copy `\n`-separated file list from system clipboard to sel | sh | _see in-file docs_ |
Note:
1. A plugin has to explicitly request `nnn` to clear the selection e.g. after operating on the selected files.
## Installation
@ -207,6 +211,7 @@ The `ctxcode` indicates the context to change the active directory of.
| `1`-`4` | context number |
| `0` | current context |
| `+` | smart context (next inactive else current) |
| `-` | clear the selection |
The `opcode` indicates the operation type.

View file

@ -45,6 +45,11 @@ fi
if [ "$resp" = "s" ]; then
checksum_type
sed 's|'"$PWD/"'||g' < "$selection" | xargs -0 -I{} ${chsum}sum {} > "checksum_$(date '+%Y%m%d%H%M').$chsum"
# Clear selection
if [ -p "$NNN_PIPE" ]; then
printf "-" > "$NNN_PIPE"
fi
elif [ -n "$1" ]; then
if [ -f "$1" ]; then
for chks in md5 sha1 sha224 sha256 sha384 sha512

View file

@ -21,7 +21,7 @@ cleanup() {
printf "%s" "$1" | sed -e 's/[^A-Za-z0-9._-]/_/g'
}
if [ -f "$sel" ]; then
if [ -s "$sel" ]; then
targets=$(sed -e "s/\\x0/\\n/g;\$a\\" "$sel" | \
while read -r i; do
basename "$i";
@ -57,3 +57,8 @@ for i in $targets; do
mv "$i" "$tmp$(cleanup "$i")";
fi
done
# Clear selection
if [ -s "$sel" ] && [ -p "$NNN_PIPE" ]; then
printf "-" > "$NNN_PIPE"
fi

View file

@ -54,3 +54,8 @@ if [ -s "$selection" ]; then
echo "needs at least 2 files or directories selected for comparison"
fi
fi
# Clear selection
if [ -p "$NNN_PIPE" ]; then
printf "-" > "$NNN_PIPE"
fi

View file

@ -19,3 +19,8 @@ else
fi
printf "%s" "$files" | xargs -n1 -I{} gpg --decrypt --output "{}.dec" {}
# Clear selection
if [ "$resp" = "s" ] && [ -p "$NNN_PIPE" ]; then
printf "-" > "$NNN_PIPE"
fi

View file

@ -34,4 +34,9 @@ else
recipient=$(printf "%s" "$keyuids" | fzf | awk '{print $1}')
printf "%s" "$files" | xargs -n1 gpg --encrypt --recipient "$recipient"
# Clear selection
if [ "$resp" = "s" ] && [ -p "$NNN_PIPE" ]; then
printf "-" > "$NNN_PIPE"
fi
fi

View file

@ -8,11 +8,16 @@
selection=${NNN_SEL:-${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection}
id=$(kdeconnect-cli -a --id-only | awk '{print $1}')
if [ "$(find "$selection")" ]; then
if [ -s "$selection" ]; then
kdeconnect-cli -d "$id" --share "$(cat "$selection")"
# If you want a system notification, uncomment the next 3 lines.
#notify-send -a "Kdeconnect" "Sending $(cat "$selection")"
#else
#notify-send -a "Kdeconnect" "No file selected"
# Clear selection
if [ -p "$NNN_PIPE" ]; then
printf "-" > "$NNN_PIPE"
fi
fi

View file

@ -82,3 +82,8 @@ mocp -p
# uncomment the line below to show mocp interface after appending
# mocp
# Clear selection
if [ "$resp" = "y" ] && [ -p "$NNN_PIPE" ]; then
printf "-" > "$NNN_PIPE"
fi

View file

@ -2,9 +2,13 @@
# Description: Organize files in directories by category
#
# Note: This plugin clears the selection as it changes the contents of the current dir
#
# Shell: POSIX compliant
# Author: th3lusive
sel=${NNN_SEL:-${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection}
organize() {
case "$(file -biL "$1")" in
*video*)
@ -48,6 +52,11 @@ main() {
do
[ -f "$file" ] && organize "$file"
done
# Clear selection
if [ -s "$sel" ] && [ -p "$NNN_PIPE" ]; then
printf "-" > "$NNN_PIPE"
fi
}
main "$@"

View file

@ -33,6 +33,11 @@ fi
if [ "$resp" = "y" ]; then
# -o flag is necessary for interactive editors
xargs -o -0 $batchrenamesel < "$selection"
# Clear selection
if [ -p "$NNN_PIPE" ]; then
printf "-" > "$NNN_PIPE"
fi
elif [ ! "$(LC_ALL=C ls -a)" = ".
.." ]; then
# On older systems that don't have ls -A

View file

@ -19,3 +19,8 @@ xargs -0 -a "$sel" -I % rsync -ah --progress % "$PWD"
# less verbose
# xargs -0 -a "$sel" -I % rsync -ah --info=progress2 % "$PWD"
# Clear selection
if [ -p "$NNN_PIPE" ]; then
printf "-" > "$NNN_PIPE"
fi

View file

@ -33,6 +33,11 @@ if [ "$resp" = "j" ]; then
file="$(basename "$(echo "$arr" | sed -n '1p' | sed -e 's/[0-9][0-9]$//')")"
sort -z < "$selection" | xargs -0 -I{} cat {} > "${file}.out"
# Clear selection
if [ -p "$NNN_PIPE" ]; then
printf "-" > "$NNN_PIPE"
fi
fi
elif [ "$resp" = "s" ]; then
if [ -n "$1" ] && [ -f "$1" ]; then

View file

@ -11,10 +11,11 @@
selection=${NNN_SEL:-${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection}
if [ ! -s "$selection" ]; then
printf "0 selected "
read -r _
exit
fi
if [ -s "$selection" ]; then
xargs -0 -I {} sh -c 'if [ -x "{}" ] ; then chmod -x "{}" ; else chmod +x "{}" ; fi' < "$selection"
xargs -0 -I {} sh -c 'if [ -x "{}" ] ; then chmod -x "{}" ; else chmod +x "{}" ; fi' < "$selection"
# Clear selection
if [ -p "$NNN_PIPE" ]; then
printf "-" > "$NNN_PIPE"
fi
fi

View file

@ -4754,15 +4754,15 @@ static void readpipe(int fd, char **path, char **lastname, char **lastdir)
char ctx, *nextpath = NULL;
ssize_t len = read_nointr(fd, g_buf, 1);
if (len != 1) {
if (selbufpos)
clearselection();
if (len != 1)
return;
}
if (g_buf[0] == '+')
ctx = (char)(get_free_ctx() + 1);
else if (g_buf[0] < '0')
else if (g_buf[0] == '-') { /* Clear selection on '-' */
clearselection();
return;
} else if (g_buf[0] < '0')
return;
else {
ctx = g_buf[0] - '0';
@ -4789,8 +4789,7 @@ static void readpipe(int fd, char **path, char **lastname, char **lastdir)
rmlistpath();
nextpath = load_input(fd, *path);
} else if (selbufpos)
clearselection();
}
if (nextpath) {
if (ctx == 0 || ctx == cfg.curctx + 1) { /* Same context */