add actual picker functionality

This commit is contained in:
speedie 2023-06-05 22:55:52 +02:00
parent 197a9eb3ac
commit bb64a10d10

View file

@ -3,10 +3,15 @@
# Wallpaper picker for X11 and Wayland using spmenu. # Wallpaper picker for X11 and Wayland using spmenu.
# Licensed under the GNU General Public License version 3.0 # 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) PROTOCOL="${PROTOCOL:-1}" # Protocol to use (0: X11, 1: Wayland/wlroots)
LOAD_CONFIG="${LOAD_CONFIG:-true}" # Load config (0/1) 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_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 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; } die() { printf "%s\n" "$*" && exit 1; }
remove_arg() { args="$(printf "%s\n" "$args" | sed "s|$1||g")"; } remove_arg() { args="$(printf "%s\n" "$args" | sed "s|$1||g")"; }
@ -19,7 +24,7 @@ exit ${i:-0}
} }
read_args() { read_args() {
feature="pw" feature="${DEFAULT_FEATURE}"
args="$(printf "%s\n" "$@")" args="$(printf "%s\n" "$@")"
argc="$(printf "%s\n" "$@" | wc -l)" argc="$(printf "%s\n" "$@" | wc -l)"
@ -59,17 +64,20 @@ check_deps() {
} }
write_config() { write_config() {
dir="$(dirname "$WALLPAPER_CONFIG_FILE")" mkdir -p "$WALLPAPER_CONFIG_DIR" || die "failed to create directory $WALLPAPER_CONFIG_DIR"
mkdir -p "$dir" || die "failed to create directory $dir"
cat << EOF > "$WALLPAPER_CONFIG_FILE" cat << EOF > "$WALLPAPER_CONFIG_FILE"
# wallpaper-spmenu # wallpaper-spmenu ${VERSION:-0.1}
# This is the default config file for wallpaper-spmenu. # This is the default config file for wallpaper-spmenu.
# You can use any valid shell syntax here. # You can use any valid shell syntax here.
# See https://spmenu.speedie.site wiki for more information. # See https://spmenu.speedie.site wiki for more information.
PROTOCOL="${PROTOCOL}" # Default protocol to use (0: X11, 1: Wayland w/ wlroots) PROTOCOL="${PROTOCOL}" # Default protocol to use (0: X11, 1: Wayland w/ wlroots)
WALLPAPER_DIR="${WALLPAPER_DIR}" # Default wallpaper directory, can be set on runtime 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 EOF
} }
@ -81,8 +89,65 @@ read_config() {
. "$WALLPAPER_CONFIG_FILE" . "$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() {
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}" [ ! -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() { main() {
@ -95,6 +160,8 @@ main() {
"pw") pick_wallpaper ;; "pw") pick_wallpaper ;;
esac esac
printf "%s\n" "$selwal"
return $? return $?
} }