]> Sergey Matveev's repositories - nnn.git/commitdiff
Refactored splitjoin and ndiff plugins, made them POSIX compliant
authorath3 <ha05190@protonmail.com>
Thu, 20 Jun 2019 08:45:24 +0000 (10:45 +0200)
committerath3 <ha05190@protonmail.com>
Thu, 20 Jun 2019 08:45:24 +0000 (10:45 +0200)
plugins/README.md
plugins/ndiff
plugins/splitjoin

index 3b8f55085d7b57b971103b907512d4ec0be4efe2..7ab679dde2744197fa68800b4b66ae2f63c07ffd 100644 (file)
@@ -9,16 +9,16 @@
 | imgur | bash | - | Upload an image to imgur (from [imgur-screenshot](https://github.com/jomo/imgur-screenshot)) |
 | ipinfo | sh | curl, whois | Fetch external IP address and whois information |
 | kdeconnect | sh | kdeconnect-cli | Send selected files to an Android device |
-| md5sum | sh | md5sum | Create and verify md5 checksums |
+| checksum | sh | md5sum, sha256sum | Create and verify checksums |
 | mocplay | sh | [moc](http://moc.daper.net/) | Appends (and plays, see script) selection/dir/file in moc|
-| ndiff | bash | vimdiff | Diff for selection (limited to 2 for directories) |
+| ndiff | sh | vimdiff | Diff for selection (limited to 2 for directories) |
 | nmount | sh | pmount, udisks2 | Toggle mount status of a device as normal user |
 | nwal | sh | nitrogen | Set the selected image as wallpaper using nitrogen |
 | pastebin | sh | [pastebinit](https://launchpad.net/pastebinit) | Paste contents of (text) file to paste.ubuntu.com |
 | pdfview | sh | pdftotext/mupdf-tools | View PDF file in `$PAGER` |
 | picker | sh | nnn | Pick files and pipe the newline-separated list to another utility |
 | pywal | sh | pywal | Set selected image as wallpaper, change terminal color scheme |
-| splitjoin | bash | split, cat | Split file or join selection |
+| splitjoin | sh | split, cat | Split file or join selection |
 | sxiv | sh | sxiv | Browse images in a dir in sxiv, set wallpaper, copy path ([config](https://wiki.archlinux.org/index.php/Sxiv#Assigning_keyboard_shortcuts))|
 | transfer | sh | curl | Upload file to transfer.sh |
 | upgrade | sh | wget | Upgrade to latest nnn version manually on Debian 9 Stretch |
index e76b151c19d2b86c2efb0b407ec989e33dea3de5..59193ffc25d75b7fa67803c6e4517421e293cb74 100755 (executable)
@@ -1,26 +1,28 @@
-#!/usr/bin/env bash
+#!/usr/bin/env sh
 
 # Description: Show diff of 2 directories or multiple files in vimdiff
 #
-# Shell: Bash
+# Shell: POSIX compliant
 # Author: Arun Prakash Jana
 
 selection=~/.config/nnn/.selection
 
 if [ -s $selection ]; then
-    arr=$(cat $selection | tr '\0' '\n')
-    { read -r f1; read -r f2; } <<< "$arr"
-
-    if [ -z "$f2" ]; then
-        exit
-    fi
-
-    if [ -d "$f1" ] && [ -d "$f2" ]; then
-        vimdiff <(cd "$f1" && find | sort) <(cd "$f2" && find | sort)
+    arr=$(tr '\0' '\n' < "$selection")
+    if [ "$(echo "$arr" | wc -l)" -gt 1 ]; then
+         f1="$(echo "$arr" | sed -n '1p')"
+         f2="$(echo "$arr" | sed -n '2p')"
+         if [ -d "$f1" ] && [ -d "$f2" ]; then
+            dir1=$(mktemp "${TMPDIR:-/tmp}"/nnn-$(basename "$f1").XXXXXXXX)
+            dir2=$(mktemp "${TMPDIR:-/tmp}"/nnn-$(basename "$f2").XXXXXXXX)
+            ls -A1 "$f1" > "$dir1"
+            ls -A1 "$f2" > "$dir2"
+            vimdiff "$dir1" "$dir2"
+            rm "$dir1" "$dir2"
+        else
+            cat $selection | xargs -0 vimdiff
+        fi
     else
-        cat $selection | xargs -0 -o vimdiff
-
-        # For GNU xargs (note: ignoreme takes up $0)
-        # cat $selection | xargs -0 bash -c '</dev/tty vimdiff "$@"' ignoreme
+        echo "needs at least 2 files or directories selected for comparison"
     fi
 fi
index cbb573e6f659c36b61ccd7841f2071af46bffa55..4325d3a7ac85b95a6f5d9043ca4224a471838ce8 100755 (executable)
@@ -1,34 +1,46 @@
-#!/usr/bin/env bash
+#!/usr/bin/env sh
 
 # Description: Splits the file passed as argument or joins selection
 #
 # Note: Adds numeric suffix to split files
-#       Adds '.join' suffix to the first file to be joined and saves as output file for join
+#       Adds '.out suffix to the first file to be joined and saves as output file for join
 #
-# Shell: Bash
+# Shell: POSIX compliant
 # Author: Arun Prakash Jana
 
 selection=~/.config/nnn/.selection
+resp=s
 
-echo -n "press 's' (split current file) or 'j' (join selection): "
-read resp
+if [ -s "$selection" ]; then
+    echo -n "press 's' (split current file) or 'j' (join selection): "
+    read resp
+fi
 
 if [ "$resp" = "j" ]; then
     if [ -s "$selection" ]; then
-        arr=$(cat $selection | tr '\0' '\n')
-        { read -r file; } <<< "$arr"
-
-        file=$(basename "$file").out
+        arr=$(tr '\0' '\n' < "$selection")
+        if [ "$(echo "$arr" | wc -l)" -lt 2 ]; then
+            echo "joining needs at least 2 files"
+            exit
+        fi
+        for entry in $arr
+        do
+            if [ -d "$entry" ]; then
+                echo "cant join directories"
+                exit
+            fi
+        done
 
-        cat "$selection" | sort -z | xargs -0 -i cat {} > "$file"
+        file="$(basename "$(echo "$arr" | sed -n '1p' | sed -e 's/[0-9][0-9]$//')")"
+        sort -z < "$selection" | xargs -0 -I{} cat {} > "${file}.out"
     fi
 elif [ "$resp" = "s" ]; then
-    if ! [ -z "$1" ] && [ -f "$1" ] ; then
+    if [ -n "$1" ] && [ -f "$1" ]; then
         # a single file is passed
         echo -n "split size in MB: "
         read size
 
-        if ! [ -z "$size" ]; then
+        if [ -n "$size" ]; then
             split -d -b "$size"M "$1" "$1"
         fi
     fi