Update zathura to use drop-in colorscheme file

Zathura can use include directive to gather configuration from multiple
files, make use of that for less intrusive theming changes. Now only
needs to append include directive in main configuration file and the
colorscheme can just be put into a completely separate file.
This commit is contained in:
Marty Oehme 2022-03-10 21:01:35 +01:00
parent b780cefea8
commit 6e91749030
Signed by: Marty
GPG Key ID: B7538B8F50A1C800
1 changed files with 30 additions and 53 deletions

View File

@ -2,14 +2,11 @@
readonly dependency=("nicodebo/base16-zathura") readonly dependency=("nicodebo/base16-zathura")
readonly app="zathura" readonly app="zathura"
# #
# zathura does not support including other files into # zathura now allows including other files into its
# its configuration yet. As such, this processor needs to # main configuration file. That means we can be less
# change the user's configuration file *itself*. # intrusive about the colorscheme changes and only insert
# # the include line, adding any colorscheme options
# This will generally not be a problem, though it can lead # in a completely different file.
# to corrupted configurations in exceptional circumstances
# (such as every line being preceded by the process control
# regex, for whatever reason).
# #
# In general, the processor looks for a specific line in the # In general, the processor looks for a specific line in the
# following format: # following format:
@ -24,28 +21,30 @@ package="$2"
theme="$3" theme="$3"
permanent="$4" permanent="$4"
zathuraconf="$HOME/.config/zathura/zathurarc" zathura_conf_dir="$HOME/.config/zathura"
zathuraconf="$zathura_conf_dir/zathurarc"
colorscheme_filename="colorscheme"
## Main Entrypoint ## Main Entrypoint
# #
# Finds the right theme template file and starts theme # Finds the right theme template file and starts theme
# switching and, if necessary, permanent setting processes. # switching and, if necessary, permanent setting processes.
main() { main() {
dbg_msg $app "Starting Processor" dbg_msg $app "Starting Processor"
tfile="$path/$package/build_schemes/base16-$theme.config" tfile="$path/$package/build_schemes/base16-$theme.config"
if ! file_exists "$tfile"; then if ! file_exists "$tfile"; then
dbg_msg $app "error" "Theme template $theme not found in package $package" dbg_msg $app "error" "Theme template $theme not found in package $package"
exit 1 exit 1
fi fi
if [ ! -f "$zathuraconf" ]; then if [ ! -f "$zathuraconf" ]; then
dbg_msg $app "error" "Zathura config file not found. Please make sure file exists: $zathuraconf" dbg_msg $app "error" "Zathura config file not found. Please make sure file exists: $zathuraconf"
fi fi
if [[ "$permanent" == "true" ]]; then save; fi if [[ "$permanent" == "true" ]]; then save; fi
dbg_msg $app "Processor Done" dbg_msg $app "Processor Done"
} }
## Theme setter ## Theme setter
@ -53,40 +52,18 @@ main() {
# Takes care of permanently writing the desired # Takes care of permanently writing the desired
# base16 theme into application settings. # base16 theme into application settings.
save() { save() {
dbg_msg $app "Saving theme" dbg_msg $app "Saving theme"
# following format: if ! $(grep -qe "^include $colorscheme_filename" "$zathuraconf"); then
# '# Base16 [theme name] - zathura color config' dbg_msg $app "No include directive for colorscheme file found in configuration file. Adding it now."
# from which it will delete everything until it finds a line: printf "\ninclude %s\n" "$colorscheme_filename" >>"$zathuraconf"
# '# Base16End [theme name] - zathura color config' else
startline="$(grep -ne '^# Base16 .*$' "$zathuraconf" | cut -f1 -d:)" dbg_msg $app "Found include directive for colorscheme file. No operation necessary."
endline="$(grep -ne '^# Base16End .*- zathura color config$' "$zathuraconf" | cut -f1 -d:)" fi
tmpfile="$zathuraconf.tmp" cat "$tfile" >"${zathura_conf_dir}/${colorscheme_filename}" || exit 1
tmptheme="$zathuraconf.thm.tmp"
if [[ -n "$startline" && -z "$endline" ]] || [[ -z "$startline" && -n "$endline" ]] || [[ "$startline" -gt "$endline" ]]; then dbg_msg $app "Saved theme to ${zathura_conf_dir}/${colorscheme_filename}"
dbg_msg $app "error" "Base 16 Pattern not correctly recognized in file: $zathuraconf. Please make sure pattern begins with # Base 16 and ends with # Base 16end and only exists once in the file. No changes to file made."
elif [[ -z "$startline" && -z "$endline" ]]; then
cat "$zathuraconf" >"$tmpfile" || exit 1
else
# remove old lines of any base16 theme
sed -e "$startline,$endline d" "$zathuraconf" >"$tmpfile" || exit 1
fi
# fix template theme name key for easier inclusion
sed -e "s/^colors:/$theme: \&colorscheme/" "$tfile" >"$tmptheme" || exit 1
echo "# Base16End $theme - zathura color config" >>"$tmptheme"
# combine both into final config file
# replace original file with new colorscheme-added version
cat "$tmptheme" "$tmpfile" >"$zathuraconf"
rm "$tmptheme" "$tmpfile"
dbg_msg $app "Saved theme to $zathuraconf"
} }
# Safe sourcing: https://stackoverflow.com/a/12694189 # Safe sourcing: https://stackoverflow.com/a/12694189
@ -99,7 +76,7 @@ if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
# Makes sure the processor is called for the correct # Makes sure the processor is called for the correct
# base16 package, or refuses to run if it is not. # base16 package, or refuses to run if it is not.
if printf '%s\n' "${dependency[@]}" | grep -q -P "^$package$"; then if printf '%s\n' "${dependency[@]}" | grep -q -P "^$package$"; then
main main
else else
dbg_msg $app "error" "Processor does not work for package $package" dbg_msg $app "error" "Processor does not work for package $package"
fi fi