_msg: Refactor

This commit is contained in:
Stig-Ørjan Smelror 2024-02-14 12:40:53 +01:00
parent 0a8cd680bd
commit 19748fce8e
2 changed files with 25 additions and 24 deletions

View File

@ -1,6 +1,5 @@
typeset -A localization
localization=(
_qsgen2_msg_1 "Bruk: qsgen2 add \"Nettstedsnavn\" \"sti/til/prosjekt\""
_qsgen2_msg_2 "- Tvungen oppdatering: Genererer alt"
_qsgen2_msg_3 "* %SCRIPT_NAME% kan ikke analysere dette nettstedet. Avslutter. *"
_qsgen2_msg_4 "Vennligst installer Pandoc."
@ -18,7 +17,7 @@ _blog_cache_msg_2 "1. _blog_cache: ${blog_file}"
_blog_cache_msg_3 "2. _blog_cache: nåværende cache: ${current_hash}"
_blog_cache_msg_4 "3. _blog_cache: ny cache-fil: ${blog_file}$"
_blog_cache_msg_5 "4. _blog_cache: ny nåværende cache: ${current_hash}"
_pages_cache_msg_1 "SIDERS HASH VERDI: ${pages_cache[${name}]}"
_pages_cache_msg_1 "_pages_cache: PAGES HASH VERDI: ${pages_cache[${name}]}"
_pages_cache_msg_2 "1. pages_cache: ${pages_cache[$file]}"
_pages_cache_msg_3 "1. _pages_cache: nåværende cache: ${current_hash}"
_pages_cache_msg_4 "2. _pages_cache: sidefil: ${pages_cache[$file]}"

40
qsgen2
View File

@ -56,48 +56,50 @@ if (${globaldebug}); then
fi
function _msg() {
# Usage: _msg <type> "key"
local type=$1
local key=$2
local msg=${localization[$key]}
shift # Remove the first argument so $@ now contains only message keys or parts
# Check if message exists for the key
if [[ -z "$msg" ]]; then
printf "Localization key '%s' not found.\n" "$key"
return
local full_msg=""
for part in "$@"; do
if [[ -n "${localization[$part]}" ]]; then
# Part is a localization key
part="${localization[$part]}"
elif [[ "$part" == "%SCRIPT_NAME%" ]]; then
# Special handling for script name placeholder
part=${0:t}
fi
# Dynamic replacement for known placeholders
msg=${msg//"%SCRIPT_NAME%"/${0:t}}
# Append part to the full message
full_msg+="$part"
done
# Apply color formatting based on type and use printf for safe output
case ${type} in
case $type in
std)
printf "${green}%s${end}\n" "$msg"
printf "${green}%s${end}\n" "$full_msg"
;;
info)
printf "${yellow}%s${end}\n" "$msg"
printf "${yellow}%s${end}\n" "$full_msg"
;;
debug)
printf "${red}%s${end}\n" "$msg"
printf "${red}%s${end}\n" "$full_msg"
;;
other)
printf "${bold_yellow}%s${end}\n" "$msg"
printf "${bold_yellow}%s${end}\n" "$full_msg"
;;
sub)
printf "${magenta}%s${end}\n" "$msg"
printf "${magenta}%s${end}\n" "$full_msg"
;;
main)
printf "${white}${green_bg} %s ${end}\n" "$msg"
printf "${white}${green_bg} %s ${end}\n" "$full_msg"
;;
*)
printf "%s\n" "$msg"
printf "%s\n" "$full_msg"
;;
esac
}
function _version() {
_msg info "_qsgen2_msg_7$(strftime "%Y")"
_msg info "_qsgen2_msg_7" "$(strftime "%Y")"
echo "${yellow}- https://github.com/kekePower/qsgen2/${end}"
_msg info "_qsgen2_msg_8"
exit