2021-03-24 09:21:38 +00:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
|
2021-03-25 19:36:25 +00:00
|
|
|
# Description: Clean filename or dirname (either hovered or selections)
|
2021-03-24 09:21:38 +00:00
|
|
|
# to be more shell-friendly.
|
|
|
|
# This script replaces any non A-Za-z0-9._- char
|
|
|
|
# with underscore (_).
|
|
|
|
#
|
2021-03-25 19:36:25 +00:00
|
|
|
# LIMITATION: Won't work with filename that contains newline
|
2021-03-24 09:21:38 +00:00
|
|
|
#
|
|
|
|
# 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'
|
|
|
|
}
|
|
|
|
|
2021-03-27 11:37:58 +00:00
|
|
|
if [ -s "$sel" ]; then
|
2021-03-24 09:21:38 +00:00
|
|
|
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
|
2021-03-25 19:36:25 +00:00
|
|
|
tmp=''
|
|
|
|
if [ -e "$(cleanup "$i")" ]; then
|
|
|
|
tmp='_'
|
|
|
|
fi
|
|
|
|
mv "$i" "$tmp$(cleanup "$i")";
|
2021-03-24 09:21:38 +00:00
|
|
|
fi
|
|
|
|
done
|
2021-03-27 11:37:58 +00:00
|
|
|
|
|
|
|
# Clear selection
|
|
|
|
if [ -s "$sel" ] && [ -p "$NNN_PIPE" ]; then
|
|
|
|
printf "-" > "$NNN_PIPE"
|
|
|
|
fi
|