mirror of
https://github.com/jarun/nnn.git
synced 2024-11-24 11:51:27 +00:00
b0aa1fe379
* plugins: cleanfilename: fixed to not overwrite existing file * plugins: cleanfilename: fixed to not overwrite existing file
60 lines
1.2 KiB
Bash
Executable file
60 lines
1.2 KiB
Bash
Executable file
#!/usr/bin/env sh
|
|
|
|
# Description: Clean filename or dirname (either hovered or selections)
|
|
# to be more shell-friendly.
|
|
# This script replaces any non A-Za-z0-9._- char
|
|
# with underscore (_).
|
|
#
|
|
# LIMITATION: Won't work with filename that contains newline
|
|
#
|
|
# 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
|
|
tmp=''
|
|
if [ -e "$(cleanup "$i")" ]; then
|
|
tmp='_'
|
|
fi
|
|
mv "$i" "$tmp$(cleanup "$i")";
|
|
fi
|
|
done
|