Skip to content

Conversation

@elkcityhazard
Copy link

Add a check for mime-type in documentation for "Get a random wallpaper that is not the current one".

This prevents hyprpaper from trying to load a .git directory, or other mime types and crashing.

Add a check for mime-type in documentation for "Get a random wallpaper that is not the current one".  

This prevents hyprpaper from trying to load a .git directory, or other mime types and crashing.
Copy link
Member

@fufexan fufexan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just keep shuffling until we find a good mimetype?

Optionally we could configure a maximum count and if we hit it without finding a mimetype, show the error. This way, the script does not run forever in case of failures.

@elkcityhazard
Copy link
Author

#!/bin/bash
ATTEMPTS=0
MAX_NUM=10
FILES=()
WALLPAPER_DIR="/usr/share/backgrounds"
CURRENT_WALL=$(hyprctl --instance 0 hyprpaper listloaded)
WALLPAPER=""

while true; do
    ((ATTEMPTS++))
    echo "Attempt #$ATTEMPTS"

    if (( ATTEMPTS > MAX_NUM )); then
        notify-send "Error: we tried to set the following files: ${FILES[*]}"
        exit 1
    fi

    WALLPAPER=$(find "$WALLPAPER_DIR" -type f ! -name "$(basename "$CURRENT_WALL")" | shuf -n 1)
    if [[ -z "$WALLPAPER" ]]; then
        notify-send "Error: No suitable wallpapers found."
        exit 1
    fi

    MIME_TYPE=$(file --mime-type -b "$WALLPAPER")
    if [[ ! "$MIME_TYPE" =~ ^image/ ]]; then
        echo "$WALLPAPER - $MIME_TYPE"
        FILES+=("$WALLPAPER")
        notify-send "WARNING" "The selected file '$WALLPAPER' is not an image (MIME type: $MIME_TYPE)."
    else
        echo "Setting: $WALLPAPER"
        # Apply the selected wallpaper
        hyprctl --instance 0 hyprpaper reload ,"$WALLPAPER"
        break
    fi
done

fixed my whoops of changing original shebang
Copy link
Member

@fufexan fufexan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about --instance 0 being everywhere. I guess it should be noted above/below the script that if it is run in an environment without HIS present, it should be added.

ATTEMPTS=0
MAX_NUM=10
FILES=()
WALLPAPER_DIR="/usr/share/backgrounds"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also looks weird. Should be left as the user's home.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sound feedback, I will get that updated when I can.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants