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

zathura-pywal (4311B)


      1 #!/bin/sh
      2 #
      3 # zathura-pywal
      4 
      5 NEWLINE='
      6 '
      7 ZATHURA_DIR="${XDG_CONFIG_HOME:-${HOME}/.config/}/zathura/"
      8 ZATHURARC="${ZATHURA_DIR}/zathurarc"
      9 SETTINGS="/usr/share/zathura-pywal/settings"
     10 COLORS="${XDG_CACHE_HOME:-${HOME}/.cache/}/wal/colors.sh"
     11 
     12 usage() {
     13     printf '%s\n' "Usage: $(basename "${0}") [options]" 1>&2;
     14     printf 'Options:\n'
     15     printf '%s%-6sAlpha transparency of the background (default: 1)\n' '-a' ''
     16     printf '%s%-6sDisplay this prompt\n' '-h' ''
     17 }
     18 
     19 zathura_pywal() {
     20     printf 'Checking for zathurarc...\n'
     21     if [ -f "${ZATHURARC}" ]; then
     22         printf 'Backing up zathurarc...\n'
     23         [ ! -f "${SETTINGS}" ] && printf 'Settings not found.\n' && return 1
     24         settings="$(grep -f "${SETTINGS}" -v "${ZATHURARC}" | sed '/^$/d' )"
     25         cp "${ZATHURARC}" "${ZATHURARC}.bak"
     26     else
     27         printf 'Creating zathurarc...\n'
     28         mkdir -p "${ZATHURARC%/*}"
     29         touch "${ZATHURARC}"
     30     fi
     31 
     32     printf 'Checking for pywal color scheme...\n'
     33     if [ -f "${COLORS}" ]; then . "${COLORS}"
     34     else
     35         printf 'Pywal color scheme not found.\nPlease make sure you run pywal in order to generate a color scheme.\n'
     36         return 1
     37     fi
     38 
     39     cat /dev/null > "${ZATHURARC}"
     40 
     41     if [ -n "${settings}" ]; then
     42         printf 'Copying old settings into zathurarc...\n'
     43         printf '%s\n\n' "### Settings from original zathurarc ###${NEWLINE}${settings}" >> "${ZATHURARC}"
     44     fi
     45 
     46     printf 'Changing zathura color scheme...\n'
     47 
     48     hex="$(printf '%s' "${color0}" | tr '[:lower:]' '[:upper:]' | tr -d '[:punct:]')"
     49 
     50     a=$(printf '%s' "${hex}" | cut -c 1-2)
     51     b=$(printf '%s' "${hex}" | cut -c 3-4)
     52     c=$(printf '%s' "${hex}" | cut -c 5-6)
     53 
     54     r=$(printf '%d' 0x${a})
     55     g=$(printf '%d' 0x${b})
     56     b=$(printf '%d' 0x${c})
     57 
     58     [ -z "${alpha}" ] && alpha=1
     59 
     60     [ "${alpha%"${alpha#?}"}" = '.' ] && alpha="0.${alpha#?}"
     61 
     62     [ ${#alpha} -ge 4 ] && alpha="${alpha%${alpha#????}}"
     63 
     64     printf '%s' "### zathura-pywal ###
     65 # Allow recolor
     66 set recolor 'true'
     67 
     68 # Don't allow original hue when recoloring
     69 set recolor-keephue 'false'
     70 
     71 # Don't keep original image colors while recoloring
     72 set recolor-reverse-video 'false'
     73 
     74 # Command line completion entries
     75 set completion-fg '${color7}'
     76 set completion-bg '${color0}'
     77 
     78 # Command line completion group elements
     79 set completion-group-fg '${color2}'
     80 set completion-group-bg '${color0}'
     81 
     82 # Current command line completion element
     83 set completion-highlight-fg '${color0}'
     84 set completion-highlight-bg '${color7}'
     85 
     86 # Default foreground/background color
     87 set default-bg rgba(${r},${g},${b},${alpha})
     88 
     89 # Input bar
     90 set inputbar-fg '${color7}'
     91 set inputbar-bg '${color0}'
     92 
     93 # Notification
     94 set notification-fg '${color7}'
     95 set notification-bg '${color0}'
     96 
     97 # Error notification
     98 set notification-error-fg '${color7}'
     99 set notification-error-bg '${color1}'
    100 
    101 # Warning notification
    102 set notification-warning-fg '${color7}'
    103 set notification-warning-bg '${color1}'
    104 
    105 # Tab - TODO
    106 # set tabbar-fg
    107 # set tabbar-bg
    108 
    109 # Focused tab - TODO
    110 # set tabbar-focus-fg
    111 # set tabbar-focus-bg
    112 
    113 # Status bar
    114 set statusbar-fg '${color7}'
    115 set statusbar-bg '${color0}'
    116 
    117 # Highlighting parts of the document (e.g. show search results)
    118 set highlight-color '${color2}'
    119 set highlight-active-color '${color2}'
    120 
    121 # Represent light/dark colors in recoloring mode
    122 set recolor-lightcolor rgba(0,0,0,0)
    123 set recolor-darkcolor '${color7}'
    124 
    125 # 'Loading...' text
    126 set render-loading-fg '${color7}'
    127 set render-loading-bg '${color0}'
    128 
    129 # Index mode
    130 set index-fg '${color7}'
    131 set index-bg '${color0}'
    132 
    133 # Selected element in index mode
    134 set index-active-fg '${color0}'
    135 set index-active-bg '${color7}'" >> "${ZATHURARC}"
    136 
    137     printf "All done.\nPress 'Ctrl' + 'R' (or whatever 'recolor' is mapped to) with zathura open to toggle the color scheme.\n"
    138 }
    139 
    140 main() {
    141     [ ${#} -eq 0 ] && zathura_pywal
    142 
    143     while getopts "a:h" o; do
    144         case "${o}" in
    145             a)
    146                 alpha="${OPTARG}"
    147                 if ! printf '%s' "${alpha}" | grep -q '^0*\.[0-9][0-9]*$\|^[0-1]$'; then
    148                     printf 'Alpha must be a decimal value between 0 and 1.\n'
    149                     usage
    150                     return 1
    151                 fi
    152                 zathura_pywal "${alpha}"
    153                 ;;
    154             h|*)
    155                 usage
    156                 ;;
    157         esac
    158     done
    159 }
    160 
    161 main "${@}"