Skip to content

Instantly share code, notes, and snippets.

@mattmc3
Last active March 5, 2023 00:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattmc3/9cb2980cae6503df3a7e3fc870ae38c5 to your computer and use it in GitHub Desktop.
Save mattmc3/9cb2980cae6503df3a7e3fc870ae38c5 to your computer and use it in GitHub Desktop.
Keyboard.io palette JSON generator
#!/bin/sh
# Author: mattmc3
# Copyright: 2023
# License: MIT
print_err() {
printf '%s\n' "$my_name: Error: $1" >&2
exit ${2:-1}
}
init() {
if ! command -v fzf >/dev/null; then
print_err "Command not found 'fzf'. Install from https://github.com/junegunn/fzf."
fi
iterm2dir=$XDG_CACHE_HOME/palette/iterm2cs
if ! test -d "$iterm2dir"; then
git clone --depth 1 --quiet https://github.com/mbadolato/iTerm2-Color-Schemes $iterm2dir 2>/dev/null
regen_theme_files
fi
unset iterm2dir
}
regen_theme_files() {
# use .conf files from lxterminal to make .sh files we can source later
rm -rf -- ${my_workdir}/themes
mkdir -p ${my_workdir}/themes
for file in ${iterm2dir}/lxterminal/*.conf; do
grep 'palette_color_[0-7]=' "$file" | sort | \
awk '
BEGIN{FS="[_=]"; OFS="="; c="black_red_green_yellow_blue_magenta_cyan_white"; split(c, colors)}
{print colors[$3+1],"\""$4"\""}
' > "${my_workdir}/themes/$(basename "$file" .conf).sh"
done
unset file
}
palette_update() {
echo "Updating color schemes..."
git -C "$XDG_CACHE_HOME/palette/iterm2cs" pull --quiet
regen_theme_files
echo "Done."
}
palette_choose() {
result=$(palette_list | fzf --layout=reverse-list --preview "palette preview ${my_workdir}/themes/{}.sh" --query="$@")
[[ $? -eq 0 ]] || return 1
palette_print "${my_workdir}/themes/${result}.sh"
unset result
}
palette_print() {
if ! test -f "$1"; then
print_err "Color scheme file not found '$1'."
fi
source "$1"
rgb_fmt=' {"r": %s, "g": %s, "b": %s, "rgb": "rgb(%s, %s, %s)"}'
printf '%s\n' '"palette": ['
printf "$rgb_fmt" 0 0 0 0 0 0
for color_name in red green yellow blue magenta cyan white black; do
rgb="$(eval echo $(echo "\$$color_name"))"
set -- $(echo "$rgb" | awk -F '[\(\),]' '{print $2, $3, $4}')
printf ",\n${rgb_fmt}" $1 $2 $3 $1 $2 $3
done
for num in $(seq 1 7); do
printf ",\n${rgb_fmt}" 0 0 0 0 0 0
done
printf '\n%s\n' ']'
unset rgb_fmt num rgb
}
palette_preview() {
if ! test -f "$1"; then
print_err "Color scheme file not found '$1'."
fi
source "$1"
for color_name in red green yellow blue magenta cyan white black
do
rgb="$(eval echo $(echo "\$$color_name"))"
set -- $(echo "$rgb" | awk -F '[\(\),]' '{print $2, $3, $4}')
printf "\x1b[38;2;%s;%s;%sm%-10s\x1b[0m rgb(%3s, %3s, %3s)\n" $1 $2 $3 $color_name $1 $2 $3
done
unset color_name rgb
}
palette_list() {
for file in "${my_workdir}"/themes/*.sh; do
basename "$file" .sh
done
unset file
}
# declare vars
my_name="palette"
test -n "$XDG_CACHE_HOME" || XDG_CACHE_HOME=$HOME/.cache
my_workdir=$XDG_CACHE_HOME/palette
my_help="\
Usage: $my_name [options] <command>
Options:
-h, --help Print this help and exit
Subcommands:
choose Choose LED color scheme (default)
list List available color schemes
update Update available color schemes
preview Preview color scheme
"
# parse options
while test "${1#-}" != "$1"; do
case "$1" in
-h | --help)
printf '%s\n' "$my_help"
exit 0
;;
--)
# No more options to process
shift
break
;;
*)
print_err "invalid option $1"
;;
esac
done
init
if test -z "$1"; then
my_cmd=choose
else
my_cmd=$1; shift
fi
if ! typeset -f "palette_${my_cmd}" >/dev/null; then
print_err "Subcommand not found '$my_cmd'."
else
my_name="$my_name $my_cmd"
"palette_${my_cmd}" "$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment