142 lines
3.2 KiB
Bash
Executable file
142 lines
3.2 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
|
|
# Define associative array
|
|
declare -A apps=()
|
|
|
|
# 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}"
|
|
}
|
|
function add_file() {
|
|
is_file "${2}" && add_app "${1}" "${2}"
|
|
}
|
|
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"
|
|
#!/usr/bin/env bash
|
|
|
|
apps+=(
|
|
["Example application"]="example-application"
|
|
["Test script"]="${HOME}/.local/bin/test-script"
|
|
)
|
|
EOF
|
|
)
|
|
|
|
# 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
|
|
|
|
# Sort entries
|
|
if [ -n "${MENU_SORT}" ]; then
|
|
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
|
|
|
|
# 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
|
|
echo ":: Invoking 'launch-menu'"
|
|
selection=$(echo -e "${apps_keys}" | ${HOME}/.local/bin/launch-menu "start")
|
|
|
|
# Terminate if no selection was made
|
|
[ -z "${selection}" ] && exit 0
|
|
|
|
# Execute
|
|
echo ":: Executing"
|
|
# shellcheck disable=SC2086
|
|
( eval ${apps[${selection}]} )
|