punktdateien/bash/bin/launch-menu-apps

69 lines
1.7 KiB
Bash
Executable file

#!/usr/bin/env bash
set -eo pipefail
# Define associative array
declare -A apps=()
[[ ! -d "${HOME:?}/.config/launch-menu-apps.d" ]] && (
mkdir -p "${HOME}/.config/launch-menu-apps.d"
cat << EOF >> "${HOME}/.config/launch-menu-apps.d/template.sh.disabled"
#!/usr/bin/env bash
apps+=(
["Example application"]="example-application"
["Test script"]="${HOME}/.local/bin/test-script"
)
EOF
)
# shellcheck disable=SC2012
[[ "$(ls -1 "${HOME}"/.config/launch-menu-apps.d/*.sh 2>/dev/null | wc -l)" == "0" ]] && exit 0
# Append entries
for script in "${HOME}"/.config/launch-menu-apps.d/*.sh; do
echo ":: Sourcing script ${script}"
# shellcheck disable=SC1090
source "${script}"
done
# Sort entries
if [ -n "${MENU_SORT}" ]; then
echo ":: Warning: Sorting is currently broken and is considered experimental"
declare -a apps_sorted=($(
for app in "${!apps[@]}"; do
echo "[\"${key}\"]=${apps[${app}]}"
done | sort
))
unset apps
declare -A apps=()
for app in "${!apps_sorted[@]}"; do
key=$(echo -n "${apps_sorted[${app}]}" | grep -o '\["[A-Za-z]+"\]' || true)
value=$(echo -n "${apps_sorted[${app}]}" | grep -o '"[A-Za-z]+"' || true)
echo ":: COPY BSE: ${apps_sorted[@]}"
echo ":: KEY: ${key}"
echo " VAL: ${value}"
apps+=(
["${app}"]="${apps_sorted[${app}]}"
)
done
fi
# Create new variables
apps_keys=
for app in "${!apps[@]}"; do
if [ -n "${MENU_SORT}" ]; then
echo ":: KEYC KEY: ${app}"
echo " VAL: ${apps[${app}]}"
fi
[[ -n "${apps_keys}" ]] && apps_keys="${apps_keys}\n"
apps_keys=${apps_keys}${app}
done
# Make selection
selection=$(echo -e "${apps_keys}" | ${HOME}/.local/bin/launch-menu "start")
# Execute
# shellcheck disable=SC2086
( eval ${apps[${selection}]} )