add support for profiles

This commit is contained in:
speedie 2023-03-29 23:45:54 +02:00
parent 25235a5c50
commit 0607f9f607
6 changed files with 92 additions and 4 deletions

1
TODO
View file

@ -6,3 +6,4 @@
- Contextual completions
- Use cairo for text drawing over Xft
- MAYBE wayland support, but only if it doesn't require writing any extra code which as of now seems unlikely
- Write documentation about profiles

View file

@ -92,6 +92,7 @@ static Key keys[] = {
{ 0, SHIFT, XK_g, moveend, {0} },
{ 0, 0, XK_Next, movenext, {0} },
{ 0, 0, XK_Prior, moveprev, {0} },
{ 0, CONTROL|SHIFT, XK_p, setprofile, {0} },
{ 0, MODIFIER1, XK_p, navhistory, {.i = -1 } },
{ 0, MODIFIER1, XK_n, navhistory, {.i = +1 } },

View file

@ -505,3 +505,10 @@ setcolumns(const Arg *arg)
resizeclient();
drawmenu();
}
void
setprofile(const Arg *arg)
{
int ax = system("command -v spmenu_profile > /dev/null && spmenu_profile --spmenu-set-profile > /dev/null");
exit(ax);
}

View file

@ -37,3 +37,4 @@ static void setimgpos(const Arg *arg);
static void setimggaps(const Arg *arg);
static void setlines(const Arg *arg);
static void setcolumns(const Arg *arg);
static void setprofile(const Arg *arg);

View file

@ -5,7 +5,6 @@ readargs(int argc, char *argv[])
int j = 0;
int cxrdb = 0;
int hxrdb = 0;
// check if we should load the xrdb/config, because it needs to be loaded before arguments are checked (internal -> xresources -> arguments)
for (j = 1; j < argc; j++) {
@ -25,12 +24,11 @@ readargs(int argc, char *argv[])
XrmInitialize();
if (loadconfig) {
cxrdb = system("[ -e $HOME/.config/spmenu/spmenurc ] && xrdb -override $HOME/.config/spmenu/spmenurc");
hxrdb = system("[ -e $HOME/.spmenurc ] && xrdb -override $HOME/.spmenurc");
cxrdb = system("command -v spmenu_profile > /dev/null && spmenu_profile --spmenu-load-default-profile > /dev/null");
}
// avoid an annoying warning gcc will spit out when you don't care about the result
if (!cxrdb || !hxrdb || cxrdb || hxrdb || xresources) load_xresources();
if (!cxrdb || cxrdb || xresources) load_xresources();
}
// no arguments

80
scripts/spmenu_profile Executable file
View file

@ -0,0 +1,80 @@
#!/bin/sh
# spmenu_profile
load_profile() {
[ -f "$HOME/.config/spmenu/.profile" ] && PROFILE="$(cat "$HOME/.config/spmenu/.profile")"
[ -z "$PROFILE" ] && xrdb -override "$HOME/.config/spmenu/spmenurc" || xrdb -override "$PROFILE"
}
set_profile() {
pgrep -x spmenu && pkill -x spmenu
profiles="$(find "$HOME/.config/spmenu/profiles" -type f)"
profilec="$(find "$HOME/.config/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-cursor --hide-highlighting --normal --no-allow-typing)"
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-cursor --hide-highlighting --insert | tr -d ' ')"
[ -f "/usr/share/spmenu/example.Xresources" ] && cp "/usr/share/spmenu/example.Xresources" "$HOME/.config/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 --insert)"
[ ! -f "$HOME/.config/spmenu/profiles/$selprofile" ] && return
rm -f "$HOME/.config/spmenu/profiles/$selprofile" "$HOME/.config/spmenu/.profile"
set_profile
}
check() {
case "$sel" in
"Add") add_profile ;;
"Remove") remove_profile ;;
"Default") printf "%s/.config/spmenu/spmenurc\n" "$HOME" > "$HOME/.config/spmenu/.profile" && set_profile ;;
"") exit 0 ;;
*) profile="$sel" && write_profile && set_profile ;;
esac
return
}
write_profile() {
printf "%s/%s\n" "$HOME/.config/spmenu/profiles/" "$profile" > "$HOME/.config/spmenu/.profile"
[ ! -f "$HOME/.config/spmenu/profiles/$profile" ] && rm -f "$HOME/.config/spmenu/.profile"
load_profile
}
list_profiles() {
profiles="$(find "$HOME/.config/spmenu/profiles" -type f)"
profilec="$(find "$HOME/.config/spmenu/profiles" -type f | wc -l)"
[ "$1" != "noopts" ] && [ -f "$HOME/.config/spmenu/.profile" ] && printf "\033[1;33m%s\n" "Current profile: $(basename "$(cat "$HOME/.config/spmenu/.profile")")" | sed "s/spmenurc/Default/g"
for i in $(seq "$profilec"); do
curprofile="$(basename "$(printf "%s" "$profiles" | sed "${i}q;d")")"
printf "%s\n" "$curprofile"
done
[ "$1" != "noopts" ] && list_ex_options
}
list_ex_options() {
printf "Default\nAdd\nRemove\n"
}
main() {
command -v xrdb > /dev/null || exit 1
[ ! -d "$HOME/.config/spmenu/profiles" ] && mkdir -p "$HOME/.config/spmenu/profiles"
[ ! -f "$HOME/.config/spmenu/spmenurc" ] && [ -e "/usr/share/spmenu/example.Xresources" ] && \
cp "/usr/share/spmenu/example.Xresources" "$HOME/.config/spmenu/spmenurc"
[ "$1" = "--spmenu-load-default-profile" ] && load_profile
[ "$1" = "--spmenu-set-profile" ] && set_profile
}
main "$@"