aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/splitjoin
diff options
context:
space:
mode:
authorGravatar ath3 <ha05190@protonmail.com>2019-06-20 10:45:24 +0200
committerGravatar ath3 <ha05190@protonmail.com>2019-06-20 10:45:24 +0200
commit2dc3da62a2fe8672393c20ceeb1792fb0560f333 (patch)
tree30075b6a1ac603c8d4c0548c0cbb058eb6c65718 /plugins/splitjoin
parent682e810e6e893a1b8824a9eef032ff1fea9eaafc (diff)
downloadnnn-2dc3da62a2fe8672393c20ceeb1792fb0560f333.tar.gz
Refactored splitjoin and ndiff plugins, made them POSIX compliant
Diffstat (limited to 'plugins/splitjoin')
-rwxr-xr-xplugins/splitjoin36
1 files changed, 24 insertions, 12 deletions
diff --git a/plugins/splitjoin b/plugins/splitjoin
index cbb573e..4325d3a 100755
--- a/plugins/splitjoin
+++ b/plugins/splitjoin
@@ -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