dotfiles

Mahdi's dotfiles
git clone git://mahdi.pw/dotfiles.git
Log | Files | Refs | Submodules | README | LICENSE

bg-set (2030B)


      1 #!/bin/sh
      2 # OLD: I use bg-gen instead.
      3 #
      4 bg_set() { xwallpaper --zoom "$1"; }
      5 
      6 THEME="$(cat "$HOME"/.cache/theme || echo "nord")"
      7 WLPS="$HOME/.local/share/wallpapers/$THEME"
      8 
      9 # Image Preview Configuration
     10 WID="$(xdotool getwindowfocus)"
     11 ID="setbg-preview-ueberzug-${WID}"
     12 IMAGE="/tmp/setbg-image-${WID}.png"
     13 FIFO="/tmp/setbg-preview-ueberzug-fifo-${WID}"
     14 WIDTH=$FZF_PREVIEW_COLUMNS
     15 HEIGHT=$FZF_PREVIEW_LINES
     16 VPAD=$(fzf --version | {
     17     IFS='. ' read -r v1 v2 _
     18     [ "$v1" = 0 ] && [ "$v2" -le 26 ] && echo 0 || echo 4
     19 })
     20 
     21 # Image Preview
     22 start_ueberzug() {
     23     rm -f "$FIFO" "$IMAGE" > /dev/null 2>&1
     24     mkfifo "$FIFO" || exit 1
     25     ueberzug layer -p json -s < "$FIFO" &
     26     exec 3> "$FIFO"
     27 }
     28 stop_ueberzug() {
     29     exec 3>&-
     30     printf '{ "action": "remove", "identifier": "%s" }\n' "$ID" > "$FIFO"
     31     rm -f "$FIFO" "$IMAGE" > /dev/null 2>&1
     32 }
     33 clean_ueberzug() {
     34     printf '{ "action": "remove", "identifier": "%s" }\n' "$ID" > "$FIFO"
     35 }
     36 image_preview() {
     37     printf '{ "action": "add", "identifier": "%s", "path": "%s", "x": %d, "y": %d, "scaler": "fit_contain", "width": %d, "height": %d }\n' \
     38         "$ID" "$IMG" "$((WIDTH + VPAD))" 4 "$((WIDTH - VPAD))" "$((HEIGHT))" > "$FIFO" \
     39         || printf '{ "action": "remove", "identifier": "%s" }\n' "$ID" > "$FIFO"
     40     exit 1
     41 }
     42 
     43 if [ "$SET_BG_SELECT" ]; then
     44     cd "$WLPS" || exit
     45     if [ "$#" = 0 ]; then
     46         trap stop_ueberzug EXIT QUIT INT TERM
     47         start_ueberzug
     48         OUTPUT=$(find . -type f -printf '%P\n' | sort \
     49         	| fzf -i -e --layout=reverse --border=sharp \
     50         	--info=inline --preview "sh $0 {}" \
     51             --preview-window "right:50%:noborder:wrap" \
     52             --bind "ยต:toggle-preview+toggle-preview")
     53         if [ "$OUTPUT" ]; then
     54             stop_ueberzug
     55     	bg_set "$WLPS/$OUTPUT"
     56         fi
     57     elif [ "$#" = 1 ]; then
     58         clean_ueberzug
     59 	IMG="/tmp/setbg-thumb-$1"
     60 	convert -thumbnail 300x "$1" "$IMG"
     61         image_preview "$IMG"
     62 	rm "$IMG"
     63     fi
     64 else
     65     WLPS="${1:-$WLPS}"
     66     [ "${WLPS}" ] && bg_set "${WLPS}"
     67 fi