spmenu/scripts/spmenu_profile
2023-04-14 13:28:33 +02:00

92 lines
3.4 KiB
Bash
Executable file
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/sh
# spmenu_profile
CONFDIR="${XDG_CONFIG_HOME:-$HOME/.config}"
PREFIX="${PREFIX:-/usr}"
load_profile() {
[ -f "${CONFDIR}/spmenu/.profile" ] && [ "$(cat "${CONFDIR}/spmenu/.profile")" = "NONE" ] && NONE=true
if [ "$NONE" = "true" ]; then
return
fi
[ -f "${CONFDIR}/spmenu/.profile" ] && \
PROFILE="$(cat "${CONFDIR}/spmenu/.profile")"
[ -f "$HOME/.spmenurc" ] && xrdb -override "$HOME/.spmenurc" # legacy config
[ -n "$PROFILE" ] && xrdb -override "$PROFILE"
}
set_profile() {
pgrep -x spmenu && pkill -x spmenu
profiles="$(find "${CONFDIR}/spmenu/profiles" -type f)"
profilec="$(find "${CONFDIR}/spmenu/profiles" -type f | wc -l)"
sel="$(list_profiles list | spmenu --lines 20 --columns 1 --prompt "Profile settings" --hide-match-count --hide-left-arrow --hide-right-arrow --hide-image --hide-mode --hide-highlighting --normal | sed -e 's/\x1b\[[0-9;]*m//g')"
check
}
add_profile() {
selprofile="$(printf "" | spmenu --lines 20 --columns 1 --prompt "Enter a name for the profile:" --hide-mode --hide-match-count --hide-left-arrow --hide-right-arrow --hide-image --hide-highlighting --insert | tr -d ' ' | sed -e 's/\x1b\[[0-9;]*m//g')"
[ -f "${DESTDIR}${PREFIX}/share/spmenu/example.Xresources" ] && cp "${DESTDIR}${PREFIX}/share/spmenu/example.Xresources" "${CONFDIR}/spmenu/profiles/$selprofile"
set_profile
}
remove_profile() {
selprofile="$(list_profiles noopts | spmenu --lines 20 --columns 1 --prompt "Select a profile to remove" --hide-mode --hide-match-count --hide-left-arrow --hide-right-arrow --hide-image --hide-highlighting --normal | sed -e 's/\x1b\[[0-9;]*m//g')"
[ ! -f "${CONFDIR}/spmenu/profiles/$selprofile" ] && return
rm -f "${CONFDIR}/spmenu/profiles/$selprofile" "${CONFDIR}/spmenu/.profile"
set_profile
}
check() {
case "$sel" in
"Add") add_profile ;;
"Remove") remove_profile ;;
"Default") printf "%s%s/share/spmenu/example.Xresources\n" "${DESTDIR}" "${PREFIX}" > "${CONFDIR}/spmenu/.profile" && set_profile ;;
"None") printf "NONE\n" > "${CONFDIR}/spmenu/.profile" ;;
"") exit 0 ;;
*) profile="$sel" && write_profile && set_profile ;;
esac
return
}
write_profile() {
printf "%s/%s\n" "${CONFDIR}/spmenu/profiles/" "$profile" > "${CONFDIR}/spmenu/.profile"
[ ! -f "${CONFDIR}/spmenu/profiles/$profile" ] && rm -f "${CONFDIR}/spmenu/.profile"
load_profile
}
list_profiles() {
profiles="$(find "${CONFDIR}/spmenu/profiles" -type f)"
profilec="$(find "${CONFDIR}/spmenu/profiles" -type f | wc -l)"
[ "$1" != "noopts" ] && [ -f "${CONFDIR}/spmenu/.profile" ] && printf "\033[1;33m<b>%s</b>\n" "Current profile: $(basename "$(cat "${CONFDIR}/spmenu/.profile")")" | sed "s/spmenurc/Default/g; s/NONE/None/g; s/example.Xresources/Default/g"
for i in $(seq "$profilec"); do
curprofile="$(basename "$(printf "%s" "$profiles" | sed "${i}q;d")")"
printf "\033[1;34m%s\033[0m\n" "$curprofile"
done
[ "$1" != "noopts" ] && list_ex_options
}
list_ex_options() {
printf "\n<b>None</b>\n<b>Default</b>\n<b>Add</b>\n<b>Remove</b>\n"
}
main() {
command -v xrdb > /dev/null || exit 1
[ ! -d "${CONFDIR}/spmenu/profiles" ] && mkdir -p "${CONFDIR}/spmenu/profiles"
[ "$1" = "--spmenu-load-default-profile" ] && load_profile
[ "$1" = "--spmenu-set-profile" ] && set_profile
}
main "$@"