mirror of
https://github.com/jarun/nnn.git
synced 2024-11-24 11:51:27 +00:00
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:
parent
1c2cb7fd31
commit
c61a716e11
|
@ -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
56
plugins/cleanfilename
Executable 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
|
Loading…
Reference in a new issue