nnn/plugins/cleanfilename
Benawi Adha c61a716e11
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
2021-03-24 14:51:38 +05:30

57 lines
1.1 KiB
Bash
Executable file

#!/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