]> Sergey Matveev's repositories - nnn.git/commitdiff
plugin: cleanfilename: clean filename to be more shell-friendly (#913)
authorBenawi Adha <43810055+wustho@users.noreply.github.com>
Wed, 24 Mar 2021 09:21:38 +0000 (16:21 +0700)
committerGitHub <noreply@github.com>
Wed, 24 Mar 2021 09:21:38 +0000 (14:51 +0530)
* plugins: cleanfilename: clean filename to be more shell-friendly

* plugins: cleanfilename: clean filename to be more shell-friendly

* plugins: cleanfilename: clean filename to be more shell-friendly

* plugins: cleanfilename: clean filename to be more shell-friendly

* plugins: cleanfilename: clean filename to be more shell-friendly

plugins/README.md
plugins/cleanfilename [new file with mode: 0755]

index c4a204daee91cc05c9f93113edfd26c53edfc45e..fdb0295b1d39a6cf66ec75f0fcc885737e463bbe 100644 (file)
@@ -20,6 +20,7 @@ Plugins extend the capabilities of `nnn`. They are _executable_ scripts (or bina
 | [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 |
+| [cleanfilename](cleanfilename) | Clean filename to be more shell-friendly | sh | sed |
 | [dups](dups) | List non-empty duplicate files in current dir | bash | find, md5sum,<br>sort uniq xargs |
 | [chksum](chksum) | Create and verify checksums | sh | md5sum,<br>sha256sum |
 | [diffs](diffs) | Diff for selection (limited to 2 for directories) | sh | vimdiff, mktemp |
diff --git a/plugins/cleanfilename b/plugins/cleanfilename
new file mode 100755 (executable)
index 0000000..924d5d0
--- /dev/null
@@ -0,0 +1,56 @@
+#!/usr/bin/env sh
+
+# Description: Clean filename (either hovered or selections)
+#              to be more shell-friendly.
+#              This script replaces any non A-Za-z0-9._- char
+#              with underscore (_).
+#
+#              Although the name is 'cleanfilename', but it should work
+#              on directories too.
+#
+# Dependencies: sed
+#
+# Shell: POSIX compliant
+# Author: Benawi Adha
+
+prompt=true
+sel=${NNN_SEL:-${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection}
+IFS='
+'
+
+cleanup() {
+    printf "%s" "$1" | sed -e 's/[^A-Za-z0-9._-]/_/g'
+}
+
+if [ -f "$sel" ]; then
+    targets=$(sed -e "s/\\x0/\\n/g;\$a\\" "$sel" | \
+        while read -r i; do
+            basename "$i";
+        done)
+else
+    targets=$1
+fi
+
+for i in $targets; do
+    printf "%s --> %s\n" "$i" "$(cleanup "$i")";
+done
+
+if $prompt; then
+    echo
+    printf "Proceed [Yn]? "
+    read -r input
+    case "$input" in
+        y|Y|'')
+            ;;
+        *)
+            echo "Canceled"
+            exit
+            ;;
+    esac
+fi
+
+for i in $targets; do
+    if [ "$i" != "$(cleanup "$i")" ]; then
+        mv "$i" "$(cleanup "$i")";
+    fi
+done