bash : EXPERIMENTAL : add support for filenames with spaces.

This commit is contained in:
Dmitry Voronin 2023-11-22 13:20:01 +03:00
parent 3bc8f5b3b3
commit 4f68139a17
5 changed files with 33 additions and 10 deletions

View file

@ -8,6 +8,7 @@ _ARCHIVE_DATE()
# usage: archive [FILES] # usage: archive [FILES]
archive() archive()
{ {
local IFS=$'\n'
local targets=("${@}") # target file(s). local targets=("${@}") # target file(s).
local count=0 # processed count. local count=0 # processed count.
local total=${#} # total to process. local total=${#} # total to process.
@ -15,7 +16,7 @@ archive()
# Set dafult value to target all directories. # Set dafult value to target all directories.
if [[ "${targets}" = "" ]]; then if [[ "${targets}" = "" ]]; then
targets=($(ls --classify | grep /$)) targets=($(ls --classify | grep /\$))
total=${#targets[@]} total=${#targets[@]}
fi fi
@ -43,6 +44,7 @@ archive()
# usage: archive_fast [FILES] # usage: archive_fast [FILES]
archive_fast() archive_fast()
{ {
local IFS=$'\n'
local targets=("${@}") # target file(s). local targets=("${@}") # target file(s).
local count=0 # processed count. local count=0 # processed count.
local total=${#} # total to process. local total=${#} # total to process.
@ -78,6 +80,7 @@ archive_fast()
# usage: archive_check [FILES] # usage: archive_check [FILES]
archive_check() archive_check()
{ {
local IFS=$'\n'
local targets=("${@}") # target file(s). local targets=("${@}") # target file(s).
local total=${#} # total to process. local total=${#} # total to process.
local count=0 # processed count. local count=0 # processed count.
@ -128,6 +131,7 @@ archive_check()
# Usage: archive_prune [NAME] # Usage: archive_prune [NAME]
archive_prune() archive_prune()
{ {
local IFS=$'\n'
local targets=("${@}") local targets=("${@}")
local count=0 local count=0
local total=${#} local total=${#}
@ -155,6 +159,7 @@ archive_prune()
# usage: unarchive [FILES] # usage: unarchive [FILES]
unarchive() unarchive()
{ {
local IFS=$'\n'
local targets=("${@}") # target file(s). local targets=("${@}") # target file(s).
local count=0 # processed count. local count=0 # processed count.
local total=${#} # total to process. local total=${#} # total to process.
@ -212,6 +217,7 @@ unarchive()
# usage: archive_name [ARCHIVE] [NAME] # usage: archive_name [ARCHIVE] [NAME]
archive_name() archive_name()
{ {
local IFS=$'\n'
local targets="${1}" # target archive(s). local targets="${1}" # target archive(s).
local name="${2}" # new name. local name="${2}" # new name.
local total=1 # total targets to process. local total=1 # total targets to process.
@ -264,6 +270,7 @@ archive_name()
# convert an old archive to a new format. TODO: remove me after some time when there won't be any old archives. # convert an old archive to a new format. TODO: remove me after some time when there won't be any old archives.
archive_convert() archive_convert()
{ {
local IFS=$'\n'
local old_format="_[[:alnum:]]{40}.tar.[xg]z" local old_format="_[[:alnum:]]{40}.tar.[xg]z"
local targets=($(ls | grep -E ${old_format})) local targets=($(ls | grep -E ${old_format}))
@ -311,6 +318,7 @@ export _ARCHIVE_PATTERN
# autocomplete. # autocomplete.
_archive_name() _archive_name()
{ {
local IFS=$'\n'
COMPREPLY=() COMPREPLY=()
local cur="${COMP_WORDS[COMP_CWORD]}" local cur="${COMP_WORDS[COMP_CWORD]}"
@ -332,5 +340,5 @@ _archive_grep()
_autocomplete_grep ${_ARCHIVE_PATTERN} _autocomplete_grep ${_ARCHIVE_PATTERN}
} }
complete -F _archive_grep archive_check unarchive complete -o filenames -F _archive_grep archive_check unarchive
complete -F _archive_name archive_name complete -o filenames -F _archive_name archive_name

View file

@ -3,6 +3,7 @@
# there are also options like -o nospace. see man for more info. # there are also options like -o nospace. see man for more info.
_autocomplete() _autocomplete()
{ {
local IFS=$'\n'
local commands="${@}" local commands="${@}"
COMPREPLY=() COMPREPLY=()
@ -18,6 +19,7 @@ _autocomplete()
# autocomplete only first argument. # autocomplete only first argument.
_autocomplete_first() _autocomplete_first()
{ {
local IFS=$'\n'
local commands="${@}" local commands="${@}"
COMPREPLY=() COMPREPLY=()
@ -32,8 +34,10 @@ _autocomplete_first()
fi fi
} }
# Autocomplete by grepping file names.
_autocomplete_grep() _autocomplete_grep()
{ {
local IFS=$'\n'
COMPREPLY=() COMPREPLY=()
local pattern="${1}" local pattern="${1}"
@ -48,6 +52,7 @@ _autocomplete_grep()
# autocomplete nested program. # autocomplete nested program.
_autocomplete_nested() _autocomplete_nested()
{ {
# local IFS=$'\n'
local cur prev words cword split i local cur prev words cword split i
_init_completion -s || return _init_completion -s || return

View file

@ -2,6 +2,7 @@
# Usage: convert <FORMAT> [FILES] # Usage: convert <FORMAT> [FILES]
convert() convert()
{ {
local IFS=$'\n'
local targets=("${@:2}") local targets=("${@:2}")
local format="${1}" local format="${1}"
local total=${#targets[@]} local total=${#targets[@]}
@ -83,7 +84,7 @@ _convert_gz-xz()
_convert_xz-gz() _convert_xz-gz()
{ {
pv "${1}" | xz -d | gzip -1 > "${1.xz}.gz" pv "${1}" | xz -d | gzip -1 > "${1%.xz}.gz"
} }
_convert_mp3() _convert_mp3()

View file

@ -2,6 +2,7 @@
# usage: name [FILES] # usage: name [FILES]
name() name()
{ {
local IFS=$'\n'
local targets=("${@}") # target file(s). local targets=("${@}") # target file(s).
local count=0 # processed count. local count=0 # processed count.
local total=${#} # total to process. local total=${#} # total to process.
@ -60,6 +61,7 @@ name()
# usage: name_hash [FILES] # usage: name_hash [FILES]
name_hash() name_hash()
{ {
local IFS=$'\n'
local targets=("${@}") # target file(s). local targets=("${@}") # target file(s).
local count=0 # processed counter. local count=0 # processed counter.
local total=${#} # total to process. local total=${#} # total to process.
@ -117,6 +119,7 @@ name_hash()
# usage: name_hash_check [FILES] # usage: name_hash_check [FILES]
name_hash_check() name_hash_check()
{ {
local IFS=$'\n'
local targets=("${@}") # target file(s). local targets=("${@}") # target file(s).
local total=${#} # total to process. local total=${#} # total to process.
local count=0 # processed counter. local count=0 # processed counter.
@ -172,6 +175,7 @@ name_hash_check()
# usage: name_series <SEASON> [FILES] # usage: name_series <SEASON> [FILES]
name_series() name_series()
{ {
local IFS=$'\n'
local season="${1}" # Season number. local season="${1}" # Season number.
local targets=("${@:2}") # Target files. local targets=("${@:2}") # Target files.
local count=0 # Processed counter. local count=0 # Processed counter.
@ -229,6 +233,7 @@ name_series()
# usage: name_manga <SEASON> [FILES] # usage: name_manga <SEASON> [FILES]
name_manga() name_manga()
{ {
local IFS=$'\n'
local season="${1}" # Season number. local season="${1}" # Season number.
local targets=("${@:2}") # Target files. local targets=("${@:2}") # Target files.
local count=0 # Processed counter. local count=0 # Processed counter.
@ -287,6 +292,7 @@ name_manga()
# usage: name_ext <EXTENSION> [FILES] # usage: name_ext <EXTENSION> [FILES]
name_ext() name_ext()
{ {
local IFS=$'\n'
local extension="${1}" # new extension. local extension="${1}" # new extension.
local targets=("${@:2}") # target file(s). local targets=("${@:2}") # target file(s).
local count=0 # processed counter. local count=0 # processed counter.
@ -342,6 +348,7 @@ name_ext()
# Usage: name_prefix [OLD] [NEW] [FILES] # Usage: name_prefix [OLD] [NEW] [FILES]
name_prefix() name_prefix()
{ {
local IFS=$'\n'
local old="${1}" # Old prefix. local old="${1}" # Old prefix.
local new="${2}" # New prefix. local new="${2}" # New prefix.
local targets=("${@:3}") # Target files. local targets=("${@:3}") # Target files.

View file

@ -4,6 +4,7 @@ _UNPACK_SUPPORTED=".tar$|.tgz$|.txz$|.tar.gz$|.tar.xz$|.zip$|.iso$|.rar$"
# Usage: pack <TARGET.ext> [FILES] # Usage: pack <TARGET.ext> [FILES]
pack() pack()
{ {
local IFS=$'\n'
local output="${1}" local output="${1}"
local targets=("${@:2}") local targets=("${@:2}")
local format="${output##*.}" local format="${output##*.}"
@ -30,22 +31,22 @@ pack()
# process. # process.
case "${format}" in case "${format}" in
"tgz") "tgz")
_pack_tgz "${@}" _pack_tgz "${output}" "${targets[@]}"
;; ;;
"txz") "txz")
_pack_txz "${@}" _pack_txz "${output}" "${targets[@]}"
;; ;;
"tar") "tar")
_pack_tar "${@}" _pack_tar "${output}" "${targets[@]}"
;; ;;
"zip") "zip")
_pack_zip "${@}" _pack_zip "${output}" "${targets[@]}"
;; ;;
"gz") "gz")
_pack_gz "${@}" _pack_gz "${output}" "${targets[@]}"
;; ;;
"xz") "xz")
_pack_xz "${@}" _pack_xz "${output}" "${targets[@]}"
esac esac
# actions on error. # actions on error.
@ -60,6 +61,7 @@ pack()
# usage: unpack [FILES] # usage: unpack [FILES]
unpack() unpack()
{ {
local IFS=$'\n'
local targets=("${@}") local targets=("${@}")
local count=0 local count=0
local failed=0 local failed=0