From 0607f9f60727d7c974bbc6cdecaaf3153b304010 Mon Sep 17 00:00:00 2001 From: speedie Date: Wed, 29 Mar 2023 23:45:54 +0200 Subject: [PATCH] add support for profiles --- TODO | 1 + keybinds.h | 1 + libs/arg.c | 7 ++++ libs/arg.h | 1 + libs/argv.c | 6 ++-- scripts/spmenu_profile | 80 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 92 insertions(+), 4 deletions(-) create mode 100755 scripts/spmenu_profile diff --git a/TODO b/TODO index 0bf02ba..be0edae 100644 --- a/TODO +++ b/TODO @@ -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 diff --git a/keybinds.h b/keybinds.h index 04d8358..5d783f6 100644 --- a/keybinds.h +++ b/keybinds.h @@ -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 } }, diff --git a/libs/arg.c b/libs/arg.c index 1f0661f..a3482ec 100644 --- a/libs/arg.c +++ b/libs/arg.c @@ -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); +} diff --git a/libs/arg.h b/libs/arg.h index 8366de9..28f7ab3 100644 --- a/libs/arg.h +++ b/libs/arg.h @@ -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); diff --git a/libs/argv.c b/libs/argv.c index 36286ee..c534593 100644 --- a/libs/argv.c +++ b/libs/argv.c @@ -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 diff --git a/scripts/spmenu_profile b/scripts/spmenu_profile new file mode 100755 index 0000000..ccdd214 --- /dev/null +++ b/scripts/spmenu_profile @@ -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 "$@"