Name: Add name_make_numbering.

This commit is contained in:
Dmitry Voronin 2025-02-05 19:30:36 +03:00
parent 6bd60a997e
commit c65a5ae8c6
Signed by: voronind
SSH key fingerprint: SHA256:3kBb4iV2ahufEBNq+vFbUe4QYfHt98DHQjN7QaptY9k

View file

@ -389,6 +389,45 @@ function name_fix_numbering() {
_iterate_targets process ${targets[@]}
}
# Create numbering for unnumbered files.
# Usage: name_make_numbering [FILES]
function name_make_numbering() {
local IFS=$'\n'
local power=0
local targets=(${@})
[[ ${targets} == "" ]] && targets=(*)
# Count leading zeroes.
count=${#targets[@]}
while [ ${count} -gt 0 ]; do
((power++))
count=$((count / 10))
done
local iter=0
process() {
((iter++))
# Prepare new name.
local digits=($(parse_ints "${target}"))
local digit="${digits[0]}"
local new_name=$(printf "%0${power}d" "${iter}")"_${target#${digit}_}"
# Skip if the same name.
[[ ${target} == "${new_name}" ]] && return 0
# Check that file does not exist.
if [[ -e ${new_name} ]]; then
_error "${new_name}: File exists!"
return 1
fi
mv -- ${target} ${new_name} && echo ${new_name}
}
_iterate_targets process ${targets[@]}
}
function _comp_name_parse() {
_autocomplete $(find_function | grep ^parse)
}