SBGMENU: convert multiple IFs to an elegant CASE statement. Minor refinements.

This commit is contained in:
Protesilaos Stavrou 2019-02-20 12:09:32 +02:00
parent 93dcb3ef49
commit 2315343ada
No known key found for this signature in database
GPG key ID: 99BD6459CD5CA3EA

View file

@ -20,7 +20,13 @@
# https://gitlab.com/protesilaos/dotfiles
#
# Dependencies on Debian Buster:
# apt install feh suckless-tools
# sudo apt install suckless-tools
#
# For BSPWM sessions, you also need:
# sudo apt install feh
#
# Optional dependency (binary is fdfind):
# sudo apt install fd-find
#
# Assign this script to a key binding for your convenience.
@ -53,11 +59,18 @@ my_dmenu() {
# image, or set it as the new wallpaper.
sbgmenu_mode="$(echo -e 'View images\nSet desktop wallpaper' | my_dmenu 'Choose the sbgmenu mode')"
find_pictures() {
if [ -x /usr/bin/fdfind ]; then
fdfind 'jpg|jpeg|png' "$HOME/Pictures/"
else
find "$HOME/Pictures" -type f \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' \)
fi
}
# Get image files that end in .{jpg,jpeg,png} from ~/Pictures and
# present them as dmenu items.
my_pictures() {
find "$HOME/Pictures" -type f \( -iname '*.jpg' -o -iname '*.jpeg' \
-o -iname '*.png' \) | my_dmenu "$1" "$2"
my_pictures_menu() {
find_pictures | my_dmenu "$1" "$2"
}
# Act depending on the mode of action.
@ -65,36 +78,49 @@ sbgmenu_action() {
# If the option is to view an image, use the default image viewer
# (user defined).
if [ "$sbgmenu_mode" == 'View images' ]; then
sbgmenu_output="$(my_pictures 'View selected picture' 10)"
sbgmenu_output="$(my_pictures_menu 'View selected picture' 10)"
echo "Opening file"
xdg-open "$sbgmenu_output"
# If the decision is to set a wallpaper, prepare the relevant
# command based on the running session. Note that the bspwm session
# is not standardised. Using `feh` is my way of doing things.
elif [ "$sbgmenu_mode" == 'Set desktop wallpaper' ]; then
sbgmenu_output="$(my_pictures 'Set new desktop wallpaper' 10)"
echo "Changing wallpaper"
sbgmenu_output="$(my_pictures_menu 'Set new desktop wallpaper' 10)"
# Copy the selected image to the home directory, as a hidden
# file.
cp -f "$sbgmenu_output" "$HOME/.wallpaper.jpg"
case "$DESKTOP_SESSION" in
bspwm)
# NOTE there is no standard way for defining a wallpaper in
# BSPWM. Using a hidden file from the home dir is specific
# to my custom desktop session.
#
# Copy the selected image to the home directory, as a hidden
# file.
#
# Dependency on Debian Buster:
# sudo apt install feh
cp -f "$sbgmenu_output" "$HOME/.wallpaper.jpg"
feh --bg-fill "$HOME/.wallpaper.jpg"
;;
gnome)
gsettings set org.gnome.desktop.background picture-uri "file://$sbgmenu_output"
;;
mate)
gsettings set org.mate.background picture-filename "$sbgmenu_output"
;;
xfce)
# TODO any way to define a wallpaper in Xfce without
# specifying screen, monitor, workspace?
xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor0/workspace0/last-image -s "$sbgmenu_output"
;;
*)
echo "ERROR. Unknown Desktop Session."
echo "Cannot set wallpaper."
echo "Aborting..."
exit 1
;;
esac
# TODO make this a case statement
if [ "$DESKTOP_SESSION" == 'bspwm' ]; then
feh --bg-fill "$HOME/.wallpaper.jpg"
elif [ "$DESKTOP_SESSION" == 'gnome' ]; then
gsettings set org.gnome.desktop.background picture-uri "file://$sbgmenu_output"
elif [ "$DESKTOP_SESSION" == 'mate' ]; then
gsettings set org.mate.background picture-filename "$sbgmenu_output"
elif [ "$DESKTOP_SESSION" == 'xfce' ]; then
xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor0/workspace0/last-image -s "$sbgmenu_output"
# Abort if the session is not known. Print error message.
else
echo "ERROR. Unknown Desktop Session."
echo "Cannot set wallpaper."
echo "Aborting..."
exit 1
fi
echo "Changing wallpaper to $sbgmenu_output"
# If neither of the available modes is selected, abort sbgmenu
else
exit 1