feat(wallpaper): add better wallpaper module

This commit is contained in:
Moritz Böhme 2023-05-07 13:53:31 +02:00
parent 326b14d39a
commit 3e9468d872
No known key found for this signature in database
GPG key ID: 970C6E89EB0547A9
8 changed files with 99 additions and 23 deletions

View file

@ -45,7 +45,7 @@ in
focus_follows_pointer = true;
};
startupPrograms = [
"randomWallpaper"
"wallpaper -r"
];
extraConfig = builtins.readFile ./bspwmrc;
};

View file

@ -224,5 +224,5 @@ in
bindm = $mainMod, mouse:272, movewindow
bindm = $mainMod, mouse:273, resizewindow
exec-once=randomWallpaper
exec-once=wallpaper -r
''

View file

@ -71,7 +71,7 @@ in
command = "systemctl --user restart waybar";
always = true;
}
{ command = "randomWallpaper"; }
{ command = "wallpaper -r"; }
];
};
};

View file

@ -0,0 +1,24 @@
{ lib, config, pkgs, ... }:
with lib;
let
cfg = config.my.programs.wallpaper;
in
{
options.my.programs.wallpaper.enable = mkEnableOption "wallpaper";
config = mkIf cfg.enable {
environment.systemPackages =
let
wallpaper = pkgs.writeShellApplication {
name = "wallpaper";
runtimeInputs = with pkgs; [ findutils coreutils feh hyprland jq fzf viu ];
text = builtins.readFile ./wallpaper.sh;
};
in
[
wallpaper
];
};
}

View file

@ -0,0 +1,70 @@
#!/usr/bin/env bash
WALLPAPERS_PATH="$HOME/.config/wallpapers"
WALLPAPERS=$(find "$WALLPAPERS_PATH" -type f,l)
function help() {
echo "Usage: wallpaper [OPTIONS]"
echo ""
echo "Options:"
echo " -h, --help Show this help message and exit"
echo " -r, --random Set a random wallpaper"
echo " -s, --set <PATH> Set a wallpaper"
}
function randomWallpaper() {
find ~/.config/wallpapers/ -type f,l | shuf -n 1
}
function setWallpaper() {
case "$DESKTOP_SESSION" in
hyprland)
hyprctl hyprpaper preload "$1" &>/dev/null
hyprctl monitors -j | jq '.[].name' | xargs -I{} -P 0 hyprctl hyprpaper wallpaper '{}',"$1" &>/dev/null
hyprctl hyprpaper unload all &>/dev/null
;;
*)
feh --bg-fill "$1" &>/dev/null
;;
esac
}
# Parse arguments
# https://stackoverflow.com/a/14203146
POSITIONAL=()
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-h | --help)
help
exit 0
;;
-r | --random)
WALLPAPER=$(randomWallpaper)
shift # past argument
;;
-s | --set)
WALLPAPER="$2"
shift # past argument
shift # past value
;;
*)
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional arguments
if [[ -z ${WALLPAPER+x} ]]; then
WALLPAPER=$(echo "$WALLPAPERS" | fzf --preview="viu -sb -h 30 {}" --preview-window=right:70%:wrap)
fi
if [[ ! -f "$WALLPAPER" ]]; then
echo "File not found: $WALLPAPER"
exit 1
fi
setWallpaper "$WALLPAPER"