From d1774fa6cb4768be1dcda76babb2a344a63751e6 Mon Sep 17 00:00:00 2001 From: desktop Date: Wed, 27 Dec 2023 14:05:07 +0300 Subject: [PATCH] Name : Skip archives when simplifying names. --- .config/bash/module/Archive.sh | 7 ++++ .config/bash/module/Checksum.sh | 2 +- .config/bash/module/Name.sh | 67 ++++++++++++++++++++------------- .config/bash/module/Util.sh | 4 +- .config/bash/module/Vdl.sh | 4 +- 5 files changed, 52 insertions(+), 32 deletions(-) diff --git a/.config/bash/module/Archive.sh b/.config/bash/module/Archive.sh index b40c917..8c5f5e6 100644 --- a/.config/bash/module/Archive.sh +++ b/.config/bash/module/Archive.sh @@ -267,6 +267,13 @@ function _archive_date() { date +%Y%m%d%H%M } +# Check if file is an archive. +function _is_archive() { + local out=$(echo "${*}" | grep -E ${_archive_pattern}) + + [[ "${out}" != "" ]] +} + complete -o filenames -F _archive_grep archive_check unarchive archive_rm complete -o filenames -F _archive_grep_fast archive_xz complete -o filenames -F _archive_name archive_name diff --git a/.config/bash/module/Checksum.sh b/.config/bash/module/Checksum.sh index b1b79c3..6b2ddd0 100644 --- a/.config/bash/module/Checksum.sh +++ b/.config/bash/module/Checksum.sh @@ -75,7 +75,7 @@ function _checksum_check() { # Compare values. if [[ "${stored}" != "${actual}" ]]; then - echo -e "${color_bred}${file}: Failed.${color_default}" + _error "${file}: Failed." return 1 fi diff --git a/.config/bash/module/Name.sh b/.config/bash/module/Name.sh index 8bd51a2..dff9885 100644 --- a/.config/bash/module/Name.sh +++ b/.config/bash/module/Name.sh @@ -1,3 +1,37 @@ +# Rename dirs to `snake_case` and files to `PascalCase`. Careful with structured file names like archives! +# Usage: name [FILES] +function name() { + local IFS=$'\n' + local targets=(${@}) + [[ "${targets}" = "" ]] && targets=($(ls)) + + process() { + # Skip archive. + if $(_is_archive ${target}); then + _warn "File is an archive, skip." + return 0 + fi + + if [[ -d "${target}" ]]; then + local new_name=$(parse_snake ${target}) + [[ -e "${new_name}" ]] && return 0 + + mv -- ${target} ${new_name} && echo ${new_name} + else + local ext=".${target##*.}" + local name=${target%.*} + [[ "${ext}" = ".${target}" ]] && ext="" + + local new_name="$(parse_pascal ${name})${ext}" + [[ -e "${new_name}" ]] && return 0 + + mv -- ${target} ${new_name} && echo ${new_name} + fi + } + + _iterate_targets process ${targets[@]} +} + # Rename files to strip all special characters. # All files by default. # Usage: name_simple [FILES] @@ -7,6 +41,12 @@ function name_simple() { [[ "${targets}" = "" ]] && targets=([^.]*) process() { + # Skip archive. + if $(_is_archive ${target}); then + _warn "File is an archive, skip." + return 0 + fi + # parse new name. local ext="" local name="${target}" @@ -314,30 +354,3 @@ function name_fix_numbering() { _iterate_targets process ${targets[@]} } -# Rename dirs to `snake_case` and files to `PascalCase`. Careful with structured file names like archives! -# Usage: name [FILES] -function name() { - local IFS=$'\n' - local targets=(${@}) - [[ "${targets}" = "" ]] && targets=($(ls)) - - process() { - if [[ -d "${target}" ]]; then - local new_name=$(parse_snake ${target}) - [[ -e "${new_name}" ]] && return 0 - - mv -- ${target} ${new_name} && echo ${new_name} - else - local ext=".${target##*.}" - local name=${target%.*} - [[ "${ext}" = ".${target}" ]] && ext="" - - local new_name="$(parse_pascal ${name})${ext}" - [[ -e "${new_name}" ]] && return 0 - - mv -- ${target} ${new_name} && echo ${new_name} - fi - } - - _iterate_targets process ${targets[@]} -} diff --git a/.config/bash/module/Util.sh b/.config/bash/module/Util.sh index f33ca3d..041c527 100644 --- a/.config/bash/module/Util.sh +++ b/.config/bash/module/Util.sh @@ -35,7 +35,7 @@ function _iterate_targets() { # status info. local status="[${count}/${total}] ${target}" - echo -e "${color_bwhite}${status}${color_default}" + _info "${status}" # Call function. ${foo} "${target}" @@ -43,7 +43,7 @@ function _iterate_targets() { # Show error. if [[ ${?} != 0 ]]; then ((failed++)) - echo -e "${color_bred}${status}: Failed.${color_default}" + _error "${status}: Failed." fi # Add newline if not the last one. diff --git a/.config/bash/module/Vdl.sh b/.config/bash/module/Vdl.sh index 6439b7f..3258529 100644 --- a/.config/bash/module/Vdl.sh +++ b/.config/bash/module/Vdl.sh @@ -3,7 +3,7 @@ function vdl() { # Check that ffmpeg and ffprobe are available. if [[ "$(ffmpeg -version)" = "" ]] || [[ "$(ffprobe -version)" = "" ]]; then - echo -e "${color_red}ffmpeg and ffprobe are required.${color_default}" + _error "ffmpeg and ffprobe are required." return 1 fi @@ -14,7 +14,7 @@ function vdl() { # If could not get [LINK] eventually, show an error and exit. if [[ "${target}" = "" ]]; then - echo -e "${color_red}Could not determine [LINK] to download.${color_default}" + _error "Could not determine [LINK] to download." help vdl return 2 fi