dotfiles/styler/.local/bin/styler

144 lines
3.7 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env bash
readonly BASE_PATH="${STYLER_DATA_PATH:-${XDG_DATA_HOME:-$HOME/.local/share}/styler}"
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"
;;
-l | --list | list)
cmd="list"
;;
-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
}
# retrieves all installed themes from all packages, appends applications they exist for
get_themes() {
local themes
themes=$(find "$PACKAGE_PATH" -type f -name 'base16-*')
echo "$themes" | sed "s/.*\\/base16-//" | sed "s/\\..*//" | sort | uniq
}
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
}
list() {
local selected="$1"
case "$selected" in
packages)
get_packages
;;
processors)
get_processors
;;
themes)
get_themes
;;
*)
echo "Please select one of packages | processors | themes to list."
;;
esac
}
main "$@"