Rewrite of yadoom to better handle command line parameters.

This commit is contained in:
Storm Dragon 2024-08-25 13:18:17 -04:00
parent c8b553fdc6
commit 8a5d0be2b4

View File

@ -1,21 +1,10 @@
#!/usr/bin/env bash #!/usr/bin/env bash
help() {
echo "${0##*/}"
echo "Released under the terms of the GPL license."
echo -e "This is a Stormux project: https://stormux.org\n"
echo -e "Usage:\n"
echo "With no arguments, open ${0##*/}."
for i in "${!command[@]}" ; do
echo "-${i/:/ <parameter>}: ${command[${i}]}"
done | sort
exit 0
}
# Function to check if a file is an IWAD # Function to check if a file is an IWAD
is_iwad() { is_iwad() {
local file="$1" local file="$1"
[[ "${file,,}" =~ ipk3$ ]] && return 0
if [[ -f "$file" ]]; then if [[ -f "$file" ]]; then
# Extract the first four bytes and check if they match the IWAD signature # Extract the first four bytes and check if they match the IWAD signature
if hexdump -n 4 -e '4/1 "%02X"' "$file" | grep -q '^49574144$'; then if hexdump -n 4 -e '4/1 "%02X"' "$file" | grep -q '^49574144$'; then
@ -27,59 +16,72 @@ is_iwad() {
return 1 return 1
} }
iwad_menu() {
# Extract the Path= lines from the [IWADSearch.Directories] section
mapfile -t wadPaths < <(sed -n "/^\[IWADSearch.Directories\]/,/^\[/{/Path=/p;}" "$configFile" | cut -d '=' -f 2)
declare -a wadList
# Loop through each path in wadPaths and find WAD files
for i in "${wadPaths[@]}"; do
# Use find and append results to wadList array
if [[ -d "$i" ]]; then
while IFS= read -r -d $'\0' wad; do
is_iwad "$wad" && wadList+=("$wad")
done < <(find "$i" -follow -maxdepth 1 -type f \( -iname '*.wad' -o -iname '*.ipk3' \) -print0)
fi
done
# Set up the menu
declare -a wadMenu
for path in "${wadList[@]}" ; do
title="${path##*/}"
title="${title%.*}"
wadMenu+=("${title}" "${path}")
done
# Run yad to display the dialog
iwad=$(yad --list \
--title="Yadoom" \
--text="Select an Iwad" \
--column="Iwad" \
--column="Path" \
--button="Launch:0" \
--button="Close:1" \
--hide-column=2 \
--search-column=1 \
--skip-taskbar \
"${wadMenu[@]}")
yadCode="$?"
[[ ${yadCode} -eq 0 ]] || exit 0
iwad="${iwad#*|}"
iwad="${iwad%|}"
}
declare -A command=( configFile="${XDG_CONFIG_HOME:-$HOME/.config}/gzdoom/gzdoom.ini"
[c:]="gzdoom configuration file to be used. Default: ${XDG_CONFIG_HOME:-$HOME/.config}/gzdoom/gzdoom.ini" declare -a gzdoomArgs
[h]="This help screen."
)
configFile="${configfile:-${XDG_CONFIG_HOME:-$HOME/.config}/gzdoom/gzdoom.ini}"
# Process arguments
# Convert the keys of the associative array to a format usable by getopts while [[ $# -gt 0 ]]; do
args="${!command[*]}" case "$1" in
args="${args//[[:space:]]/}" -c)
while getopts "${args}" i 2> /dev/null ; do configFile="$2"
case "$i" in shift 2
c) configFile="${OPTARG}" ;; ;;
h) help ;; -iwad|-i)
*) continue ;; iwad="$2"
gzdoomArgs+=("-iwad" "$2")
shift 2
;;
*)
gzdoomArgs+=("$1")
shift
;;
esac esac
done done
# Extract the Path= lines from the [IWADSearch.Directories] section # If no IWAD specified, show the menu
mapfile -t wadPaths < <(sed -n "/^\[IWADSearch.Directories\]/,/^\[/{/Path=/p;}" "$configFile" | cut -d '=' -f 2) if [[ -z "$iwad" ]]; then
declare -a wadList iwad_menu
# Loop through each path in wadPaths and find WAD files gzdoomArgs+=("-iwad" "$iwad")
for i in "${wadPaths[@]}"; do fi
# Use find and append results to wadList array
if [[ -d "$i" ]]; then
while IFS= read -r -d $'\0' wad; do
is_iwad "$wad" && wadList+=("$wad")
done < <(find "$i" -follow -maxdepth 1 -type f -iname '*.wad' -print0)
fi
done
# Set up the menu # Launch GZDoom with all arguments
declare -a wadMenu gzdoom "${gzdoomArgs[@]}"
for path in "${wadList[@]}" ; do
title="${path##*/}"
title="${title%.*}"
wadMenu+=("${title}" "${path}")
done
# Run yad to display the dialog
iwad=$(yad --list \
--title="Yadoom" \
--text="Select an Iwad" \
--column="Iwad" \
--column="Path" \
--button="Launch:0" \
--button="Close:1" \
--hide-column=2 \
--search-column=1 \
--skip-taskbar \
"${wadMenu[@]}")
yadCode="$?"
[[ ${yadCode} -eq 0 ]] || exit 0
iwad="${iwad#*|}"
iwad="${iwad%|}"
gzdoom "$@" -iwad "${iwad}"