From 72d904dfa2ef67e8cdc96d830ba1dc928cdc9ea7 Mon Sep 17 00:00:00 2001 From: home Date: Sun, 17 Dec 2023 16:27:21 +0300 Subject: [PATCH] Name : utilize _iterate_target. --- .config/bash/module/archive.sh | 64 +++-- .config/bash/module/name.sh | 442 ++++++++------------------------- .config/bash/module/util.sh | 18 +- 3 files changed, 165 insertions(+), 359 deletions(-) diff --git a/.config/bash/module/archive.sh b/.config/bash/module/archive.sh index 2669757..e1f8a59 100644 --- a/.config/bash/module/archive.sh +++ b/.config/bash/module/archive.sh @@ -10,7 +10,6 @@ function archive() { [[ "${targets}" = "" ]] && targets=($(_ls_dir)) process() { - local target=${1} local date=$(_archive_date) # Parse name. @@ -20,7 +19,8 @@ function archive() { local hash=$(tar -c ${target} | pv -s $(/usr/bin/du -sb ${target} | awk '{print $1}') | xz -9e | tee ${name}.txz | sha1sum | cut -d\ -f1) # append hash to target name. - mv ${name}.txz ${name}_${date}-${hash}.txz + local new_name="${name}_${date}-${hash}.txz" + mv -- ${name}.txz ${new_name} && echo ${new_name} } _iterate_targets process ${targets[@]} @@ -35,7 +35,7 @@ function archive_fast() { [[ "${targets}" = "" ]] && targets=($(_ls_dir)) process() { - local target="${1}" + # Start timestamp. local date=$(_archive_date) # Parse name. @@ -45,7 +45,8 @@ function archive_fast() { local hash=$(tar -c "${target}" | pv -s $(/usr/bin/du -sb "${target}" | awk '{print $1}') | gzip -1 | tee "${name}".tgz | sha1sum | cut -d\ -f1) # append hash to target name. - mv "${name}".tgz "${name}"_${date}-${hash}.tgz + local new_name="${name}_${date}-${hash}.tgz" + mv -- "${name}".tgz ${new_name} && echo ${new_name} } _iterate_targets process ${targets[@]} @@ -60,8 +61,6 @@ function archive_check() { [[ "${targets}" = "" ]] && targets=($(ls | grep -E ${_archive_pattern})) process() { - local target=${1} - # extract hash from name. local data=($(_archive_parse ${target})) local saved=${data[2]} @@ -96,8 +95,7 @@ function archive_prune() { local copy_time=${copy_data[1]} if [[ "${copy_time}" -lt "${time}" ]]; then - echo -e "${name}: Prune ${copy_time}." - rm -- ${copy} + rm -- ${copy} && echo "${name}: Prune ${copy_time}." fi done } @@ -113,7 +111,7 @@ function archive_rm() { [[ "${targets}" = "" ]] && targets=($(ls | grep -E ${_archive_pattern})) process() { - rm -- "${1}" + rm -- "${target}" } _iterate_targets process ${targets[@]} @@ -123,17 +121,16 @@ function archive_rm() { # Usage: archive_xz [FILES] function archive_xz() { local IFS=$'\n' - local targets=("${@}") + local targets=(${@}) [[ "${targets}" = "" ]] && targets=$(ls | grep -E ${_archive_pattern_fast}) process() { - local target="${1}" local data=($(_archive_parse "${target}")) local tmp="${data[0]}.txz" # Check that old format. if [[ "${data[3]}" != "tgz" ]]; then - echo -e "${color_bred}Not in .tgz format!${color_default}" + _error "Not in .tgz format!" return 1 fi @@ -141,7 +138,45 @@ function archive_xz() { local hash=$(pv "${target}" | gzip -d | xz -9e | tee "${tmp}" | sha1sum | cut -d\ -f1) # Rename. - mv -- "${tmp}" "${data[0]}_${data[1]}-${hash}.txz" && rm "${target}" + local new_name="${data[0]}_${data[1]}-${hash}.txz" + mv -- ${tmp} ${new_name} && rm ${target} && echo ${new_name} + } + + _iterate_targets process ${targets[@]} +} + +# Rename archives. +# If no name specified, it simplifies archive's name. +# If no archives specified, apply to all archives. +# Usage: archive_name [ARCHIVE] [NAME] +function archive_name() { + local IFS=$'\n' + local targets="${1}" + local name="${2}" + [[ "${targets}" = "" ]] && targets=($(ls | grep -E ${_archive_pattern})) + + process() { + # simplify name by default. + if [[ "${name}" = "" || ${count} -gt 1 ]]; then + name="${target%_*}" + name="$(parse_camel ${name})" + fi + + # remove old name. + local data="${target##*_}" + local new_name="${name}_${data}" + + # check for the same name. + [[ "${target}" = "${new_name}" ]] && return 0 + + # check for existing target. + if [[ -f "${new_name}" ]]; then + _error "${new_name}: Already exists!" + return 1 + fi + + # rename. + mv -- ${target} ${new_name} && echo ${new_name} } _iterate_targets process ${targets[@]} @@ -173,7 +208,8 @@ function unarchive() { ;; esac else - false + _error "Validation failed!" + return 1 fi } diff --git a/.config/bash/module/name.sh b/.config/bash/module/name.sh index 15b71c4..ffc1151 100644 --- a/.config/bash/module/name.sh +++ b/.config/bash/module/name.sh @@ -3,22 +3,10 @@ # Usage: name [FILES] function name() { local IFS=$'\n' - local targets=("${@}") - local count=0 - local total=${#} - local failed=0 - - # all targets except hidden by default. - if [[ "${targets}" = "" ]]; then - targets=([^.]*) - total=${#targets[@]} - fi - - # process. - for target in "${targets[@]}"; do - # increment counter. - ((count++)) + local targets=(${@}) + [[ "${targets}" = "" ]] && targets=([^.]*) + process() { # parse new name. local ext="" local name="${target}" @@ -35,32 +23,20 @@ function name() { # Get new name. local new_name=$(parse_simplify "${name}")${ext,,} - # status line. - local status="[${count}/${total}] ${target} -> ${new_name}" - echo -e "${color_bwhite}${status}${color_default}" - # check if same name. - [[ "${target}" = "${new_name}" ]] && continue + [[ "${target}" = "${new_name}" ]] && return 0 # check if target name already exists. if [[ -f "${new_name}" ]]; then - echo -e "${color_bred}${status}: already exists!${color_default}" - ((failed++)) + _error "${new_name}: Already exists!" + return 1 fi # rename target. mv -- "${target}" "${new_name}" + } - if [[ ${?} != 0 ]]; then - echo -e "${color_bred}${status}: Failed.${color_default}" - ((failed++)) - fi - done - - if [[ ${failed} != 0 ]]; then - echo -e "${color_bred}Failed: ${failed}.${color_default}" - false - fi + _iterate_targets process ${targets[@]} } # Rename all files to their hashes while keeping extensions. @@ -68,22 +44,10 @@ function name() { # Usage: name_hash [FILES] function name_hash() { local IFS=$'\n' - local targets=("${@}") - local count=0 - local total=${#} - local failed=0 - - # all files except hidden by default. - if [[ "${targets}" = "" ]]; then - targets=($(ls --classify | grep -v /\$)) - total=${#targets[@]} - fi - - # process. - for target in "${targets[@]}"; do - # increment count. - ((count++)) + local targets=(${@}) + [[ "${targets}" = "" ]] && targets=($(_ls_file)) + process() { # extract extension. local extension="${target##*.}" if [[ "${extension}" = "${target}" ]]; then @@ -96,32 +60,14 @@ function name_hash() { local hash=$(pv "${target}" | sha1sum | cut -d\ -f1) new_name="${hash,,}${extension,,}" - # prepare status. - local status="[${count}/${total}] ${target} -> ${new_name}" - # check if same name. - if [[ "${target}" = "${new_name}" ]]; then - # echo -e "${color_green}${status}: No change.${color_default}" - echo -e "${color_bwhite}${status}${color_default}" - continue - fi - - # show change. - echo -e "${color_bwhite}${status}${color_default}" + [[ "${target}" = "${new_name}" ]] && return 0 # rename target. - mv -- "${target}" "${new_name}" + mv -- ${target} ${new_name} && echo ${new_name} + } - if [[ ${?} != 0 ]]; then - echo -e "${color_bred}${status}: Failed.${color_default}" - ((failed++)) - fi - done - - if [[ ${failed} != 0 ]]; then - echo -e "${color_bred}Failed: ${failed}.${color_default}" - false - fi + _iterate_targets process ${targets[@]} } # Check hashes for previously renamed files. @@ -129,42 +75,22 @@ function name_hash() { # Usage: name_hash_check [FILES] function name_hash_check() { local IFS=$'\n' - local targets=("${@}") - local total=${#} - local count=0 - local failed=0 - - # all targets by default. - if [[ "${targets}" = "" ]]; then - targets=($(ls --classify | grep -v /\$)) - total=${#targets[@]} - fi - - # process. - for target in "${targets[@]}"; do - # increment count. - ((count++)) - - # status info. - local status="[${count}/${total}] ${target}" - - echo -e "${color_bwhite}${status}${color_default}" + local targets=(${@}) + [[ "${targets}" = "" ]] && targets=([^.]*) + process() { # extract hashes. local stored="${target%%.*}" local actual=$(pv "${target}" | sha1sum | cut -d\ -f1) # compare hashes. if [[ "${stored}" != "${actual}" ]]; then - echo -e "${color_bred}${status}: Failed.${color_default}" - ((failed++)) + _error "Validation failed!" + return 1 fi - done + } - if [[ ${failed} != 0 ]]; then - echo -e "${color_bred}Failed: ${failed}.${color_default}" - false - fi + _iterate_targets process ${targets[@]} } # Rename files for Jellyfin series, i.e. `Episode S01E01.mkv` @@ -172,54 +98,31 @@ function name_hash_check() { # Usage: name_series [FILES] function name_series() { local IFS=$'\n' - local season="${1}" - local targets=("${@:2}") - local count=0 + local season=${1} local episode=0 - local total=${#} - local failed=0 + local targets=(${@:2}) + [[ "${targets}" = "" ]] && targets=($(_ls_file)) - # error when no season number specified. + # Error when no season number specified. if [[ "${season}" = "" ]]; then echo "usage: name_series [FILES]" return 2 fi - # all targets by default. - if [[ "${targets}" = "" ]]; then - targets=($(ls --classify | grep -v /\$)) - total=${#targets[@]} - fi - - # process. - for target in "${targets[@]}"; do - # increment episode number. - ((count++)) + process() { ((episode++)) # extract new name. local new_name="Episode S${season}E$(printf %02d ${episode}).${target##*.}" - # prepare status. - local status="[${count}/${total}] ${target} -> ${new_name}" - echo -e "${color_bwhite}${status}${color_default}" - - # Warning on no change. - [[ "${target}" = "${new_name}" ]] && continue + # Skip on no change. + [[ "${target}" = "${new_name}" ]] && return 0 # rename target. - mv -- "${target}" "${new_name}" + mv -- ${target} ${new_name} && echo ${new_name} + } - if [[ ${?} != 0 ]]; then - echo -e "${color_bred}${status}: Failed.${color_default}" - ((failed++)) - fi - done - - if [[ ${failed} != 0 ]]; then - echo -e "${color_bred}Failed: ${failed}.${color_default}" - false - fi + _iterate_targets process ${targets[@]} } # Rename files for Kavita manga format. @@ -227,55 +130,32 @@ function name_series() { # Usage: name_manga [FILES] function name_manga() { local IFS=$'\n' - local season="${1}" - local targets=("${@:2}") - local count=0 + local manga=${PWD##*/} + local season=${1} local episode=0 - local total=${#} - local manga="${PWD##*/}" - local failed=0 + local targets=(${@:2}) + [[ "${targets}" = "" ]] && targets=($(_ls_file)) - # error when no season number specified. + # Error when no season number specified. if [[ "${season}" = "" ]]; then echo "usage: name_manga [FILES]" return 2 fi - # all targets by default. - if [[ "${targets}" = "" ]]; then - targets=($(ls --classify | grep -v /\$)) - total=${#targets[@]} - fi - - # process. - for target in "${targets[@]}"; do - # increment episode number. - ((count++)) + process() { ((episode++)) - # extract new name. + # Extract new name. local new_name="${manga} Vol.${season} Ch.${episode}.${target##*.}" - # prepare status. - local status="[${count}/${total}] ${target} -> ${new_name}" - echo -e "${status}" + # Skip on no change. + [[ "${target}" = "${new_name}" ]] && return 0 - # Warning on no change. - [[ "${target}" = "${new_name}" ]] && continue + # Rename target. + mv -- ${target} ${new_name} && echo ${new_name} + } - # rename target. - mv -- "${target}" "${new_name}" - - if [[ ${?} != 0 ]]; then - echo -e "${color_bred}${status}: Failed.${color_default}" - ((failed++)) - fi - done - - if [[ ${failed} != 0 ]]; then - echo -e "${color_bred}Failed: ${failed}.${color_default}" - false - fi + _iterate_targets process ${targets[@]} } # Rename files with new extension. @@ -283,52 +163,28 @@ function name_manga() { # Usage: name_ext [FILES] function name_ext() { local IFS=$'\n' - local extension="${1}" - local targets=("${@:2}") - local count=0 - local total=$((${#}-1)) - local failed=0 + local extension=${1} + local targets=(${@:2}) + [[ "${targets}" = "" ]] && targets=($(_ls_file)) - # error when no new extension specified. + # Error when no new extension specified. if [[ "${extension}" = "" ]]; then help name_ext return 2 fi - # all targets by default. - if [[ "${targets}" = "" ]]; then - targets=($(ls --classify | grep -v /\$)) - total=${#targets[@]} - fi - - # process. - for target in "${targets[@]}"; do - # increment count. - ((count++)) - - # extract new name. + process() { + # Extract new name. local new_name="${target%.*}"."${extension}" - # Prepare status. - local status="[${count}/${total}] ${target} -> ${new_name}" - echo -e "${color_bwhite}${status}${color_default}" + # Skip on no change. + [[ "${target}" = "${new_name}" ]] && return 0 - # Warning on no change. - [[ "${target}" = "${new_name}" ]] && continue + # Rename target. + mv -- ${target} ${new_name} && echo ${new_name} + } - # rename target. - mv -- "${target}" "${new_name}" - - if [[ ${?} != 0 ]]; then - echo -e "${color_bred}${status}: Failed.${color_default}" - ((failed++)) - fi - done - - if [[ ${failed} != 0 ]]; then - echo -e "${color_bred}Failed: ${failed}.${color_default}" - false - fi + _iterate_targets process ${targets[@]} } # Change file name prefix. @@ -336,47 +192,23 @@ function name_ext() { # Usage: name_prefix [FILES] function name_prefix() { local IFS=$'\n' - local old="${1}" - local new="${2}" - local targets=("${@:3}") - local count=0 - local total=$((${#}-2)) - local failed=0 - - # All matching targets by default. - if [[ "${targets}" = "" ]]; then - targets=(${old}*) - total=${#targets[@]} - fi - - # Process. - for target in "${targets[@]}"; do - # Increment counter. - ((count++)) + local old=${1} + local new=${2} + local targets=(${@:3}) + [[ "${targets}" = "" ]] && targets=(${old}*) + process() { # Create new name. local new_name="${new}${target#$old}" - # Prepare status. - local status="[${count}/${total}] ${target} -> ${new_name}" - echo -e "${color_bwhite}${status}${color_default}" - - # Warning on no change. - [[ "${target}" = "${new_name}" ]] && continue + # Skip on no change. + [[ "${target}" = "${new_name}" ]] && return 0 # Rename. - mv -- "${target}" "${new_name}" + mv -- ${target} ${new_name} && echo ${new_name} + } - if [[ ${?} != 0 ]]; then - echo -e "${color_bred}${status}: Failed.${color_default}" - ((failed++)) - fi - done - - if [[ ${failed} != 0 ]]; then - echo -e "${color_bred}Failed: ${failed}.${color_default}" - false - fi + _iterate_targets process ${targets[@]} } # Change file name postfix. @@ -384,47 +216,23 @@ function name_prefix() { # Usage: name_postfix [FILES] function name_postfix() { local IFS=$'\n' - local old="${1}" - local new="${2}" - local targets=("${@:3}") - local count=0 - local total=$((${#}-2)) - local failed=0 - - # All matching targets by default. - if [[ "${targets}" = "" ]]; then - targets=(*${old}) - total=${#targets[@]} - fi - - # Process. - for target in "${targets[@]}"; do - # Increment counter. - ((count++)) + local old=${1} + local new=${2} + local targets=(${@:3}) + [[ "${targets}" = "" ]] && targets=(*${old}) + process() { # Create new name. local new_name="${target%$old}${new}" - # Prepare status. - local status="[${count}/${total}] ${target} -> ${new_name}" - echo -e "${color_bwhite}${status}${color_default}" - - # Warning on no change. - [[ "${target}" = "${new_name}" ]] && continue + # Skip on no change. + [[ "${target}" = "${new_name}" ]] && return 0 # Rename. - mv -- "${target}" "${new_name}" + mv -- ${target} ${new_name} && echo ${new_name} + } - if [[ ${?} != 0 ]]; then - echo -e "${color_bred}${status}: Failed.${color_default}" - ((failed++)) - fi - done - - if [[ ${failed} != 0 ]]; then - echo -e "${color_bred}Failed: ${failed}.${color_default}" - false - fi + _iterate_targets process ${targets[@]} } # Replace part of the name. @@ -432,67 +240,35 @@ function name_postfix() { # Usage: name_replace [FILES] function name_replace() { local IFS=$'\n' - local old="${1}" - local new="${2}" - local targets=("${@:3}") - local count=0 - local total=$((${#}-2)) - local failed=0 - - # All matching targets by default. - if [[ "${targets}" = "" ]]; then - targets=(*${old}*) - total=${#targets[@]} - fi - - # Process. - for target in "${targets[@]}"; do - # Increment counter. - ((count++)) + local old=${1} + local new=${2} + local targets=(${@:3}) + [[ "${targets}" = "" ]] && targets=(*${old}*) + process() { # Create new name. local new_name="${target//$old/$new}" - # Prepare status. - local status="[${count}/${total}] ${target} -> ${new_name}" - echo -e "${color_bwhite}${status}${color_default}" - - # Warning on no change. - [[ "${target}" = "${new_name}" ]] && continue + # Skip on no change. + [[ "${target}" = "${new_name}" ]] && return 0 # Rename. - mv -- "${target}" "${new_name}" + mv -- ${target} ${new_name} && echo ${new_name} + } - if [[ ${?} != 0 ]]; then - echo -e "${color_bred}${status}: Failed.${color_default}" - ((failed++)) - fi - done - - if [[ ${failed} != 0 ]]; then - echo -e "${color_bred}Failed: ${failed}.${color_default}" - false - fi + _iterate_targets process ${targets[@]} } # Fix numbering for numbered files. I.e if there are 10 items and some of them start without zero, then append zero to it. 1..10 -> 01..10. # Usage: name_fix_numbering [FILES] function name_fix_numbering() { local IFS=$'\n' - local targets=("${@}") - local count=0 - local total=${#} - local failed=0 + local highest=0 local power=0 - - # All files by default. - if [[ "${targets[*]}" = "" ]]; then - targets=($(ls | grep "^[0-9]")) - total=${#targets[@]} - fi + local targets=(${@}) + [[ "${targets}" = "" ]] && targets=($(ls | grep "^[0-9]")) # Count leading zeroes. - local highest=0 for target in "${targets[@]}"; do # Check that starts with a digit. [[ "${target}" =~ ^[0-9] ]] || continue @@ -510,16 +286,11 @@ function name_fix_numbering() { i=$((${i}/10)) done - # Process. - for target in "${targets[@]}"; do - ((count++)) - - local status="[${count}/${total}] ${target}" - + process() { # Check that starts with a digit. if [[ ! "${target}" =~ ^[0-9] ]]; then - echo -e "${color_bwhite}${status}${color_default}" - continue + _error "Does not start with a digit!" + return 1 fi # Prepare new name. @@ -528,32 +299,17 @@ function name_fix_numbering() { digit=$((10#${digit})) local new_name=$(printf "%0${power}d" "${digit}")"${target#${digits[0]}}" - status="${status} -> ${new_name}" - - # Skip if the same. - if [[ "${target}" = "${new_name}" ]]; then - echo -e "${color_bwhite}${status}${color_default}" - continue - fi + # Skip if the same name. + [[ "${target}" = "${new_name}" ]] && return 0 # Check that file does not exist. if [[ -e "${new_name}" ]]; then - echo -e "${color_bred}${status}: File exists!${color_default}" + _error "${new_name}: File exists!" return 1 fi - echo -e "${color_bwhite}${status}${color_default}" + mv -- ${target} ${new_name} && echo ${new_name} + } - mv -- "${target}" "${new_name}" - - if [[ ${?} != 0 ]]; then - echo -e "${color_bred}${status}: Failed.${color_default}" - ((failed++)) - fi - done - - if [[ ${failed} != 0 ]]; then - echo -e "${color_bred}Failed: ${failed}.${color_default}" - false - fi + _iterate_targets process ${targets[@]} } diff --git a/.config/bash/module/util.sh b/.config/bash/module/util.sh index 3448fd6..c6feca8 100644 --- a/.config/bash/module/util.sh +++ b/.config/bash/module/util.sh @@ -10,6 +10,7 @@ function _parse_ints() { # Function-wrapper to iterate with specified function with provided files. # By default Iterates on all non-hidden files and directories. +# List of variables available to FUNCTION: target - current file, count - current item index, total - sum of targets, failed - count of previously failed items, status - status line (not recommended to use). # Usage: _iterate_targets [FILES] function _iterate_targets() { local IFS=$'\n' @@ -18,11 +19,12 @@ function _iterate_targets() { local total=${#targets[@]} local count=0 local failed=0 + local code=0 # set dafult value to target all supported archives. if [[ "${targets}" = "" ]]; then - targets=($(ls)) - total=${#targets[@]} + _error "No targets provided." + return 1 fi # iterate each target. @@ -42,11 +44,23 @@ function _iterate_targets() { ((failed++)) echo -e "${color_bred}${status}: Failed.${color_default}" fi + + # Add newline if not the last one. + [[ "${count}" = "${total}" ]] || echo done # Show error. if [[ ${failed} != 0 ]]; then + echo echo -e "${color_bred}Failed: ${failed}.${color_default}" false fi } + +# Report error to stdout. +# Always returns code 1. +# If no message specified, prints "An error has occured". +# Usage: _error [MESSAGE] +function _error() { + echo -e "${color_bred}${*}${color_default}" +}