spmenu/scripts/spmenu_profile

92 lines
3.4 KiB
Plaintext
Raw Normal View History

2023-03-29 23:45:54 +02:00
#!/bin/sh
# spmenu_profile
CONFDIR="${XDG_CONFIG_HOME:-$HOME/.config}"
PREFIX="${PREFIX:-/usr}"
2023-03-29 23:45:54 +02:00
load_profile() {
2023-04-04 23:57:47 +02:00
[ -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
2023-03-31 00:37:16 +02:00
[ -n "$PROFILE" ] && xrdb -override "$PROFILE"
2023-03-29 23:45:54 +02:00
}
set_profile() {
pgrep -x spmenu && pkill -x spmenu
profiles="$(find "${CONFDIR}/spmenu/profiles" -type f)"
profilec="$(find "${CONFDIR}/spmenu/profiles" -type f | wc -l)"
2023-03-29 23:45:54 +02:00
2023-04-03 14:57:03 +02:00
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')"
2023-03-29 23:45:54 +02:00
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"
2023-03-29 23:45:54 +02:00
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"
2023-03-29 23:45:54 +02:00
set_profile
}
check() {
case "$sel" in
2023-04-17 20:52:49 +02:00
"<b>Add</b>") add_profile ;;
"<b>Remove</b>") remove_profile ;;
"<b>Default</b>") printf "%s%s/share/spmenu/example.Xresources\n" "${DESTDIR}" "${PREFIX}" > "${CONFDIR}/spmenu/.profile" && set_profile ;;
"<b>None</b>") printf "NONE\n" > "${CONFDIR}/spmenu/.profile" ;;
2023-03-29 23:45:54 +02:00
"") 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"
2023-03-29 23:45:54 +02:00
load_profile
}
list_profiles() {
profiles="$(find "${CONFDIR}/spmenu/profiles" -type f)"
profilec="$(find "${CONFDIR}/spmenu/profiles" -type f | wc -l)"
2023-03-29 23:45:54 +02:00
2023-04-14 13:28:33 +02:00
[ "$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"
2023-03-29 23:45:54 +02:00
for i in $(seq "$profilec"); do
curprofile="$(basename "$(printf "%s" "$profiles" | sed "${i}q;d")")"
printf "\033[1;34m%s\033[0m\n" "$curprofile"
2023-03-29 23:45:54 +02:00
done
[ "$1" != "noopts" ] && list_ex_options
}
list_ex_options() {
2023-04-14 13:28:33 +02:00
printf "\n<b>None</b>\n<b>Default</b>\n<b>Add</b>\n<b>Remove</b>\n"
2023-03-29 23:45:54 +02:00
}
main() {
command -v xrdb > /dev/null || exit 1
[ ! -d "${CONFDIR}/spmenu/profiles" ] && mkdir -p "${CONFDIR}/spmenu/profiles"
2023-03-29 23:45:54 +02:00
[ "$1" = "--spmenu-load-default-profile" ] && load_profile
[ "$1" = "--spmenu-set-profile" ] && set_profile
}
main "$@"