punktdateien/bash/bin/launch-menu-apps

143 lines
3.2 KiB
Text
Raw Permalink Normal View History

2024-06-03 22:41:10 +02:00
#!/usr/bin/env bash
# Define associative array
declare -A apps=()
2024-10-20 04:07:58 +02:00
# Utility methods
# -> Checking
function is_binary() {
if ! which "${1}" &>/dev/null; then
return 1
else
return 0
fi
}
function is_file() {
if [ ! -f "${1}" ]; then
return 1
else
return 0
fi
}
function is_not_file() {
if [ -f "${1}" ]; then
return 1
else
return 0
fi
}
function is_flatpak() {
if ! flatpak info "${1}" &>/dev/null; then
return 1
else
return 0
fi
}
# -> Adding
function add_app() {
apps+=( ["${1}"]="${2}" )
}
function add_binary() {
is_binary "${2}" && add_app "${1}" "${2}"
}
2024-10-22 21:15:57 +02:00
function add_file() {
is_file "${2}" && add_app "${1}" "${2}"
}
2024-10-20 04:07:58 +02:00
function add_flatpak() {
is_flatpak "${2}" && add_app "${1}" "flatpak run ${2}"
}
if [ -f "/tmp/launch-menu-apps_appcache.tmp" ]; then
# Load cache
echo ":: Loading cache"
chmod +x "/tmp/launch-menu-apps_appcache.tmp"
source "/tmp/launch-menu-apps_appcache.tmp"
else
# Create template
[[ ! -d "${HOME:?}/.config/launch-menu-apps.d" ]] && (
echo ":: Creating template script"
mkdir -p "${HOME}/.config/launch-menu-apps.d"
cat << EOF >> "${HOME}/.config/launch-menu-apps.d/template.sh.disabled"
2024-06-03 22:41:10 +02:00
#!/usr/bin/env bash
2024-10-20 04:07:58 +02:00
apps+=(
2024-06-03 22:41:10 +02:00
["Example application"]="example-application"
["Test script"]="${HOME}/.local/bin/test-script"
)
EOF
2024-10-20 04:07:58 +02:00
)
# Do nothing if no scripts could be found to source
# shellcheck disable=SC2012
[[ "$(ls -1 "${HOME}"/.config/launch-menu-apps.d/*.sh 2>/dev/null | wc -l)" == "0" ]] && exit 0
# Append entries by sourcing all scripts
for script in "${HOME}"/.config/launch-menu-apps.d/*.sh; do
echo ":: Sourcing script ${script}"
# shellcheck disable=SC1090
source "${script}"
done
# Enable failure detection
# Enabling this before sourcing all scripts will
# cause issues, that's why we're doing it here.
set -eo pipefail
# Cache entries
echo ":: Caching entries"
echo "apps+=(" > /tmp/launch-menu-apps_appcache.tmp
for app in "${!apps[@]}"; do
echo " [\"${app}\"]=\"${apps[${app}]}\"" >> /tmp/launch-menu-apps_appcache.tmp
done
echo ")" >> /tmp/launch-menu-apps_appcache.tmp
fi
2024-06-03 22:41:10 +02:00
# Sort entries
if [ -n "${MENU_SORT}" ]; then
2024-10-20 04:07:58 +02:00
echo ":: Sorting entries"
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
2024-06-03 22:41:10 +02:00
# Create new variables
apps_keys=
for app in "${!apps[@]}"; do
2024-10-20 04:07:58 +02:00
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}
2024-06-03 22:41:10 +02:00
done
# Make selection
2024-10-20 04:07:58 +02:00
echo ":: Invoking 'launch-menu'"
selection=$(echo -e "${apps_keys}" | ${HOME}/.local/bin/launch-menu "start")
2024-06-03 22:41:10 +02:00
2024-10-20 04:07:58 +02:00
# Terminate if no selection was made
[ -z "${selection}" ] && exit 0
2024-06-03 22:41:10 +02:00
# Execute
2024-10-20 04:07:58 +02:00
echo ":: Executing"
2024-06-03 22:41:10 +02:00
# shellcheck disable=SC2086
( eval ${apps[${selection}]} )