shchemes/shchemes
2024-04-01 10:08:49 -06:00

264 lines
4.8 KiB
Bash
Executable File

#!/usr/bin/env bash
# set -e
shchemes_path=$(readlink -f "${BASH_SOURCE[0]}")
shchemes_dir=$(dirname "$shchemes_path")
scheme="$SCHEME"
args=()
for i in "$@"; do
case $i in
-s=*|--scheme=*)
scheme="${i#*=}"
;;
-f=*|--format=*)
format="${i#*=}"
;;
-c=*|--comment-pattern=*)
comment_pattern="${i#*=}"
;;
--help)
args[0]="manual"
;;
*)
if [[ "$i" == "-"* ]]; then
echo "Invalid option \"$i\""
exit 1
fi
args+=("$i")
esac
done
method=${args[0]}
parse_scheme() {
if [ -z "$scheme" ]; then
echo "Please set a scheme"
exit 1
fi
scheme_format=${scheme%:*}
scheme_name=${scheme#*:}
# No format
if [ "$scheme_format" == "$scheme_name" ] ; then
scheme_format="base16"
fi
scheme_path="${shchemes_dir}/${scheme_format}/schemes/${scheme_name}.properties"
}
check_scheme_path() {
if [ ! -r "$scheme_path" ]; then
echo "Found no color scheme for \"${scheme_name}\" of format \"${scheme_format}\""
exit 1
fi
}
parse_format() {
if [ -r "$format" ]; then
format_dir="$format"
else
if [ -z "$format" ]; then
format=$scheme_format
fi
format_dir="${shchemes_dir}/${format}"
fi
}
parse_comment_pattern() {
if [ -z "$comment_pattern" ]; then
get_comment_start
else
readarray -td " " comment_pattern < <(echo "$comment_pattern")
comment_start="${comment_pattern[0]}"
comment_end="${comment_pattern[1]}"
fi
}
get_comment_start() {
local ext
ext=$(basename "$path")
ext="${ext##*.}"
case $ext in
js|rasi)
comment_start="//"
;;
css)
comment_start="/*"
comment_end="*/"
;;
*)
comment_start="#"
esac
}
create_theme() {
target=$1
parse_scheme
check_scheme_path
parse_format
if [ -n "$target" ]; then
if [ -r "$target" ]; then
template_path="$target"
else
template_path=${format_dir}/templates/${target}.mustache
if [ ! -r "$template_path" ]; then
echo "No viable ${scheme_format} template for \"${target}\""
exit 1
fi
fi
fi
if [ -z "$MO_PATH" ]; then
MO_PATH="${shchemes_dir/lib/mo/mo}"
fi
bash "${shchemes_dir}/lib/apply_scheme_to_template.sh" "$scheme_path" "$template_path"
}
inject() {
path=$1
parse_scheme
check_scheme_path
parse_comment_pattern
if [ "$comment_start" == "" ]; then
comment_start="#"
fi
awk -i inplace \
-v blockstart="${comment_start}START SHCHEMES BLOCK" \
-v blockend="${comment_start}END SHCHEMES BLOCK" \
-v commentend="$comment_end" \
-v scheme="$scheme" \
-v shchemes="$shchemes_path" \
-v sq="'" \
-F ":" '
!inblock { print }
inblock {
lines[++bi] = $0
}
$1 ~ blockstart {
inblock = 1;
bi = 0;
command = substr($2, 0, length($2) - length(commentend))
system("/usr/bin/env bash -c " sq "SCHEME=\"" scheme "\";" command sq);
next
}
$1 ~ blockend {
inblock = 0;
print;
next
}
ENDFILE {
if(inblock) {
print blockend commentend;
for(i=1;i<=bi;i++) {
print lines[i];
}
}
}
' "$path"
}
convert_tinted_template() {
path=$1
sed -r \
-e 's/\{\{base([0-9A-F]+)-hex\}\}/{{base\1}}/' \
-e 's/\{\{base([0-9A-F]+)-rgb-r\}\}/{{rgb_r base\1}}/' \
-e 's/\{\{base([0-9A-F]+)-rgb-g\}\}/{{rgb_g base\1}}/' \
-e 's/\{\{base([0-9A-F]+)-rgb-b\}\}/{{rgb_b base\1}}/' "$path"
}
convert_tinted_scheme() {
path=$1
awk -i inplace -F ' *: *' '
/^#/ { print }
$1 ~ "palette" { p = 1; next }
p && /^ +/ { gsub(/ /, "", $1); print $1 "=" $2 }
' "$path"
}
install_tinted_scheme() {
if [ -n "$1" ]; then
scheme=$1
fi
parse_scheme
# output="${shchemes_dir}/${scheme_format}/schemes/${scheme_name}.properties"
if [ ! -d "$(dirname "$scheme_path")" ]; then
echo "No folder for format"
exit 1
fi
url="https://raw.githubusercontent.com/tinted-theming/schemes/master/${scheme_format}/${scheme_name}.yaml"
http_code=$(
curl \
--silent \
--output "$scheme_path" \
--write-out "%{http_code}" \
"$url")
if [[ $http_code -lt 200 || $http_code -gt 299 ]]; then
echo "No such color scheme"
exit 1
fi
convert_tinted_scheme "$scheme_path"
echo "Installed ${scheme}"
}
install_tinted_template() {
url=$1
target=$2
curl "$url" | convert_tinted_template > "${shchemes_dir}/base16/templates/${target}.mustache"
}
manual() {
man "${shchemes_dir}/doc/shchemes.1"
}
if [ "$method" != "" ]; then
case $method in
create_theme)
create_theme "${args[1]}"
;;
inject)
inject "${args[1]}" "${args[2]}"
;;
convert_tinted_template)
convert_tinted_template "${args[1]}"
;;
install_tinted_template)
install_tinted_template "${args[1]}" "${args[2]}"
;;
convert_tinted_scheme)
convert_tinted_scheme "${args[1]}"
;;
install_tinted_scheme)
install_tinted_scheme "${args[1]}" "${args[2]}"
;;
help|manual|man)
manual
;;
*)
echo "No such method"
esac
fi