plugin: cleanfilename: clean filename to be more shell-friendly (#913)

* 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
This commit is contained in:
Benawi Adha 2021-03-24 16:21:38 +07:00 committed by GitHub
parent 1c2cb7fd31
commit c61a716e11
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 0 deletions

View 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 |

56
plugins/cleanfilename Executable file
View file

@ -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