spmenu/scripts/spmenu_profile

84 lines
3.2 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" ] && PROFILE="$(cat "${CONFDIR}/spmenu/.profile")"
[ -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-input --hide-image --hide-mode --hide-cursor --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/.config/spmenu/spmenurc\n" "$HOME" > "${CONFDIR}/spmenu/.profile" && set_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%s\n" "Current profile: $(basename "$(cat "${CONFDIR}/spmenu/.profile")")" | sed "s/spmenurc/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 "\nDefault\nAdd\nRemove\n"
}
main() {
command -v xrdb > /dev/null || exit 1
[ ! -d "${CONFDIR}/spmenu/profiles" ] && mkdir -p "${CONFDIR}/spmenu/profiles"
[ ! -f "${CONFDIR}/spmenu/spmenurc" ] && [ -e "${DESTDIR}${PREFIX}/share/spmenu/example.Xresources" ] && \
cp "${DESTDIR}${PREFIX}/share/spmenu/example.Xresources" "${CONFDIR}/spmenu/spmenurc"
[ "$1" = "--spmenu-load-default-profile" ] && load_profile
[ "$1" = "--spmenu-set-profile" ] && set_profile
}
main "$@"