1
0
Fork 0

Convert change_wallpaper.sh using nix writeShellApplication

This commit is contained in:
Avery 2024-03-19 00:39:54 +01:00
parent 294fd9dcf3
commit c6651a7eb6
Signed by: Avery
GPG key ID: B684FD451B692E04
4 changed files with 85 additions and 77 deletions

View file

@ -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 ];

View file

@ -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

View file

@ -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"
'';
}

View file

@ -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"