2020-01-30 16:08:07 +00:00
|
|
|
#!/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"
|
|
|
|
|
2020-01-30 20:58:31 +00:00
|
|
|
readonly VERSION="0.3.0"
|
2020-01-30 20:12:08 +00:00
|
|
|
|
2020-01-30 16:08:07 +00:00
|
|
|
main() {
|
|
|
|
local cmd=""
|
|
|
|
local ret=0
|
|
|
|
|
|
|
|
case "$1" in
|
2020-01-30 20:58:31 +00:00
|
|
|
-t | --theme | theme)
|
|
|
|
cmd="switch_theme"
|
|
|
|
;;
|
2020-01-30 16:08:07 +00:00
|
|
|
-s | --set | set)
|
|
|
|
cmd="set_theme"
|
|
|
|
;;
|
2020-01-30 18:49:37 +00:00
|
|
|
-l | --list | list)
|
|
|
|
cmd="list"
|
|
|
|
;;
|
2020-01-30 20:12:08 +00:00
|
|
|
-d | --download | download)
|
|
|
|
cmd="download"
|
|
|
|
;;
|
2020-01-30 16:08:07 +00:00
|
|
|
-v | --version | version)
|
2020-01-30 20:12:08 +00:00
|
|
|
printf "Program theming script.\n\n©Marty Oehme\n\nVersion: %s\n" "$VERSION"
|
2020-01-30 16:08:07 +00:00
|
|
|
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:" \
|
|
|
|
"" \
|
2020-01-30 20:58:31 +00:00
|
|
|
" -t | theme Temporarily switch theme. Use any valid base16 theme name (without base16- prefix)." \
|
|
|
|
" Theme will be lost upon restart, or application restarts." \
|
|
|
|
"" \
|
2020-01-30 16:08:07 +00:00
|
|
|
" -s | set Set the theme. Use any valid base16 theme name (without base16- prefix)." \
|
2020-01-30 20:58:31 +00:00
|
|
|
" Same as 'theme' option, but changes will be made permanent." \
|
2020-01-30 16:08:07 +00:00
|
|
|
"" \
|
2020-01-30 20:12:08 +00:00
|
|
|
" -d | download Download a base16 template into the package directory or download a processor16" \
|
|
|
|
" into the processor directory. Use user/repo format to automatically pull from github." \
|
|
|
|
"" \
|
2020-01-30 16:08:07 +00:00
|
|
|
" -h | help Print out this help." \
|
|
|
|
"" \
|
|
|
|
" -v | version Print out program information." \
|
|
|
|
"" \
|
|
|
|
""
|
|
|
|
}
|
|
|
|
|
2020-01-30 20:12:08 +00:00
|
|
|
# base directory should always exist
|
|
|
|
base_dir_exists_or_create() {
|
|
|
|
[[ -d "$BASE_PATH" ]] || mkdir "$BASE_PATH"
|
|
|
|
[[ -d "$PACKAGE_PATH" ]] || mkdir "$PACKAGE_PATH"
|
|
|
|
[[ -d "$PROCESSOR_PATH" ]] || mkdir "$BASE_PATH"
|
|
|
|
}
|
|
|
|
|
2020-01-30 16:08:07 +00:00
|
|
|
# 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() {
|
2020-01-30 20:12:08 +00:00
|
|
|
for author in "$PROCESSOR_PATH"/*; do
|
|
|
|
for package in "$author"/*; do
|
|
|
|
for processor in "$package"/*; do
|
|
|
|
[[ -e "$processor" ]] || break
|
|
|
|
[[ -f "$processor" ]] || break
|
|
|
|
if grep -q -e '/theme_[[:alnum:]]\{1,\}$' <<<"$processor"; then
|
|
|
|
printf "%s\n" "$(basename -- "$processor")"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
done
|
2020-01-30 16:08:07 +00:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2020-01-30 18:49:37 +00:00
|
|
|
# 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
|
|
|
|
}
|
|
|
|
|
2020-01-30 20:58:31 +00:00
|
|
|
# temporarily switch theme, same thing as setting, only with permanence flag turned off for processors
|
|
|
|
switch_theme() {
|
|
|
|
set_theme "$1" "false"
|
|
|
|
}
|
|
|
|
|
|
|
|
# call processors for all installed packages
|
2020-01-30 16:08:07 +00:00
|
|
|
set_theme() {
|
|
|
|
local theme="$1"
|
2020-01-30 20:58:31 +00:00
|
|
|
local permanent="${2:-true}"
|
2020-01-30 16:08:07 +00:00
|
|
|
|
|
|
|
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
|
2020-01-30 20:12:08 +00:00
|
|
|
processor=$(find "$PROCESSOR_PATH" -type f -name "theme_$appext")
|
2020-01-30 16:08:07 +00:00
|
|
|
if [[ -f "$processor" ]]; then
|
2020-01-30 20:58:31 +00:00
|
|
|
"$processor" "$PACKAGE_PATH" "$pkg" "$theme" "$permanent"
|
2020-01-30 16:08:07 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-01-30 18:49:37 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-01-30 20:12:08 +00:00
|
|
|
download() {
|
|
|
|
local pkg="$1"
|
|
|
|
local page="https://github.com"
|
|
|
|
local repo="$page/$pkg"
|
|
|
|
|
|
|
|
[[ -z "$pkg" ]] && {
|
|
|
|
echo "No package to download passed in. Please provide a package to download in the form user/repository."
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
type git >/dev/null 2>&1 || {
|
|
|
|
echo "git is required to clone base16 package. Please install git."
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
base_dir_exists_or_create
|
|
|
|
|
|
|
|
if ! git ls-remote --exit-code -h "$repo" >/dev/null; then
|
|
|
|
echo "Repository $repo not found."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# if package has patter name/base16-program, put it in packages; if name/process16-program put it in processors
|
|
|
|
# if none of the above, assume it's a processor but warn the user
|
|
|
|
if grep -q -e '^[0-9A-Za-z-]\{1,\}/base16-[0-9A-Za-z-]\{1,\}$' <<<"$pkg"; then
|
|
|
|
git clone "$repo" "$PACKAGE_PATH/$pkg"
|
|
|
|
elif grep -q -e '^[0-9A-Za-z-]\{1,\}/process16-[0-9A-Za-z-]\{1,\}$' <<<"$pkg"; then
|
|
|
|
git clone "$repo" "$PROCESSOR_PATH/$pkg"
|
|
|
|
else
|
|
|
|
echo "Package does not fit default naming scheme of packages/processors. Assuming it is a processor but please check manually."
|
|
|
|
git clone "$repo" "$PROCESSOR_PATH/$pkg"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2020-01-30 16:08:07 +00:00
|
|
|
main "$@"
|