From 6ab43c64aa34c6a13705435f7825d183a59544d8 Mon Sep 17 00:00:00 2001 From: Tom Willemse Date: Mon, 30 May 2022 23:02:55 -0700 Subject: Replace bash script with scsh script I like scsh better than bash. --- pick-random-wallpaper | 50 +++++++++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/pick-random-wallpaper b/pick-random-wallpaper index 7a2b28c..d90af5e 100755 --- a/pick-random-wallpaper +++ b/pick-random-wallpaper @@ -1,22 +1,34 @@ -#!/usr/bin/env bash +#!/usr/bin/env sh +# # -*- mode: scheme; -*- +IFS=" " +exec scsh -s "$0" "$@" +!# -function hc () { herbstclient "$@"; } +(define (random-wallpaper) + (let* ((root-info (let ((port (run/port (xwininfo -root)))) + (reverse (awk (read-line port) (line) ((result '())) + ((seq (or "Width" "Height") ": " + (submatch (+ digit))) => + (lambda (m) + (cons (string->number (match:substring m 1)) + result))))))) + (width (car root-info)) + (height (cadr root-info))) + (string-trim-right + (run/string (pipe (find ,(format #f "~a/pictures/wallpaper" (getenv "HOME")) + -type f + "(" -wholename ,(format #f "*/~ax~a/*" width height) + -o -wholename "*/patterns/*" ")" + -not -name "*.org") + (shuf -n 1)))))) -function random-wallpaper() { - local rootinfo=$(xwininfo -root) - local width=$(echo $rootinfo | grep 'Width:' | sed 's/.*Width: \([[:digit:]]\+\).*/\1/') - local height=$(echo $rootinfo | grep 'Height:' | sed 's/.*Height: \([[:digit:]]\+\).*/\1/') +(define (attribute-exists? name) + (= 0 (run (herbstclient attr ,name) + (> 1 /dev/null) + (> 2 1)))) - find ~/pictures/wallpaper -type f \ - \( -wholename "*/${width}x${height}/*" -o -wholename '*/patterns/*' \) \ - -not -name '*.org' \ - | shuf -n 1 -} - -wallpaper=$(random-wallpaper) - -if ! hc attr my_wallpaper >/dev/null 2>&1 -then hc new_attr string my_wallpaper -fi - -feh --bg-tile "$wallpaper" && hc set_attr my_wallpaper "$wallpaper" +(let ((wallpaper (random-wallpaper))) + (if (not (attribute-exists? "my_wallpaper")) + (run (herbstclient new_attr string my_wallpaper))) + (&& (feh --bg-tile ,wallpaper) + (herbstclient set_attr my_wallpaper ,wallpaper))) -- cgit v1.2.3-54-g00ecf