about summary refs log tree commit diff
diff options
context:
space:
mode:
authorSingustromo <singustromo@disroot.org>2023-11-07 22:15:10 +0100
committerSingustromo <singustromo@disroot.org>2023-11-07 22:15:10 +0100
commitab4276239811e01960b880ef462beb3f5481eb6d (patch)
tree6a18aae723427b552042ebe9b8cdae5d2056ea86
parente639d79b27c68f7fb89dd9c48dfdef0c35fc49e5 (diff)
downloadradio-sh-ab4276239811e01960b880ef462beb3f5481eb6d.tar.gz
radio-sh-ab4276239811e01960b880ef462beb3f5481eb6d.zip
non-exposed options are prefixed with _
-rwxr-xr-xradio71
1 files changed, 36 insertions, 35 deletions
diff --git a/radio b/radio
index 27d0c81..d15ea1a 100755
--- a/radio
+++ b/radio
@@ -29,9 +29,10 @@ declare -r -a _config_directories=(
     "/etc/xdg/$_scriptname-sh")
 
 # default options
+# those prefixed with _ are not exposed to the user nor read from config
 declare -A _option=(
-    [config_filename]="config.json"
-    [internal_delimiter]=";"                # internal usage only
+    [_config_filename]="config.json"
+    [_delimiter]=";"
     [max_title_length]=40                   # `get title short`
     [menu_cmd]="dmenu"                      # menu used for output
     [menu_sortby_listencount]=1             # how to sort stations in menu
@@ -220,7 +221,7 @@ PARAMETERS
     --version                          Displays the version
 
 CONFIGURATION
-    The configuration (${_option[config_filename]}) is read
+    The configuration (${_option[_config_filename]}) is read
     from one of these locations (descending priority):
         $config_directory_list
 
@@ -266,26 +267,26 @@ Convert:time_to_int() {
 
 # creates an empty configuration file
 # Depends: Sys:log(), $_configuration, $_config_directories=(...),
-#          $_config_file_location, ${_option[config_filename]}
+#          $_config_file_location, ${_option[_config_filename]}
 Config:create() {
     _configuration="$(printf '{"option": {},"stations": []}' | jq)"
     for dir in "${_config_directories[@]}"; do
         [ -d "$dir" ] || { mkdir -p "$dir" || continue; }
         [ -w "$dir" ] || continue
-        _config_file_location="$dir/${_option[config_filename]}"
+        _config_file_location="$dir/${_option[_config_filename]}"
         Sys:log "$dir is writable. Creating empty configuration."
-        printf "%s" "$_configuration" > "$dir/${_option[config_filename]}" \
-          && Sys:log "Wrote to $dir/${_option[config_filename]}"
+        printf "%s" "$_configuration" > "$dir/${_option[_config_filename]}" \
+          && Sys:log "Wrote to $dir/${_option[_config_filename]}"
         return
     done
     false; return
 }
 
 # loads the configuration contents into the global $_configuration variable
-# Depends: Sys:log(), ${_option[config_filename]}, $_configuration
+# Depends: Sys:log(), ${_option[_config_filename]}, $_configuration
 Config:read() {
     local directory config_file=""
-    local filename="${_option[config_filename]}"
+    local filename="${_option[_config_filename]}"
 
     for directory in "${_config_directories[@]}"; do
         [ -r "$directory/$filename" ] || continue
@@ -415,11 +416,11 @@ Config:get() {
     printf "%b\n" "$jq_result"
 }
 
-# Returns all options with respective value
+# Returns all exposed options with respective value
 # Configuration overwrites the default values
 # One option per line with the corresponding value and delimiter in between
 # Depends: -A _option=([..]='..',...), Config:get(),
-#          ${_option[internal_delimiter]}
+#          ${_option[_delimiter]}
 Config:dump_options() {
     local optionlist name value option_names option_values
     
@@ -430,37 +431,37 @@ Config:dump_options() {
     # combine both into 'name;value' per line
     # Don't return options with the value 'null'
     local line=1
-    while IFS="${_option[internal_delimiter]}" read -r value; do
+    while IFS="${_option[_delimiter]}" read -r value; do
         name="$(printf "%b" "$option_names" | sed "${line}q;d")"
         (( line++ ))
-        # non-exposed options
-        [[ "$name" =~ internal_delimiter|streamer_args|config_filename ]] \
-            && { Sys:log "$name not exposed"; continue; }
-        optionlist+="${name}${_option[internal_delimiter]}"
-        if [ "$value" != "null" ]; then # ignore nonexistant options
+
+        [ "${name::1}" == '_' ] && continue; # non-exposed options
+        optionlist+="${name}${_option[_delimiter]}"
+
+        if [ "$value" != "null" ]; then
             optionlist+="$value\n"
-        else # get value from array
-            Sys:log "$name not set. Using default: ${_option[$name]}"
-            optionlist+="${_option[$name]}\n"
+            continue
         fi
+
+        Sys:log "'$name' not set. Using default: ${_option[$name]}"
+        optionlist+="${_option[$name]}\n"
     done <<< "$option_values"
     
     printf "%b" "$optionlist"
-    return
 }
 
 # loads the options from the configuration file
 # into the $_option associative array
-# Depends: Config:dump_options(), ${_option[internal_delimiter]},
+# Depends: Config:dump_options(), ${_option[_delimiter]},
 #          Sys:log(), -A option=(), 
 # TODO reduce execution time (currently 35% of total exec time)
 Config:load_options() { 
     local option_name optionlist value
-    Sys:log "Loading options from configuration"
+
     optionlist="$(Config:dump_options)"
     [ -n "$optionlist" ] || return
 
-    while IFS="${_option[internal_delimiter]}" read -r option_name value; do
+    while IFS="${_option[_delimiter]}" read -r option_name value; do
         # ignore invalid options or default values
         [ "$value" == "${_option[$option_name]}" ] && continue
         { [ "$value" == 'null' ] || [ "$value" == " " ]; } && continue
@@ -472,9 +473,9 @@ Config:load_options() {
 # saves station tuples to array and prints station names to stdout
 # returns true, if jq had any output
 # Depends: -a _config_stations, ${_option[menu_sortby_listencount]},
-#          ${_option[internal_delimiter]}, $_configuration
+#          ${_option[_delimiter]}, $_configuration
 Config:load_stations() {
-    local line stations jq_cmd delimiter="${_option[internal_delimiter]}"
+    local line stations jq_cmd delimiter="${_option[_delimiter]}"
     jq_cmd="jq -j '.stations | "
 
     if [ "${_option[menu_sortby_listencount]}" -eq 1 ]; then
@@ -597,11 +598,11 @@ Config:print_stations() {
 }
 
 # prints the options with respective values
-# Depends: Config:dump_options(), Sys:msg(), ${_option[internal_delimiter]}
+# Depends: Config:dump_options(), Sys:msg(), ${_option[_delimiter]}
 Config:print_options() {
     local optionlist
     optionlist="$(Config:dump_options | sort)"
-    Sys:msg "Current configuration:\n${optionlist//${_option[internal_delimiter]}/ = }"
+    Sys:msg "Current configuration:\n${optionlist//${_option[_delimiter]}/ = }"
     exit
 }
 
@@ -615,7 +616,7 @@ Menu:clear() { [ -n "$_menu_items" ] || return; Sys:log "clearing menu.." ; _men
 Menu:insert() {
     local text="$1" command="${2:-spacer}" # command defaults to no action ''
     Sys:log "\n\t'$text' cmd:'$command'"
-    _menu_items+="${text}${_option[internal_delimiter]}$command\n"
+    _menu_items+="${text}${_option[_delimiter]}$command\n"
 
     if [ -n "$3" ]; then
         shift; shift
@@ -665,7 +666,7 @@ Menu:open() {
     # prepare for display
     if [ -z "$text" ]; then
         while IFS=$'\n' read -r station; do
-            text+="${station%"${_option[internal_delimiter]}"*}\n"
+            text+="${station%"${_option[_delimiter]}"*}\n"
         done <<< "$(printf '%b' "$_menu_items")"
     fi
 
@@ -708,7 +709,7 @@ Menu:open() {
     # Menu was filled via Menu:insert
     if [ -n "$_menu_items" ]; then
         returnval="$(printf "%b" "$_menu_items" \
-                    | grep "${item}${_option[internal_delimiter]}" 2>&-)"
+                    | grep "${item}${_option[_delimiter]}" 2>&-)"
         Menu:clear
     fi
     Sys:log "selection: '${returnval:-"$item"}'"
@@ -1010,9 +1011,9 @@ main() {
     Sys:depcheck fw:"$_config_file_location"
 
     # These can be set after config was loaded
-    _option[streamer_args]="--input-ipc-server='${_option[streamer_socket]}' \
+    _option[_streamer_args]="--input-ipc-server='${_option[streamer_socket]}' \
                            --audio-client-name=${_option[clientname]}"
-    _streamer_cmd="mpv ${_option[streamer_args]} 2>&-"
+    _streamer_cmd="mpv ${_option[_streamer_args]} 2>&-"
     _socketcmd="socat - ${_option[streamer_socket]} 2>&-"
     _streamer_pid="$(Streamer:get_pid)" # Needed for check
 
@@ -1113,8 +1114,8 @@ main() {
 
     choice="$(Menu:open)" || exit
 
-    station="${choice%"${_option[internal_delimiter]}"*}"
-    cmd="${choice#*"${_option[internal_delimiter]}"}"
+    station="${choice%"${_option[_delimiter]}"*}"
+    cmd="${choice#*"${_option[_delimiter]}"}"
     Sys:log "Choice: '$station' cmd: '$cmd' ($choice)"
 
     case "$cmd" in