From 5b25505990028bc94b58710cd693a17b3ddf7774 Mon Sep 17 00:00:00 2001 From: Levon Gevorgyan Date: Tue, 28 Apr 2026 09:36:27 -0500 Subject: [PATCH] switch to dedicated echo_ functions --- add-synced-lyrics.sh | 14 +++++++---- lib/0-utils.sh | 59 +++++++++++++++++++++++++++++--------------- lib/deezer.sh | 6 ++--- lib/lrclib.sh | 6 ++--- lib/spotify.sh | 6 ++--- 5 files changed, 57 insertions(+), 34 deletions(-) diff --git a/add-synced-lyrics.sh b/add-synced-lyrics.sh index 07dc70a..63a05bc 100755 --- a/add-synced-lyrics.sh +++ b/add-synced-lyrics.sh @@ -58,14 +58,18 @@ process_inputs() { download_with_deezer ) - for input in "${INPUTS[@]}"; do + local numInputs="${#INPUTS[@]}" + for ((i = 0; i < numInputs; i++)); do if [[ ${IGNORE} == false && ${ret} -ne 0 ]]; then return 1 fi + local input="${INPUTS[${i}]}" + echo_info "processing $((i + 1))/${numInputs}: ${input}" + if [[ "${input,,}" == *'instrumental'* || "${input,,}" == *'acoustic'* ]]; then - echo "${input} is instrumental, skipping" + echo_pass "${input} is instrumental, skipping" continue fi @@ -74,13 +78,13 @@ process_inputs() { if [[ -f "${output}" ]]; then if is_valid_lrc "${output}"; then + echo_pass "${input} already has lyrics, skipping" continue else rm "${output}" || return 1 fi fi - echo "processing ${input}" isrc='' if ! isrc="$(get_file_isrc "${input}")"; then ret=1 @@ -101,11 +105,11 @@ process_inputs() { fi if ! void test -f "${output}"; then - echo "failed to get lyrics for ${input}" + echo_fail "could not find lyrics for ${input}" ret=1 continue else - void echo "added lyrics for ${input}" + void echo_pass "added lyrics for ${input}" fi done diff --git a/lib/0-utils.sh b/lib/0-utils.sh index cf0fa1e..2e44dc8 100644 --- a/lib/0-utils.sh +++ b/lib/0-utils.sh @@ -20,29 +20,48 @@ jq_result_valid() { return 0 } -stderr() { echo "$*" 1>&2; } -info() { stderr "INFO [${FUNCNAME[1]}]: $*"; } -error() { stderr "ERROR [${FUNCNAME[1]}]: $*"; } +# ANSI colors +readonly RED='\e[0;31m' +readonly GREEN='\e[0;32m' +readonly PURPLE='\e[0;35m' +readonly YELLOW='\e[0;33m' +readonly CYAN='\e[0;36m' +readonly NC='\e[0m' -# only echo "$@" if ${DEBUG} is true +stderr() { + local word="${WORD:-}" + local color="${COLOR:-}" + local function="${FUNCNAME[2]}" + if [[ "${function}" =~ void ]]; then + function="${FUNCNAME[3]}" + fi + echo \ + -e \ + "${color}${word}${NC}" \ + "[${function}]" \ + "$@" 1>&2 +} +echo_info() { WORD=INFO COLOR="${CYAN}" stderr "$@"; } +echo_fail() { WORD=FAIL COLOR="${RED}" stderr "$@"; } +echo_pass() { WORD=PASS COLOR="${GREEN}" stderr "$@"; } +echo_dbug() { WORD=DBUG COLOR="${PURPLE}" stderr "$@"; } + +# only perform the command given if ${DEBUG} is enabled debug() { if [[ ${DEBUG} == false ]]; then return 0 fi - stderr "$@" + "$@" } -# do not do anything -# if ${DRY_RUN} is enabled +# only perform the command given if ${DRY_RUN} is enabled void() { - local cmd=("$@") if [[ ${DRY_RUN} == true ]]; then return 0 - else - "${cmd[@]}" - return $? fi + + "$@" } # print out "$@" formatted for bash @@ -80,7 +99,7 @@ check_required_utils() { local missing=false for util in "${utils[@]}"; do if ! have_cmd "${util}"; then - error "missing ${util}!" + echo_fail "missing ${util}!" missing=true fi done @@ -127,8 +146,8 @@ create_tmp_dir() { check_tmp_dir_empty() { for f in "${TMPDIR}"/*; do if [[ -f "${f}" ]]; then - error "tempdir ${TMPDIR} is not empty!" - error "aborting operation" + echo_fail "tempdir ${TMPDIR} is not empty!" + echo_fail "aborting operation" return 1 fi done @@ -157,7 +176,7 @@ librelyrics_dl() { done if [[ ${librelyricsRet} -eq 0 && -z ${downloadedFile} ]]; then - error "failed to download file for ${url} despite no librelyrics error" + echo_fail "failed to download file for ${url} despite no librelyrics error" return 1 fi @@ -168,7 +187,7 @@ librelyrics_dl() { fi if [[ ${librelyricsRet} -eq 0 && -z ${fileContents} ]]; then - error "failed to read file for ${url} despite no librelyrics error" + echo_fail "failed to read file for ${url} despite no librelyrics error" return 1 fi @@ -181,7 +200,7 @@ download_with_librelyrics() { local urlType="$3" if [[ ! "${urlType}" =~ spotify|deezer ]]; then - error "only spotify|deezer are supported" + echo_fail "only spotify|deezer are supported" return 1 fi @@ -194,11 +213,11 @@ download_with_librelyrics() { "${url}")" if [[ -z "${lyrics}" ]]; then - error "could not get lyrics for ${isrc} using ${urlType}" + debug echo_fail "could not find lyrics for ${isrc} using ${urlType}" return 1 fi - info "found lyrics for ${isrc} with ${urlType}" + debug echo_info "found lyrics for ${isrc} with ${urlType}" echo "${lyrics}" >"${output}" } @@ -228,7 +247,7 @@ get_file_isrc() { jqResult="$(jq -r "${jqFilter}" <<<"${probe}")" if [[ "${jqResult}" == 'null' ]]; then - error "failed to find ISRC for ${file}" + echo_fail "could not find ISRC for ${file}" return 1 fi echo "${jqResult}" diff --git a/lib/deezer.sh b/lib/deezer.sh index 88cefb6..34ab54b 100644 --- a/lib/deezer.sh +++ b/lib/deezer.sh @@ -6,7 +6,7 @@ get_deezer_isrc_response() { local isrc="$1" if [[ -z "${isrc}" ]]; then - error "missing isrc" + echo_fail "missing isrc" return 1 fi @@ -20,7 +20,7 @@ get_deezer_isrc_response() { get_deezer_url() { local isrc="$1" if [[ -z "${isrc}" ]]; then - error "missing isrc" + echo_fail "missing isrc" return 1 fi @@ -31,7 +31,7 @@ get_deezer_url() { jqResult="$(jq -r .link <<<"${deezerResponse}")" if ! jq_result_valid "${jqResult}"; then - error "could not get link from deezer for ${isrc}" + echo_fail "could not find link from deezer for ${isrc}" return 1 fi diff --git a/lib/lrclib.sh b/lib/lrclib.sh index 890839c..cd7a4c8 100644 --- a/lib/lrclib.sh +++ b/lib/lrclib.sh @@ -19,11 +19,11 @@ get_lrclib_api_response() { IFS=' ' read -r env jqFilter <<<"${data}" jqResult="$(jq -r "${jqFilter}" <<<"${deezerResponse}")" if ! jq_result_valid "${jqResult}"; then - error "could not determine lrclib ${env} for ${isrc}" + echo_fail "could not determine lrclib ${env} for ${isrc}" return 1 fi declare "${env}=${jqResult}" - debug "${env}=${jqResult}" + debug echo_debug "${env}=${jqResult}" done # variables assigned above @@ -48,7 +48,7 @@ download_with_lrclib() { lrclibResponse="$(get_lrclib_api_response "${isrc}")" syncedLyrics="$(jq -r .syncedLyrics <<<"${lrclibResponse}")" if ! jq_result_valid "${syncedLyrics}"; then - error "could not get lyrics for ${isrc}" + debug echo_fail "could not find lyrics for ${isrc}" return 1 fi diff --git a/lib/spotify.sh b/lib/spotify.sh index 294507c..427d3b9 100644 --- a/lib/spotify.sh +++ b/lib/spotify.sh @@ -6,7 +6,7 @@ get_spotify_url() { local isrc="$1" if [[ -z "${isrc}" ]]; then - error "missing isrc" + echo_fail "missing isrc" return 1 fi @@ -41,7 +41,7 @@ get_spotify_url() { for tag in "${tags[@]}"; do declare -n spotifyTagValue="${tag}" - debug "${tag}=${spotifyTagValue}" + debug echo_debug "${tag}=${spotifyTagValue}" done # shellcheck disable=SC2154 @@ -52,7 +52,7 @@ get_spotify_url() { done <<<"${jqOutput}" if [[ "${foundMatching}" == false ]]; then - error "could not find matching spotify track for ${isrc}" + debug echo_fail "could not find matching spotify track for ${isrc}" return 1 fi