Created
April 12, 2022 23:19
-
-
Save trk9001/ab230a9c7821bdfcba7ea47624c5f13a to your computer and use it in GitHub Desktop.
Update kitty's colors from a terminal.sexy color scheme.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# vim: sw=2 sts=2 et | |
# Update kitty's colors from a terminal.sexy color scheme (exported in JSON). | |
# | |
# Although there are at least a couple of PRs on terminal.sexy that add support | |
# for exporting color schemes for kitty, development on the repo seems to have | |
# gone stale and the PRs remain unmerged. This script reads a color scheme | |
# exported in the "JSON Scheme" format, and loads it into kitty's config after | |
# backing it up. Backups are created beside the original config. Dependencies | |
# are GNU coreutils and jq. | |
# | |
# chmod u+x it and run it with the --help option for usage information. | |
set -euo pipefail | |
helptxt="Update kitty's colors from terminal.sexy. | |
Usage: $(basename "$0") [options] [file...] | |
This utility reads a terminal color scheme exported from | |
terminal.sexy in the JSON Scheme format, and loads it into kitty's | |
config. Requires jq (https://github.com/stedolan/jq). | |
Options: | |
-h, --help Show help." | |
show_help() | |
{ | |
echo -e "$helptxt" | |
} | |
show_error() | |
{ | |
echo -e "ERROR: ${1:-'An error occurred.'}" >&2 | |
} | |
assert_dependencies_present() | |
{ | |
if ! command -v jq >/dev/null; then | |
show_error 'jq not found.' | |
exit 1 | |
fi | |
} | |
main() | |
{ | |
assert_dependencies_present | |
# --- PARSE CMD-LINE ARGS --- | |
local argv=("$@") | |
declare -a args=() | |
for (( i=0; i<${#argv[@]}; i++ )); do | |
local arg=${argv[$i]} | |
case $arg in | |
--) | |
local next_idx=$(( i + 1 )) | |
for (( j=next_idx; j<$#; j++ )); do | |
args+=("${argv[$j]}") | |
done | |
break | |
;; | |
-h | --help) | |
show_help | |
exit 0 | |
;; | |
-*) | |
local msg="Unknown option $arg." | |
msg="$msg\nUse the --help option for help." | |
show_error "$msg" | |
exit 1 | |
;; | |
*) | |
args+=("$arg") | |
;; | |
esac | |
done | |
if [[ ${#args[@]} -gt 1 ]]; then | |
show_error "Too many arguments" | |
exit 1 | |
fi | |
# --- PARSE THE JSON COLOR SCHEME --- | |
file=${args[0]:-/dev/stdin} | |
if [[ ! -r $file ]]; then | |
show_error "Cannot open file $file for reading." | |
exit 1 | |
fi | |
cs=$(<"$file") | |
if [[ -z $cs ]]; then | |
show_error "Input is empty." | |
exit 1 | |
fi | |
declare -A colors | |
declare -a colors_18 | |
mapfile -t colors_18 < \ | |
<(echo -e "$cs" | jq --raw-output '.color[],.foreground,.background') | |
for i in {0..15}; do | |
colors["color$i"]=${colors_18[$i]} | |
done | |
colors[foreground]=${colors_18[16]} | |
colors[background]=${colors_18[17]} | |
# --- BACK-UP & UPDATE KITTY'S CONFIG --- | |
local kitty_conf="${XDG_CONFIG_HOME:-$HOME/.config}/kitty/kitty.conf" | |
if [[ ! -w $kitty_conf ]]; then | |
show_error "Cannot open kitty's config at $kitty_conf for writing." | |
exit 1 | |
fi | |
kitty_conf_bak="$kitty_conf.$(date +'%Y%m%d%H%M%S').bak" | |
cp "$kitty_conf" "$kitty_conf_bak" | |
for key in "${!colors[@]}"; do | |
sed --regexp-extended --in-place \ | |
"/^${key}[[:blank:]]/ s/#[[:xdigit:]]{6}/${colors[$key]}/" \ | |
"$kitty_conf" | |
done | |
return $? | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment