From c6651a7eb6ccac1c4f093a7f52d4ca6ede72862a Mon Sep 17 00:00:00 2001 From: Avery Date: Tue, 19 Mar 2024 00:39:54 +0100 Subject: [PATCH] Convert change_wallpaper.sh using nix writeShellApplication --- hosts/totsugeki/desktop/default.nix | 1 + hosts/totsugeki/desktop/hyprland.nix | 6 +- .../desktop/scripts/change-wallpaper.nix | 81 +++++++++++++++++++ .../scripts_legacy/bin/change_wallpaper.sh | 74 ----------------- 4 files changed, 85 insertions(+), 77 deletions(-) create mode 100644 hosts/totsugeki/desktop/scripts/change-wallpaper.nix delete mode 100755 hosts/totsugeki/desktop/scripts_legacy/bin/change_wallpaper.sh diff --git a/hosts/totsugeki/desktop/default.nix b/hosts/totsugeki/desktop/default.nix index a9b7628..22f174e 100644 --- a/hosts/totsugeki/desktop/default.nix +++ b/hosts/totsugeki/desktop/default.nix @@ -39,6 +39,7 @@ wl-clipboard (import ./scripts/colorpicker.nix { inherit pkgs; }) (import ./scripts/currently-playing.nix { inherit pkgs; }) + (import ./scripts/change-wallpaper.nix { inherit pkgs; }) (pkgs.buildEnv { name = "desktop-scripts"; paths = [ ./scripts_legacy ]; diff --git a/hosts/totsugeki/desktop/hyprland.nix b/hosts/totsugeki/desktop/hyprland.nix index 317690a..06f1e48 100644 --- a/hosts/totsugeki/desktop/hyprland.nix +++ b/hosts/totsugeki/desktop/hyprland.nix @@ -158,9 +158,9 @@ extraConfig = '' submap = Fondo de pantalla - bind = , H, exec, change_wallpaper.sh previous - bind = , L, exec, change_wallpaper.sh next - bind = , M, exec, change_wallpaper.sh open-menu + bind = , H, exec, change-wallpaper previous + bind = , L, exec, change-wallpaper next + bind = , M, exec, change-wallpaper open-menu bindl = , escape, submap, reset bindl = MOD3, w, submap, reset diff --git a/hosts/totsugeki/desktop/scripts/change-wallpaper.nix b/hosts/totsugeki/desktop/scripts/change-wallpaper.nix new file mode 100644 index 0000000..2d3828d --- /dev/null +++ b/hosts/totsugeki/desktop/scripts/change-wallpaper.nix @@ -0,0 +1,81 @@ +{ pkgs }: + +pkgs.writeShellApplication { + name = "change-wallpaper"; + runtimeInputs = with pkgs; [ dunst rofi swww ]; + text = '' + shopt -s nullglob + + WALLPAPER_PATH="$HOME/.local/share/wallpapers" + WALLPAPERS=("$WALLPAPER_PATH"/*) + + if [ ! -f "$WALLPAPER_PATH/.current_path" ]; then + # Current wallpaper file does not exist, create it + echo "''${WALLPAPERS[0]}" > "$WALLPAPER_PATH/.current_path" + fi + + CURRENT_WALLPAPER=$(cat "$WALLPAPER_PATH/.current_path") + CURRENT_WP_INDEX=-1 + # Get the current wallpaper's index + for index in "''${!WALLPAPERS[@]}"; do + if [ "''${WALLPAPERS[$index]}" = "$CURRENT_WALLPAPER" ]; then + CURRENT_WP_INDEX=$index + fi + done + + if [ "$CURRENT_WP_INDEX" -eq -1 ]; then + CURRENT_WP_INDEX=0 + fi + + if [ "$1" = "previous" ]; then + WALLPAPER="''${WALLPAPERS[$CURRENT_WP_INDEX - 1]}" + TRANSITION_ANGLE=300 + elif [ "$1" = "next" ]; then + NEXT_INDEX=$((CURRENT_WP_INDEX + 1)) + if [ "$NEXT_INDEX" = "''${#WALLPAPERS[@]}" ]; then + NEXT_INDEX=0 + fi + WALLPAPER=''${WALLPAPERS[$NEXT_INDEX]} + TRANSITION_ANGLE=120 + elif [ "$1" = "open-menu" ]; then + INPUT="" + for index in "''${!WALLPAPERS[@]}"; do + if [ "$index" = "$CURRENT_WP_INDEX" ]; then + continue + fi + _WALLPAPER=''${WALLPAPERS[$index]} + _WALLPAPER_NAME=$(basename "$_WALLPAPER") + INPUT+="$_WALLPAPER_NAME\0icon\x1f$_WALLPAPER" + if [ ! $((index + 1)) -eq ''${#WALLPAPERS[@]} ]; then + INPUT+=$'\n' + fi + done + if ! WALLPAPER=$(echo -en "$INPUT" | rofi -dmenu -mesg "Cambiar fondo de pantalla" -p "" -i -theme ~/.config/rofi/wallpaper_selector.rasi); then + exit 1 + fi + WALLPAPER="$WALLPAPER_PATH/$WALLPAPER" + TRANSITION_ANGLE=270 + else + echo "Unknown first argument: $1" + exit 1 + fi + + if [ -z "$WALLPAPER" ]; then + exit 1 + fi + + WALLPAPER_NAME=$(basename "$WALLPAPER") + echo "Setting wallpaper to $WALLPAPER" + echo "$WALLPAPER" > "$WALLPAPER_PATH/.current_path" + ln -sf "$WALLPAPER" "$WALLPAPER_PATH/.current_image" + dunstify -i "$WALLPAPER" -t 2800 "Cambiando fondo de pantalla" "Cambiando a $WALLPAPER_NAME" + swww img\ + --transition-type wipe\ + --transition-angle $TRANSITION_ANGLE\ + --transition-step 90\ + --transition-duration 1\ + --transition-fps 165\ + "$WALLPAPER" + + ''; +} diff --git a/hosts/totsugeki/desktop/scripts_legacy/bin/change_wallpaper.sh b/hosts/totsugeki/desktop/scripts_legacy/bin/change_wallpaper.sh deleted file mode 100755 index 67fbf41..0000000 --- a/hosts/totsugeki/desktop/scripts_legacy/bin/change_wallpaper.sh +++ /dev/null @@ -1,74 +0,0 @@ -#!/bin/sh - -shopt -s nullglob - -WALLPAPER_PATH=~/.local/share/wallpapers -WALLPAPERS=($WALLPAPER_PATH/*) - -if [ ! -f "$WALLPAPER_PATH/.current_path" ]; then - # Current wallpaper file does not exist, create it - echo ${WALLPAPERS[0]} > "$WALLPAPER_PATH/.current_path" -fi - -CURRENT_WALLPAPER=$(cat "$WALLPAPER_PATH/.current_path") -# Get the current wallpaper's index -for index in ${!WALLPAPERS[@]}; do - if [ "${WALLPAPERS[$index]}" = "$CURRENT_WALLPAPER" ]; then - CURRENT_WP_INDEX=$index - fi -done - -if [ -z $CURRENT_WP_INDEX ]; then - CURRENT_WP_INDEX=0 -fi - -if [ $1 = "previous" ]; then - WALLPAPER=${WALLPAPERS[$CURRENT_WP_INDEX - 1]} - TRANSITION_ANGLE=300 -elif [ $1 = "next" ]; then - NEXT_INDEX=$((CURRENT_WP_INDEX + 1)) - if [ $NEXT_INDEX = ${#WALLPAPERS[@]} ]; then - NEXT_INDEX=0 - fi - WALLPAPER=${WALLPAPERS[$NEXT_INDEX]} - TRANSITION_ANGLE=120 -elif [ $1 = "open-menu" ]; then - INPUT=$'' - for index in ${!WALLPAPERS[@]}; do - if [ $index = $CURRENT_WP_INDEX ]; then - continue - fi - _WALLPAPER="${WALLPAPERS[$index]}" - _WALLPAPER_NAME=$(basename "$_WALLPAPER") - INPUT+="$_WALLPAPER_NAME\0icon\x1f$_WALLPAPER" - if [ ! $(($index + 1)) = ${#WALLPAPERS[@]} ]; then - INPUT+=$'\n' - fi - done - WALLPAPER=$(echo -en "$INPUT" | rofi -dmenu -mesg "Cambiar fondo de pantalla" -p "" -i -theme ~/.config/rofi/wallpaper_selector.rasi) - if [ ! $? = 0 ]; then - exit 1 - fi - WALLPAPER="${WALLPAPER_PATH}/${WALLPAPER}" - TRANSITION_ANGLE=270 -else - echo "Unknown first argument: $1" - exit 1 -fi - -if [ -z $WALLPAPER ]; then - exit 1 -fi - -WALLPAPER_NAME=$(basename "$WALLPAPER") -echo "Setting wallpaper to $WALLPAPER, args: $@" -echo "$WALLPAPER" > "$WALLPAPER_PATH/.current_path" -ln -sf "$WALLPAPER" "$WALLPAPER_PATH/.current_image" -dunstify -i "$WALLPAPER" -t 2800 "Cambiando fondo de pantalla" "Cambiando a $WALLPAPER_NAME" -swww img\ - --transition-type wipe\ - --transition-angle $TRANSITION_ANGLE\ - --transition-step 90\ - --transition-duration 1\ - --transition-fps 165\ - "$WALLPAPER"