wp.sh (3741B)
1 #!/bin/sh 2 # 3 # wp 4 5 ALPHA=0.95 6 # wp is sym link 7 WP="${XDG_DATA_HOME:-${HOME}/.local/share/}/wp" 8 # 16 colors generated by pywal 9 COLORS="${XDG_CACHE_HOME:-${HOME}/.cache/}/wal/colors.sh" 10 # term fg 11 DEFAULTFG=259 12 # xresources handles suckless vars 13 XRESOURCES="${XDG_CONFIG_HOME:-${HOME}/.config/}/x/xresources" 14 15 # get random valid file in dir 16 get_rand_file() { 17 # get all files in dir 18 files="$(for file in "${1}"/*; do 19 [ -d "${file}" ] && continue 20 printf '%s\n' "${file}" 21 done)" 22 23 # loop until valid file found 24 file='' 25 while ! check_file "${file}"; do 26 # get random file and remove it from list 27 file="$(printf '%s' "${files}" | awk 'BEGIN{ srand() } { printf "%f %s\n", rand(), $0 }' | sort | cut -d ' ' -f 2 | head -n 1)" 28 files="$(printf '%s' "${files}" | grep -iv "${file}")" 29 30 # break if no more files to check 31 [ -z "${files}" ] && break 32 done 33 34 # no files in dir are valid 35 if ! check_file "${file}"; then file=''; fi 36 37 if [ -z "${file}" ]; then return 1 38 else return 0 39 fi 40 } 41 42 check_file () { 43 # get file extension 44 file="${1}" 45 ext="${file##*.}" 46 47 case "${ext}" in 48 # formats supported by imlib2 used by feh, sxiv, etc. 49 bmp \ 50 | dib \ 51 | ff \ 52 | gif \ 53 | ico \ 54 | iff \ 55 | jfi \ 56 | jfif \ 57 | jif \ 58 | jpe \ 59 | jpeg \ 60 | jpg \ 61 | lbm \ 62 | png \ 63 | pnm \ 64 | tga \ 65 | tif \ 66 | tiff \ 67 | webp \ 68 | xpm) return ;; 69 *) return 1 ;; 70 esac 71 } 72 73 wp () { 74 # create wp sym link 75 ln -sf "${file}" "${WP}" 76 77 # change bg 78 feh --no-fehbg --bg-scale "${WP}" 79 80 # theme iff pywal installed 81 command -v wal >/dev/null && theme 82 } 83 84 theme() { 85 # remove cached themes 86 wal -c 87 88 # gen new theme with wpg (uses pywal) 89 wpg -a "${file}" && wpg -ns "${file}" 90 91 [ -f "${COLORS}" ] && . "${COLORS}" 92 93 set -- "${color0}" "${color1}" "${color2}" "${color3}" "${color4}" "${color5}" "${color6}" "${color7}" \ 94 "${color8}" "${color9}" "${color10}" "${color11}" "${color12}" "${color13}" "${color14}" "${color15}" 95 96 # replace colors in xresources 97 i=0 98 for color in "${@}"; do 99 color="$(printf '%s' "${color}" | tr '[:upper:]' '[:lower:]')" 100 printf '%s\n' "$(sed "s/#define col${i} .*/#define col${i} ${color}/" "${XRESOURCES}")" > "${XRESOURCES}" 101 i=$((i+1)) 102 [ ${i} -eq 16 ] && break 103 done 104 105 # reload xresources 106 xrdb "${XRESOURCES}" 107 108 # firefox; python-pywalfox and firefox plugin are required 109 # palette and theme templates have to be set up in firefox itself 110 # see https://github.com/frewacom/pywalfox 111 pywalfox dark 112 pywalfox update 113 114 # reload all instances of term 115 kill -s USR1 $(ps -ef | grep -i "\<${TERMINAL}\>" | grep -v '\<grep\>' | awk '{ printf "%s ", $2 }') 116 117 # hacky work-around to change terminal fg on the fly 118 # buggy with alpha 119 for tty in $(find /dev/pts/ -name '*' | grep '[1-9][0-9]*' | tr -d '[:alpha:][=/=]'); do 120 echo "\033]4;${DEFAULTFG};${color0}\007" > "/dev/pts/${tty}" 2>/dev/null 121 done 122 123 # reload dwm 124 dwmc reload 125 126 # zathura 127 zathura-pywal -a ${ALPHA} 128 } 129 130 main() { 131 # convert relative to abs if not already 132 case "${1}" in 133 /*) path="${1}" ;; 134 *) path="${PWD}/${1}" ;; 135 esac 136 137 # handle symlinks 138 path="$("${HOME}/.local/bin/symlink.sh" "${path}")" 139 140 # if dir get rand file 141 if [ -d "${path}" ]; then 142 get_rand_file "${path}" && wp "${file}" 143 # if file check it 144 elif [ -f "${path}" ]; then 145 check_file "${path}" && wp "${file}" 146 else return 1 147 fi 148 } 149 150 main "${@}"