From bb64a10d108fd560af28d31b31f1668c1a1665d8 Mon Sep 17 00:00:00 2001 From: speedie Date: Mon, 5 Jun 2023 22:55:52 +0200 Subject: [PATCH] add actual picker functionality --- wallpaper-spmenu | 75 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 71 insertions(+), 4 deletions(-) diff --git a/wallpaper-spmenu b/wallpaper-spmenu index a2abcda..4a6b584 100755 --- a/wallpaper-spmenu +++ b/wallpaper-spmenu @@ -3,10 +3,15 @@ # Wallpaper picker for X11 and Wayland using spmenu. # Licensed under the GNU General Public License version 3.0 +VERSION="${VERSION:-0.1}" PROTOCOL="${PROTOCOL:-1}" # Protocol to use (0: X11, 1: Wayland/wlroots) LOAD_CONFIG="${LOAD_CONFIG:-true}" # Load config (0/1) WALLPAPER_CONFIG_FILE="${WALLPAPER_CONFIG_FILE:-$HOME/.config/spmenu/wallpaper/config}" # Use same directory as spmenu +WALLPAPER_CONFIG_DIR="$(dirname "$WALLPAPER_CONFIG_FILE")" WALLPAPER_DIR="${WALLPAPER_DIR:-~/Wallpapers}" # Default wallpaper directory +DEFAULT_FEATURE="${DEFAULT_FEATURE:-pw}" +DEFAULT_MODE="${DEFAULT_MODE:-fill}" +ASK_MODE="${ASK_MODE:-false}" die() { printf "%s\n" "$*" && exit 1; } remove_arg() { args="$(printf "%s\n" "$args" | sed "s|$1||g")"; } @@ -19,7 +24,7 @@ exit ${i:-0} } read_args() { - feature="pw" + feature="${DEFAULT_FEATURE}" args="$(printf "%s\n" "$@")" argc="$(printf "%s\n" "$@" | wc -l)" @@ -59,17 +64,20 @@ check_deps() { } write_config() { - dir="$(dirname "$WALLPAPER_CONFIG_FILE")" - mkdir -p "$dir" || die "failed to create directory $dir" + mkdir -p "$WALLPAPER_CONFIG_DIR" || die "failed to create directory $WALLPAPER_CONFIG_DIR" cat << EOF > "$WALLPAPER_CONFIG_FILE" -# wallpaper-spmenu +# wallpaper-spmenu ${VERSION:-0.1} # This is the default config file for wallpaper-spmenu. # You can use any valid shell syntax here. # See https://spmenu.speedie.site wiki for more information. PROTOCOL="${PROTOCOL}" # Default protocol to use (0: X11, 1: Wayland w/ wlroots) WALLPAPER_DIR="${WALLPAPER_DIR}" # Default wallpaper directory, can be set on runtime + +DEFAULT_MODE="${DEFAULT_MODE}" # Default mode (stretch/fit/fill/center/tile) +ASK_MODE="${ASK_MODE}" # Ask how the wallpaper should be set (true/false) +DEFAULT_FEATURE="${DEFAULT_FEATURE}" # Default feature (sw/pw) EOF } @@ -81,8 +89,65 @@ read_config() { . "$WALLPAPER_CONFIG_FILE" } +write_script() { +rm -f "$WALLPAPER_CONFIG_DIR/set_wallpaper.sh" +cat << EOF > "$WALLPAPER_CONFIG_DIR/set_wallpaper.sh" +EOF +chmod +x "$WALLPAPER_CONFIG_DIR/set_wallpaper.sh" +} + pick_wallpaper() { + pick_wallpaper_file + [ -z "$selwal" ] && exit 1 # No file was picked + + [ -e "$WALLPAPER_CONFIG_DIR/currentwallpaper" ] && \ + mv "$WALLPAPER_CONFIG_DIR/currentwallpaper" "$WALLPAPER_CONFIG_DIR/prevwallpaper" + + ln -s "$selwal" "$WALLPAPER_CONFIG_DIR/currentwallpaper" + + write_script +} + +pick_wallpaper_file() { [ ! -d "$WALLPAPER_DIR" ] && WALLPAPER_DIR="${HOME}" + cd "$WALLPAPER_DIR" || exit 1 + + # very much taken from spmenu_run + listing_files() { + command -v pre_list_func > /dev/null && pre_list_func + + ls --color=always > /tmp/spmenu_ls_list + printf "..\n" >> /tmp/spmenu_ls_list + + while read -r l; do + command -v line_func > /dev/null && line_fnc "$(pwd)/$l" + printf "%s\n" "$l" + done < "/tmp/spmenu_ls_list"; rm -f /tmp/spmenu_ls_list + + command -v post_list_func > /dev/null && post_list_func + } + + command -v pre_func > /dev/null && pre_func + + listing_files | spmenu -l 40 -g 1 -p "$(pwd)" | sed -e 's/\x1b\[[0-9;]*m//g' > /tmp/spmenu_out + + while read -r dir; do + case "$dir" in + *) + if [ -d "$dir" ]; then + WALLPAPER_DIR="$(pwd)/$dir" + command -v dir_func > /dev/null && dir_func "$dir" + pick_wallpaper_file + elif [ -f "$dir" ]; then + selwal="$(pwd)/$dir" + else + return 1 # file not found + fi + ;; + esac + + break # TODO: multisel + done < /tmp/spmenu_out; rm -f /tmp/spmenu_out } main() { @@ -95,6 +160,8 @@ main() { "pw") pick_wallpaper ;; esac + printf "%s\n" "$selwal" + return $? }