From 43b1d7bc8e5b62cd15461b46daa024afd9c06d72 Mon Sep 17 00:00:00 2001 From: home Date: Sun, 17 Dec 2023 20:44:54 +0300 Subject: [PATCH] Transcode : Use _iterate_target. --- .config/bash/module/checksum.sh | 2 +- .config/bash/module/transcode.sh | 53 +++++++------------------------- .config/bash/module/util.sh | 8 +++++ 3 files changed, 20 insertions(+), 43 deletions(-) diff --git a/.config/bash/module/checksum.sh b/.config/bash/module/checksum.sh index d2052e9..b1b79c3 100644 --- a/.config/bash/module/checksum.sh +++ b/.config/bash/module/checksum.sh @@ -32,7 +32,7 @@ function checksum_check() { local hashfile=".${target#./}.sha1" # Skip if hash doesn't exist. - [[ -f "${hashfile}" ]] || { ((skipped++)); _warn "No hash found."; return 0; } + [[ -f "${hashfile}" ]] || { _skip "No hash found."; return 0; } # Calculate hash. local stored=$(cat "${hashfile}" | cut -d\ -f1) diff --git a/.config/bash/module/transcode.sh b/.config/bash/module/transcode.sh index 5c82bf0..03bb2fd 100644 --- a/.config/bash/module/transcode.sh +++ b/.config/bash/module/transcode.sh @@ -3,43 +3,26 @@ # Usage: transcode [FILES] function transcode() { local IFS=$'\n' - local targets=("${@:2}") - local format="${1}" - local total=${#targets[@]} - local count=0 - local failed=0 - local skipped=0 + local format=${1} + local targets=(${@:2}) + [[ "${targets}" = "" ]] && targets=($(_ls_file)) # Report no format. - if [[ "${format}" = "" ]]; then - echo -e "${color_bred}No format specified.${color_default}" + if [[ "${format}" = "" ]] || [[ "${format}" =~ "." ]]; then + _error "No format specified." help transcode return 2 fi - # All files by default. - if [[ "${targets}" = "" ]]; then - targets=($(ls --classify | grep -v /$)) - total=${#targets[@]} - fi - - # Process. - for target in "${targets[@]}"; do - # Increment counter. - ((count++)) - + process() { # Define context names and status. local from="${target##*.}" local to="${format}" local output="${target##*/}" output="${output%.*}.${to}" - local status="[${count}/${total}] ${target} -> ${output}" - - # Show status. - echo -e "${color_bwhite}${status}${color_default}" # Skip if file exists. - [[ -f "${output}" ]] && { ((skipped++)); continue; } + [[ -f "${output}" ]] && { _skip "Already exists."; return 0; } # Support multiple inputs. [[ "${to}" = "mp3" ]] && from="" @@ -68,27 +51,13 @@ function transcode() { _transcode_mkv "${target}" "${output}" ;; *) - echo -e "${color_yellow}Conversion ${target##*.}-${to} not supported.${color_default}" - false + _error "Conversion ${target##*.}-${to} not supported." + return 1 ;; esac + } - # Show error. - if [[ ${?} != 0 ]]; then - echo -e "${color_bred}${status}: Failed.${color_default}" - ((failed++)) - fi - done - - if [[ ${skipped} != 0 ]]; then - echo -e "${color_byellow}Skipped: ${skipped}.${color_default}" - return 1 - fi - - if [[ ${failed} != 0 ]]; then - echo -e "${color_bred}Failed: ${failed}.${color_default}" - false - fi + _iterate_targets process ${targets[@]} } function _transcode_gz-xz() { diff --git a/.config/bash/module/util.sh b/.config/bash/module/util.sh index 4604f70..7cec1f9 100644 --- a/.config/bash/module/util.sh +++ b/.config/bash/module/util.sh @@ -77,3 +77,11 @@ function _error() { function _warn() { echo -e "${color_byellow}${*}${color_default}" } + +# Skip current iteration. +# Usage: _skip [MESSAGE] +function _skip() { + ((skipped++)) + + [[ "${*}" = "" ]] || echo -e "${color_byellow}${*}${color_default}" +}