|
| 1 | +#!/usr/bin/env nu |
| 2 | +# |
| 3 | +# ~/.fehbg.nu |
| 4 | +# |
| 5 | +# This script selects a random image from a directory and sets it as a |
| 6 | +# wallpaper. An additional feature is that it draws the path of the image in |
| 7 | +# the lower left corner which makes it easier to find the image later. |
| 8 | +# |
| 9 | +# The procedure is as follows:: |
| 10 | +# * Select a random image from a directory (recursively entering all |
| 11 | +# subdirectories) |
| 12 | +# * Print the path of the image as an overlay on top of the selected image |
| 13 | +# * Save the result in a temporary file |
| 14 | +# * Set the temporary image as a wallpaper. |
| 15 | +# |
| 16 | +# !! Requires `WALLPAPER_DIR` and `TMP_DIR` environment variables to be set !! |
| 17 | +# (or just hardcode your own paths below) |
| 18 | +# |
| 19 | +# You can set this script to run on desktop startup, e.g., by adding it to |
| 20 | +# xinitrc. |
| 21 | +# |
| 22 | +# Dependencies; |
| 23 | +# * nu version >0.25.1 (0.25.1 doesn't work due to coercion error bug) |
| 24 | +# * feh |
| 25 | +# * imagemagick |
| 26 | + |
| 27 | +# Path definitions |
| 28 | +let img_dir = $nu.env.WALLPAPER_DIR |
| 29 | +let tmp_image = $(build-string $nu.env.TMP_DIR "/wallpaper.jpg") |
| 30 | + |
| 31 | +# Monitor resolution |
| 32 | +let resolution_y = 1440 |
| 33 | + |
| 34 | +# Position of the caption |
| 35 | +let pos_x = 5 |
| 36 | +let pos_y = 0.995 * $resolution_y |
| 37 | + |
| 38 | +# Helper commands |
| 39 | +def select_random [] { shuffle | first } |
| 40 | + |
| 41 | +# List all images in a directory and all its subdirectories |
| 42 | +def list_images [dir] { |
| 43 | + ls $(build-string $dir /**/*) | where type == File | where name =~ jpg || name =~ jpeg || name =~ tif || name =~ tiff |
| 44 | +} |
| 45 | + |
| 46 | +# Set the caption text (just filename for now) |
| 47 | +def caption [img_f] { |
| 48 | + echo $img_f |
| 49 | +} |
| 50 | + |
| 51 | +# Build the argument for the '-draw' command of the 'convert' utility |
| 52 | +def draw_str [img_f] { |
| 53 | + build-string 'text ' $pos_x ',' $pos_y ' "' $(caption $img_f) '" ' |
| 54 | +} |
| 55 | + |
| 56 | +# Select random image |
| 57 | +let img_name = $(list_images $img_dir | select_random | get name) |
| 58 | + |
| 59 | +# Resize the image to the monitor height, draw the caption and save it |
| 60 | +let res_str = $(build-string 'x' $resolution_y) |
| 61 | +convert -resize $res_str -pointsize 15 -fill "rgb(255,200,150)" -draw $(draw_str $img_name) $img_name $tmp_image |
| 62 | + |
| 63 | +# Set the created image as a background |
| 64 | +feh --no-fehbg --bg-max $tmp_image |
0 commit comments