#!/usr/bin/env nu const image_exts = ["jpg" "jpeg" "png" "webp"] def wallpaper-dirs [] { [ ($"($env.HOME)/ownCloud/Personal/Wallpaper") ($"($env.HOME)/ownCloud/Personal/Wallpaper_32x9") ] | where {|dir| $dir | path exists } } def list-wallpapers [] { wallpaper-dirs | each {|dir| ls $dir } | flatten | where type == file | where {|file| (($file.name | path parse | get extension | str lowercase) in $image_exts) } | sort-by name } const thumb_size = "480x640" def thumbnail-dir [] { let dir = $"($env.HOME)/.cache/quickshell-wallpaper-thumbs" mkdir $dir $dir } def thumbnail-path [src: string] { let hash = ($src | hash sha256 | str substring 0..24) $"(thumbnail-dir)/($hash).jpg" } # Pre-rendered small JPEG cached in ~/.cache, so the picker never has to # decode a multi-megapixel wallpaper just to show a thumbnail. def ensure-thumbnail [src: string] { let thumb = (thumbnail-path $src) if not ($thumb | path exists) { try { magick $src -auto-orient -thumbnail $"($thumb_size)^" -gravity center -extent $thumb_size -quality 85 $thumb } catch {} } $thumb } def list-wallpapers-with-thumbs [] { list-wallpapers | get name | par-each {|f| { path: $f, thumb: (ensure-thumbnail $f) } } } def set-wallpaper [path: string] { if not ($path | path exists) { try { notify-send "Wallpaper not found" $path } exit 1 } try { awww img $path --transition-type grow --transition-duration 1 } $path | save -f $"($env.HOME)/.config/current-wallpaper" # An unhandled external-command failure aborts the rest of a nu script, so # this must not depend on awww/notify-send succeeding above. try { bash -lc $"nu ~/.dotfiles/config/nushell/scripts/wallpaper-theme.nu \"($path)\"" } catch {|e| try { notify-send "Theme update failed" $e.msg } } try { notify-send "Wallpaper applied" ($path | path basename) } } def random-wallpaper [] { let files = (list-wallpapers) if ($files | is-empty) { notify-send "No wallpapers found" "~/ownCloud/Personal/Wallpaper or ~/ownCloud/Personal/Wallpaper_32x9" exit 1 } let selected = ($files | shuffle | get name | first) set-wallpaper $selected } def current-wallpaper [] { let file = $"($env.HOME)/.config/current-wallpaper" if ($file | path exists) { open $file | str trim } else { "" } } def main [ path?: string --random --set: string --current --list --list-with-current ] { if $list_with_current { { wallpapers: (list-wallpapers-with-thumbs), current: (current-wallpaper) } | to json --raw } else if $list { list-wallpapers | get name | to json --raw } else if $current { current-wallpaper } else if $random { random-wallpaper } else if $set != null { set-wallpaper $set } else if $path != null { set-wallpaper $path } else { notify-send "Wallpaper picker" "Use the mod+W wallpaper carousel instead" } }