more convenient wallpaper select/fetch

This commit is contained in:
2025-12-01 12:16:03 +03:00
parent 4ff652145b
commit abbea47279
7 changed files with 205 additions and 9 deletions
@@ -1,58 +0,0 @@
#!/usr/bin/env bash
set -euo pipefail
dest_dir="$HOME/Pictures/Wallpapers"
rmflag=0
usage() {
echo "Usage: $0 [-r] <src-file> [src-file ...]"
echo " -r remove source file after successful conversion"
exit 2
}
while getopts ":r" opt; do
case "$opt" in
r) rmflag=1 ;;
*) usage ;;
esac
done
shift $((OPTIND-1))
# Require at least one file
if [ "$#" -lt 1 ]; then
echo "Error: no source files provided"
usage
fi
# Check ImageMagick
if ! command -v magick >/dev/null 2>&1; then
echo "Error: ImageMagick 'magick' command not found"
exit 1
fi
mkdir -p "$dest_dir"
timestamp=$(date +%Y%m%d%H%M%S)
counter=0
# Process each file one by one
for src in "$@"; do
if [ ! -e "$src" ]; then
echo "Error: '$src' does not exist, skipping"
continue
fi
counter=$((counter + 1))
dst="$dest_dir/wallpaper${timestamp}_${counter}.png"
if magick -- "$src" "$dst"; then
echo "Created: $dst"
if [ "$rmflag" -eq 1 ]; then
rm -f -- "$src"
fi
else
echo "Error: conversion failed for '$src'"
fi
done
exit 0
-109
View File
@@ -1,109 +0,0 @@
#!/usr/bin/env bash
# /home/krosh/Documents/Github/nixos-config/home-manager/modules/zsh/next_wallpaper.sh
# Pick next or random wallpaper from ~/Pictures/Wallpapers and update symlink ~/Pictures/wallpaper.png
# Then call: swww img "$WALLPAPER"
set -euo pipefail
RANDOM_MODE=0
while getopts "r" opt; do
case "$opt" in
r) RANDOM_MODE=1 ;;
*) echo "Usage: $0 [-r]" >&2; exit 2 ;;
esac
done
WALLPAPERS_DIR="$HOME/Pictures/Wallpapers"
SYMLINK="$HOME/Pictures/wallpaper.png"
if [ ! -d "$WALLPAPERS_DIR" ]; then
echo "Wallpapers directory not found: $WALLPAPERS_DIR" >&2
exit 1
fi
# Resolve current wallpaper (target of symlink). If not a symlink or missing, CUR will be empty.
CUR="$(readlink -f "$SYMLINK" 2>/dev/null || true)"
# Collect png files; sort by timestamp ascending (oldest first) using ls -tr.
# If there are spaces in names, ls handles them, but filenames with newlines are unsupported.
mapfile -t FILES < <(find "$WALLPAPERS_DIR" -maxdepth 1 -type f -iname '*.png' -print0 | xargs -0 ls -1tr 2>/dev/null || true)
if [ "${#FILES[@]}" -eq 0 ]; then
echo "No png wallpapers found in $WALLPAPERS_DIR" >&2
exit 1
fi
choose_random() {
# pick random file different from CUR if possible
candidates=()
for f in "${FILES[@]}"; do
[ "$(readlink -f "$f")" = "$CUR" ] && continue
candidates+=("$f")
done
if [ "${#candidates[@]}" -eq 0 ]; then
# only current exists
echo "No other wallpaper to choose (only current exists)." >&2
exit 1
fi
idx=$((RANDOM % ${#candidates[@]}))
echo "${candidates[$idx]}"
}
choose_next() {
# find current in sorted list and pick next (wrap). If current not found, choose first.
n=${#FILES[@]}
if [ "$n" -eq 1 ]; then
# only one file
if [ "$(readlink -f "${FILES[0]}")" = "$CUR" ]; then
echo "No other wallpaper to choose (only current exists)." >&2
exit 1
else
echo "${FILES[0]}"
return
fi
fi
# find index of current
idx=-1
for i in "${!FILES[@]}"; do
if [ "$(readlink -f "${FILES[i]}")" = "$CUR" ]; then
idx="$i"
break
fi
done
if [ "$idx" -eq -1 ]; then
# current not in list: choose first
echo "${FILES[0]}"
return
fi
# next index with wrap
next=$(( (idx + 1) % n ))
# if next equals current (possible if only one), error handled above
echo "${FILES[$next]}"
}
if [ "$RANDOM_MODE" -eq 1 ]; then
NEW="$(choose_random)"
else
NEW="$(choose_next)"
fi
# Ensure absolute path
NEW="$(readlink -f "$NEW")"
# Create symlink (atomic-ish): create temporary symlink and move
TMP_SYMLINK="$(mktemp -u "${SYMLINK}.tmp.XXXX")"
ln -sfn -- "$NEW" "$TMP_SYMLINK"
mv -f "$TMP_SYMLINK" "$SYMLINK"
# Export for hyprctl command (and run it)
WALLPAPER="$NEW"
swww img "$WALLPAPER" --transition-type fade
hyprpanel -q ; hyprpanel & disown
wal -c
wal -n -i "$WALLPAPER"
exit 0
+4 -4
View File
@@ -4,11 +4,7 @@
home.packages = with pkgs; [
(writeShellScriptBin "select_command"
(builtins.readFile ./select_command.sh))
(writeShellScriptBin "move_wallpaper"
(builtins.readFile ./move_wallpaper.sh))
(writeShellScriptBin "move_avatar" (builtins.readFile ./move_avatar.sh))
(writeShellScriptBin "next_wallpaper"
(builtins.readFile ./next_wallpaper.sh))
(writeShellScriptBin "update_git_config"
(builtins.readFile ./update_system_from_git.sh))
zsh-powerlevel10k
@@ -19,6 +15,7 @@
bat
neofetch
yazi
zenity
];
home.file.".p10k.zsh".source = ./p10k.zsh;
@@ -101,6 +98,9 @@
mv_w = "move_wallpaper";
next_w = "next_wallpaper";
next_wr = "next_wallpaper -r";
next_wf = ''
next_wallpaper -f $(zenity --title "Pick a wallpaper" --file-selection --file-filter \*.png)'';
mv_avatar = "move_avatar";
t = "todo";