From 4757873d3a4f72664649f12c56aa7f593cd0cfa6 Mon Sep 17 00:00:00 2001 From: ath3 Date: Wed, 19 Jun 2019 03:57:09 +0200 Subject: [PATCH 1/4] Added md5sum plugin --- plugins/README.md | 1 + plugins/md5sum | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100755 plugins/md5sum diff --git a/plugins/README.md b/plugins/README.md index 1d243daa..3b8f5508 100644 --- a/plugins/README.md +++ b/plugins/README.md @@ -9,6 +9,7 @@ | 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 | | 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) | | nmount | sh | pmount, udisks2 | Toggle mount status of a device as normal user | diff --git a/plugins/md5sum b/plugins/md5sum new file mode 100755 index 00000000..0ba8a1b7 --- /dev/null +++ b/plugins/md5sum @@ -0,0 +1,36 @@ +#!/usr/bin/env sh + +# Description: Create and verify md5 checksums +# +# If selection is used: it will generate one md5 file containing the checksums and file names +# [with paths if they are in another directory] +# If file is used: if the file is .md5 file, then it does the check +# if the file is not .md5 file, it creates the md5 file from it +# +# Shell: POSIX compliant +# Author: ath3 + +selection=~/.config/nnn/.selection +resp=f + +if [ -s "$selection" ]; then + echo -n "work with selection (s) or current file (f) [default=f]: " + read resp +fi + +if [ "$resp" = "s" ]; then + arr=$(cat $selection | tr '\0' '\n') + { read -r file; } <<< "$arr" + file=$(basename "$file").md5 + cat "$selection" | sed 's|'"$PWD/"'||g' | xargs -0 -i md5sum {} > "$file" +else + if ! [ -z "$1" ] && [ -f "$1" ]; then + if [ $(echo $1 | grep \.md5$) ]; then + cat "$1" | md5sum -c + read + else + file=$(basename "$1").md5 + md5sum "$1" > "$file" + fi + fi +fi From c499f1cb7fa5b44299bd8e4d7635c67006232cca Mon Sep 17 00:00:00 2001 From: ath3 Date: Wed, 19 Jun 2019 13:40:31 +0200 Subject: [PATCH 2/4] Changed bash code to sh --- plugins/md5sum | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/plugins/md5sum b/plugins/md5sum index 0ba8a1b7..76a2571d 100755 --- a/plugins/md5sum +++ b/plugins/md5sum @@ -19,9 +19,7 @@ if [ -s "$selection" ]; then fi if [ "$resp" = "s" ]; then - arr=$(cat $selection | tr '\0' '\n') - { read -r file; } <<< "$arr" - file=$(basename "$file").md5 + file=$(basename "$(cat $selection | tr '\0' '\n' | head -n 1)").md5 cat "$selection" | sed 's|'"$PWD/"'||g' | xargs -0 -i md5sum {} > "$file" else if ! [ -z "$1" ] && [ -f "$1" ]; then From 682e810e6e893a1b8824a9eef032ff1fea9eaafc Mon Sep 17 00:00:00 2001 From: ath3 Date: Thu, 20 Jun 2019 03:39:23 +0200 Subject: [PATCH 3/4] Extended md5sum script to support multiple checksums and renamed it to checksum --- plugins/checksum | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ plugins/md5sum | 34 ------------------------- 2 files changed, 64 insertions(+), 34 deletions(-) create mode 100755 plugins/checksum delete mode 100755 plugins/md5sum diff --git a/plugins/checksum b/plugins/checksum new file mode 100755 index 00000000..44f2274c --- /dev/null +++ b/plugins/checksum @@ -0,0 +1,64 @@ +#!/usr/bin/env sh + +# Description: Create and verify checksums +# +# If selection is used: it will generate one file containing the checksums with file names +# [and with paths if they are in another directory] +# The filename will be checksum_timestamp.checksum_type +# If file is used: if the file is a checksum, it does the verification +# if the file is not a checksum, it will be created from it +# The filename will be filename.checksum_type +# +# Shell: POSIX compliant +# Author: ath3 + +selection=~/.config/nnn/.selection +resp=f +chsum=md5 +ischksum=0 + +checksum_type() +{ + echo "possible checksums: md5, sha1, sha224, sha256, sha384, sha512" + echo -n "create md5 (m), sha256 (s), sha512 (S) (or type one of the above checksums) [default=m]: " + read chsum_resp + for chks in md5 sha1 sha224 sha256 sha384 sha512 + do + if [ "$chsum_resp" = "$chks" ]; then + chsum=$chsum_resp + return + fi + done + if [ "$chsum_resp" = "s" ]; then + chsum=sha256 + elif [ "$chsum_resp" = "S" ]; then + chsum=sha512 + fi +} + +if [ -s "$selection" ]; then + echo -n "work with selection (s) or current file (f) [default=f]: " + read resp +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" +else + if [ -n "$1" ] && [ -f "$1" ]; then + for chks in md5 sha1 sha224 sha256 sha384 sha512 + do + if [ "$(echo "$1" | grep \.${chks}$)" ]; then + ischksum=1 + ${chks}sum -c < "$1" + read + exit + fi + done + if [ $ischksum -eq 0 ]; then + checksum_type + file=$(basename "$1").$chsum + ${chsum}sum "$1" > "$file" + fi + fi +fi diff --git a/plugins/md5sum b/plugins/md5sum deleted file mode 100755 index 76a2571d..00000000 --- a/plugins/md5sum +++ /dev/null @@ -1,34 +0,0 @@ -#!/usr/bin/env sh - -# Description: Create and verify md5 checksums -# -# If selection is used: it will generate one md5 file containing the checksums and file names -# [with paths if they are in another directory] -# If file is used: if the file is .md5 file, then it does the check -# if the file is not .md5 file, it creates the md5 file from it -# -# Shell: POSIX compliant -# Author: ath3 - -selection=~/.config/nnn/.selection -resp=f - -if [ -s "$selection" ]; then - echo -n "work with selection (s) or current file (f) [default=f]: " - read resp -fi - -if [ "$resp" = "s" ]; then - file=$(basename "$(cat $selection | tr '\0' '\n' | head -n 1)").md5 - cat "$selection" | sed 's|'"$PWD/"'||g' | xargs -0 -i md5sum {} > "$file" -else - if ! [ -z "$1" ] && [ -f "$1" ]; then - if [ $(echo $1 | grep \.md5$) ]; then - cat "$1" | md5sum -c - read - else - file=$(basename "$1").md5 - md5sum "$1" > "$file" - fi - fi -fi From 2dc3da62a2fe8672393c20ceeb1792fb0560f333 Mon Sep 17 00:00:00 2001 From: ath3 Date: Thu, 20 Jun 2019 10:45:24 +0200 Subject: [PATCH 4/4] Refactored splitjoin and ndiff plugins, made them POSIX compliant --- plugins/README.md | 6 +++--- plugins/ndiff | 32 +++++++++++++++++--------------- plugins/splitjoin | 36 ++++++++++++++++++++++++------------ 3 files changed, 44 insertions(+), 30 deletions(-) diff --git a/plugins/README.md b/plugins/README.md index 3b8f5508..7ab679dd 100644 --- a/plugins/README.md +++ b/plugins/README.md @@ -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 | diff --git a/plugins/ndiff b/plugins/ndiff index e76b151c..59193ffc 100755 --- a/plugins/ndiff +++ b/plugins/ndiff @@ -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 ' "$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