zathura-pywal

A better version of zathura-pywal that saves settings and features alpha transparency
git clone git://mattcarlson.org/repos/zathura-pywal.git
Log | Files | Refs | README

commit 98309ddbfb5967a5accc47cd0e6a67254b18d995
parent d0c962e6c844d9ed51131154d6bebb377c93902e
Author: Matthew Carlson <matt@mcarlson.xyz>
Date:   Sun, 25 Jul 2021 00:31:53 -0400

shellcheck, now POSIX-compliant

Diffstat:
AMakefile | 26++++++++++++++++++++++++++
MREADME.md | 12++++++++++--
Dinstall.sh | 12------------
Mzathura-pywal | 134+++++++++++++++++++++++++++++++++++++++++++++++--------------------------------
4 files changed, 116 insertions(+), 68 deletions(-)

diff --git a/Makefile b/Makefile @@ -0,0 +1,26 @@ +PREFIX ?= /usr + +all: + @echo Run \'make install\' as root to install zathura-pywal. + +install: + @mkdir -p $(DESTDIR)$(PREFIX)/bin/ + @mkdir -p $(DESTDIR)$(PREFIX)/share/ + @echo Creating $(DESTDIR)$(PREFIX)/share/zathura-pywal/... + @mkdir -p $(DESTDIR)$(PREFIX)/share/zathura-pywal/ + @echo Copying zathura-pywal to $(DESTDIR)$(PREFIX)/bin/... + @cp -p zathura-pywal $(DESTDIR)$(PREFIX)/bin/zathura-pywal + @echo Copying settings to $(DESTDIR)$(PREFIX)/share/zathura-pywal/settings... + @cp -p settings $(DESTDIR)$(PREFIX)/share/zathura-pywal/settings + @echo Running chmod 755 on $(DESTDIR)$(PREFIX)/bin/zathura-pywal... + @chmod 755 $(DESTDIR)$(PREFIX)/bin/zathura-pywal + @echo Installation successful. + +uninstall: + @ echo Removing /usr/bin/zathura-pywal... + @rm -f $(PREFIX)/bin/zathura-pywal + @ echo Removing /usr/share/zathura-pywal/settings... + @rm -f $(PREFIX)/share/zathura-pywal/settings + @ echo Removing /usr/share/zathura-pywal/... + @rm -rf $(PREFIX)/share/zathura-pywal/ + @echo zathura-pywal uninstalled. diff --git a/README.md b/README.md @@ -3,13 +3,21 @@ A better version of zathura-pywal that saves settings and features alpha transpa ![zathura-pywal](https://mcarlson.xyz/img/zathura_pywal.png) -Saves old `zathurarc` as `zathurarc.bak` and themes your zathura using pywal theme found in `.cache/wal/colors.sh`, placing new settings plus old settings into a new `zathurarc`. Pass in an optional alpha value between zero and one using the `-a` flag if you want a transparent background. +Saves old `zathurarc` as `zathurarc.bak` and themes your zathura using the pywal colors found in `~/.cache/wal/colors.sh`, placing new settings plus old settings into a new `zathurarc`. Pass in an optional alpha value between zero and one using the `-a` flag if you want a transparent background. + +This script is POSIX-compliant, meaning that it will work on any shell that supports the POSIX standards. Tested and working on bash, dash, mksh, and zsh. # Installation ```shell $ git clone https://github.com/mslcarlson/zathura-pywal/ $ cd zathura-pywal -$ sudo ./install.sh +$ sudo make install +``` + +# Uninstall +```shell +$ cd zathura-pywal +$ sudo make uninstall ``` # Usage diff --git a/install.sh b/install.sh @@ -1,12 +0,0 @@ -#!/bin/sh - -[ "$EUID" -ne 0 ] && echo "Permission denied. Are you root?" && exit - -DIR='/usr/share/zathura-pywal' -PATH='/usr/bin/' - -mkdir --parents "${DIR}" -echo "./settings -> ${DIR}/settings" && cp './settings' "${DIR}/settings" -echo "./zathura-pywal -> ${PATH}/zathura-pywal" && cp './zathura-pywal' "${PATH}/zathura-pywal" - -[ $? -eq 0 ] && echo "Installation complete." diff --git a/zathura-pywal b/zathura-pywal @@ -1,50 +1,75 @@ #!/bin/sh # -# A better version of zathura-pywal that saves settings and features alpha transparency +# zathura-pywal -zathurarc="/home/$(whoami)/.config/zathura/zathurarc" -grepdir="/usr/share/zathura-pywal/settings" -colors="/home/$(whoami)/.cache/wal/colors.sh" +NEWLINE=' +' +ZATHURA_DIR="${XDG_CONFIG_HOME:-${HOME}/.config/}/zathura/" +ZATHURARC="${ZATHURA_DIR}/zathurarc" +GREPDIR="/usr/share/zathura-pywal/settings" +COLORS="${XDG_CACHE_HOME:-${HOME}/.cache/}/wal/colors.sh" usage() { - printf "Usage: $(basename $0) [options]\n" 1>&2; - printf 'Options:\n' - printf -- '-a%-6sAlpha transparency of the background (default: 1)\n' - printf -- '-h%-6sDisplay this prompt\n' - exit 1; + printf '%s\n' "Usage: $(basename "${0}") [options]" 1>&2; + printf 'Options:\n' + printf '%s%-6sAlpha transparency of the background (default: 1)\n' '-a' '' + printf '%s%-6sDisplay this prompt\n' '-h' '' } zathura_pywal() { - printf 'Checking for zathurarc...\n' - sleep 0.2s - [ -f "${zathurarc}" ] && { printf "Backing up zathurarc...\n"; settings="$(grep -v -f ${grepdir} ${zathurarc} | sed '/^$/d' )"; cp "${zathurarc}" "${zathurarc}.bak" ; } \ - || { printf "Creating zathurarc...\n"; touch "${zathurarc}" ; } - sleep 0.2s - printf 'Checking for pywal color scheme...\n' - sleep 0.2s - { [ -f "${colors}" ] && . "${colors}" ; } || { printf 'Pywal color scheme not found.\nPlease make sure you run pywal in order to generate a colorscheme.\n' && exit 1; } - cat /dev/null > "${zathurarc}" - [ -n "${settings}" ] && { printf 'Copying old settings into zathurarc...\n' && sleep 0.2s && echo "### Settings from original zathurarc ### -${settings} -" >> "${zathurarc}" ; } - printf 'Changing zathura color scheme...\n' - sleep 0.2s - hex="${color0:1}" - r="$(printf "%d" 0x${hex:0:2})" - g="$(printf "%d" 0x${hex:2:2})" - b="$(printf "%d" 0x${hex:4:2})" - [ -z "${alpha}" ] && alpha=1 - [ "${alpha::1}" == "." ] && alpha="${alpha/./0.}" - alpha="${alpha:0:4}" - echo "### General recoloring settings ### -# Allow recoloring + printf 'Checking for zathurarc...\n' + if [ -f "${ZATHURARC}" ]; then + printf 'Backing up zathurarc...\n' + settings="$(grep -v -f "${GREPDIR}" "${ZATHURARC}" | sed '/^$/d' )" + cp "${ZATHURARC}" "${ZATHURARC}.bak" + else + printf 'Creating zathurarc...\n' + mkdir -p "${ZATHURARC%/*}" + touch "${ZATHURARC}" + fi + + printf 'Checking for pywal color scheme...\n' + if [ -f "${COLORS}" ]; then . "${COLORS}" + else + printf 'Pywal color scheme not found.\nPlease make sure you run pywal in order to generate a color scheme.\n' + return 1 + fi + + cat /dev/null > "${ZATHURARC}" + + if [ -n "${settings}" ]; then + printf 'Copying old settings into zathurarc...\n' + printf '%s\n\n' "### Settings from original zathurarc ###${NEWLINE}${settings}" >> "${ZATHURARC}" + fi + + printf 'Changing zathura color scheme...\n' + + hex="$(printf '%s' "${color0}" | tr '[:lower:]' '[:upper:]' | tr -d '[:punct:]')" + + a=$(printf '%s' "${hex}" | cut -c 1-2) + b=$(printf '%s' "${hex}" | cut -c 3-4) + c=$(printf '%s' "${hex}" | cut -c 5-6) + + r=$(printf '%s\n' "ibase=16; ${a}" | bc) + g=$(printf '%s\n' "ibase=16; ${b}" | bc) + b=$(printf '%s\n' "ibase=16; ${c}" | bc) + + [ -z "${alpha}" ] && alpha=1 + + [ "${alpha%"${alpha#?}"}" = '.' ] && alpha="0.${alpha#?}" + + [ ${#alpha} -ge 4 ] && alpha="${alpha%${alpha#????}}" + + printf '%s' "### zathura-pywal ### +# Allow recolor set recolor \"true\" + # Don't allow original hue when recoloring set recolor-keephue \"false\" + # Don't keep original image colors while recoloring set recolor-reverse-video \"false\" -### Theming ### # Command line completion entries set completion-fg \"${color7}\" set completion-bg \"${color0}\" @@ -58,7 +83,6 @@ set completion-highlight-fg \"${color0}\" set completion-highlight-bg \"${color7}\" # Default foreground/background color -# set default-fg # TODO set default-bg rgba(${r},${g},${b},${alpha}) # Input bar @@ -107,28 +131,30 @@ set index-bg \"${color0}\" # Selected element in index mode set index-active-fg \"${color0}\" -set index-active-bg \"${color7}\"" >> "${zathurarc}" - printf "All done.\nPress \'Ctrl\' + \'R\' (or whatever \'recolor\' is mapped to) with zathura open to toggle the color scheme.\n" +set index-active-bg \"${color7}\"" >> "${ZATHURARC}" + + printf "All done.\nPress 'Ctrl' + 'R' (or whatever 'recolor' is mapped to) with zathura open to toggle the color scheme.\n" } main() { - [ $# -eq 0 ] && zathura_pywal - - while getopts "a:h" o; do - case "${o}" in - a) - alpha="${OPTARG}" - if ! echo "${alpha}" | grep -q -P '^0*\.+[0-9]+$|^[0-1]$'; then - printf 'Alpha must be a decimal value between 0 and 1.\n' - usage - fi - zathura_pywal ${alpha} - ;; - h|*) - usage - ;; - esac - done + [ ${#} -eq 0 ] && zathura_pywal + + while getopts "a:h" o; do + case "${o}" in + a) + alpha="${OPTARG}" + if ! printf '%s' "${alpha}" | grep -q -P '^0*\.+[0-9]+$|^[0-1]$'; then + printf 'Alpha must be a decimal value between 0 and 1.\n' + usage + return 1 + fi + zathura_pywal "${alpha}" + ;; + h|*) + usage + ;; + esac + done } -main "$@" +main "${@}"