From 99e4756dc12650967cf18f3b93f38629cd09b232 Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Thu, 14 May 2026 12:55:07 +0200 Subject: refactor(output): centralize formatted terminal messages Add a shared emit() helper for styled terminal output and route error(), msg(), warn(), verbose(), and debug() through it. This removes duplicated printf formatting across the output helpers while keeping the existing prefixes, colors, quiet/verbose/debug gating, and stderr handling for errors. emit() also joins additional message arguments into a single trailing string, matching the output pattern used by the individual helpers. The output helpers now differ only in their prefix, color, and visibility condition, making future formatting changes easier and less error-prone. --- transcode.sh | 44 ++++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/transcode.sh b/transcode.sh index 1fd1fe0..9fca292 100755 --- a/transcode.sh +++ b/transcode.sh @@ -196,41 +196,57 @@ readonly EXIT_OK EXIT_RUNTIME_FAILURE EXIT_USAGE_ERROR EXIT_CONFIG_ERROR EXIT_MI # functions and runtime behaviour such as displaying usage instructions. ############################################################################### -error() { +emit() { + local prefix="$1" + local color="$2" + shift 2 + + if [[ $# -eq 0 ]]; then + printf "%b%s%b\n" "${BOLD}${color}" "$prefix" "$ALL_OFF" + return + fi + local mesg="$1" shift - printf "${BOLD}${RED}==> ERROR:${ALL_OFF} ${BOLD}%s${ALL_OFF} %s\n" "$mesg" "$@" >&2 + + local rest="" + if [[ $# -gt 0 ]]; then + printf -v rest ' %s' "$@" + rest="${rest# }" + fi + + printf "%b%s%b %b%s%b" "${BOLD}${color}" "$prefix" "$ALL_OFF" "$BOLD" "$mesg" "$ALL_OFF" + if [[ -n "$rest" ]]; then + printf " %s" "$rest" + fi + printf "\n" +} + +error() { + emit "==> ERROR:" "$RED" "$@" >&2 } msg() { if [[ $QUIET -eq 0 ]]; then - local mesg="$1" - shift - printf "${BOLD}${GREEN}==>${ALL_OFF} ${BOLD}%s${ALL_OFF} %s\n" "$mesg" "$@" + emit "==>" "$GREEN" "$@" fi } warn() { if [[ $QUIET -eq 0 ]]; then - local mesg="$1" - shift - printf "${BOLD}${YELLOW}==>${ALL_OFF} ${BOLD}%s${ALL_OFF} %s\n" "$mesg" "$@" + emit "==>" "$YELLOW" "$@" fi } verbose() { if [[ $VERBOSE -eq 1 && $QUIET -eq 0 ]]; then - local mesg="$1" - shift - printf "${BOLD}${BLUE}==>${ALL_OFF} ${BOLD}%s${ALL_OFF} %s\n" "$mesg" "$@" + emit "==>" "$BLUE" "$@" fi } debug() { if [[ $DEBUG -eq 1 ]]; then - local mesg="$1" - shift - printf "${BOLD}${MAGENTA}==> DEBUG:${ALL_OFF} ${BOLD}%s${ALL_OFF} %s\n" "$mesg" "$@" + emit "==> DEBUG:" "$MAGENTA" "$@" fi } -- cgit v1.3.1