mirror of
https://github.com/jarun/nnn.git
synced 2024-11-24 11:51:27 +00:00
plugins: cleanfilename: sh->bash and added support for cleaning newline (#923)
* plugins: cleanfilename: sh->bash and added support for cleaning newline * plugins: cleanfilename: sh->bash and added support for cleaning newline
This commit is contained in:
parent
37fbed2fb7
commit
0f4dfee6c6
|
@ -1,37 +1,48 @@
|
||||||
#!/usr/bin/env sh
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
# Description: Clean filename or dirname (either hovered or selections)
|
# Description: Clean filename or dirname (either hovered or selections)
|
||||||
# to be more shell-friendly.
|
# to be more shell-friendly. This script cleans
|
||||||
# This script replaces any non A-Za-z0-9._- char
|
# any character which is not A-Za-z0-9._-
|
||||||
# with underscore (_).
|
# and replaces it with underscore (_).
|
||||||
#
|
#
|
||||||
# LIMITATION: Won't work with filename that contains newline
|
# It supports cleaning single/double quote, newline,
|
||||||
|
# leading, trailing spaces.
|
||||||
|
#
|
||||||
|
# eg.
|
||||||
|
# to be continued (つづく).mp4 -> to_be_continued______.mp4
|
||||||
|
# [work] stuff.txt -> _work__stuff.txt
|
||||||
|
# home's server -> home_s_server
|
||||||
|
# qwe\trty -> __qwe_rty
|
||||||
|
#
|
||||||
|
# And if there are two almost similar filenames
|
||||||
|
# like: 'asd]f' and 'asd f' which both will be renamed to 'asd_f',
|
||||||
|
# to avoid overwriting, the last file will be prepended by _.
|
||||||
|
# So they will be: 'asd_f' and '_asd_f'
|
||||||
#
|
#
|
||||||
# Dependencies: sed
|
# Dependencies: sed
|
||||||
#
|
#
|
||||||
# Shell: POSIX compliant
|
# Shell: Bash
|
||||||
# Author: Benawi Adha
|
# Author: Benawi Adha
|
||||||
|
|
||||||
prompt=true
|
prompt=true
|
||||||
sel=${NNN_SEL:-${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection}
|
sel=${NNN_SEL:-${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection}
|
||||||
IFS='
|
|
||||||
'
|
|
||||||
|
|
||||||
cleanup() {
|
cleanup() {
|
||||||
printf "%s" "$1" | sed -e 's/[^A-Za-z0-9._-]/_/g'
|
# printf "%s" "$1" | sed -e 's/[^A-Za-z0-9._-]/_/g'
|
||||||
|
printf "%s" "$1" | sed 's/[^A-Za-z0-9._-]/_/g' | sed ':a;N;$!ba;s/\n/_/g'
|
||||||
}
|
}
|
||||||
|
|
||||||
if [ -s "$sel" ]; then
|
if [ -s "$sel" ]; then
|
||||||
targets=$(sed -e "s/\\x0/\\n/g;\$a\\" "$sel" | \
|
targets=()
|
||||||
while read -r i; do
|
while IFS= read -r -d '' i || [ -n "$i" ]; do
|
||||||
basename "$i";
|
targets+=( "$(basename "$i")" )
|
||||||
done)
|
done < "$sel"
|
||||||
else
|
else
|
||||||
targets=$1
|
targets=("$1")
|
||||||
fi
|
fi
|
||||||
|
|
||||||
for i in $targets; do
|
for i in "${targets[@]}"; do
|
||||||
printf "%s --> %s\n" "$i" "$(cleanup "$i")";
|
printf "%s -> %s\n" "$i" "$(cleanup "$i")";
|
||||||
done
|
done
|
||||||
|
|
||||||
if $prompt; then
|
if $prompt; then
|
||||||
|
@ -48,7 +59,7 @@ if $prompt; then
|
||||||
esac
|
esac
|
||||||
fi
|
fi
|
||||||
|
|
||||||
for i in $targets; do
|
for i in "${targets[@]}"; do
|
||||||
if [ "$i" != "$(cleanup "$i")" ]; then
|
if [ "$i" != "$(cleanup "$i")" ]; then
|
||||||
tmp=''
|
tmp=''
|
||||||
if [ -e "$(cleanup "$i")" ]; then
|
if [ -e "$(cleanup "$i")" ]; then
|
||||||
|
|
Loading…
Reference in a new issue