Add styler theming framework

Sets up basic theming program styler which can be called to change
theming of various applications.
This commit is contained in:
Marty Oehme 2020-01-30 17:08:07 +01:00
parent b56c33834f
commit 2ee8453750

115
styler/.local/bin/styler Executable file
View file

@ -0,0 +1,115 @@
#!/usr/bin/env bash
readonly BASE_PATH="${STYLER_DATA_PATH:-${XDG_DATA_HOME:-$HOME/.local/share}/styler}"
readonly DEBUG="${STYLER_ENABLE_DEBUG_MODE:-false}"
readonly PACKAGE_PATH="$BASE_PATH/packages"
readonly PROCESSOR_PATH="$BASE_PATH/processors"
main() {
local cmd=""
local ret=0
case "$1" in
-s | --set | set)
cmd="set_theme"
;;
-v | --version | version)
printf "Program theming script.\n\n©Marty Oehme\n\nVersion: 0.1.0\n"
exit 0
;;
-h | --help | help | *)
cmd="usage"
;;
esac
shift
$cmd "$1"
ret=$((ret + $?))
exit $ret
}
usage() {
printf "%s\n" \
"" \
" styler - Quickly switch your linux style." \
" Uses base16 themes to quickly set them for a variety of applications." \
"" \
" Usage: styler [-hv | set base16-themename]" \
"" \
" Options:" \
"" \
" -s | set Set the theme. Use any valid base16 theme name (without base16- prefix)." \
"" \
" -h | help Print out this help." \
"" \
" -v | version Print out program information." \
"" \
""
}
# retrieves all relevant packages from BASE_PATH/packages
# 'relevant' here means they follow github pattern of author/repository
get_packages() {
for author in "$PACKAGE_PATH"/*; do
# TODO should eventually be used to either distinguish between author/pkg and pkg packages
# or to spit out a warning if they should not be used.
# if grep -q -e '^base16-' <<<"$(basename -- "$author")"; then
# echo ERROR
# fi
for package in "$author"/*; do
[[ -e "$author" ]] || break
[[ -d "$package" ]] || break
printf "%s/%s\n" "$(basename -- "$author")" "$(basename -- "$package")"
done
done
}
# retrieves all processors from BASE_PATH/processors
# 'relevant' here means they follow github pattern of author/repository
get_processors() {
for processor in "$PROCESSOR_PATH"/*; do
[[ -e "$processor" ]] || break
[[ -f "$processor" ]] || break
printf "%s\n" "$(basename -- "$processor")"
done
}
set_theme() {
local theme="$1"
local packages
packages="$(get_packages)"
if [[ -z "$packages" ]]; then
printf "ERROR: No base16 packages installed. Please install at least 1 base16 package in %s/.\n" "$PACKAGE_PATH" >&2
exit 1
fi
local processors
processors="$(get_processors)"
if [[ -z "$processors" ]]; then
printf "ERROR: No application processors installed. Please install at least one processor in %s/.\n" "$PROCESSOR_PATH" >&2
exit 1
fi
for pkg in $packages; do
local appext
# filter the application a package targets, since base16 packages
# carry standard names this removes everything before base16-
# the result is the application it targets
# shellcheck disable=SC2001
appext=$(sed "s|^[[:alnum:]]\{1,\}/base16-||" <<<"$pkg")
# Compares application extension with existing processors and runs the appropriate processor if found
processor="$PROCESSOR_PATH/theme_$appext"
if [[ -f "$processor" ]]; then
"$processor" "$PACKAGE_PATH" "$pkg" "$theme"
else
printf "WARN: No processor found for application %s in %s. Make sure you install a processor for the application.\n" "$appext" "$PROCESSOR_PATH/" >&2
fi
done
}
main "$@"