Skip to content

Instantly share code, notes, and snippets.

@lamchau
Last active January 17, 2025 03:50
Show Gist options
  • Save lamchau/c008c4690f21b392437017e72ba82c51 to your computer and use it in GitHub Desktop.
Save lamchau/c008c4690f21b392437017e72ba82c51 to your computer and use it in GitHub Desktop.
utility to set wezterm color schemes using a fuzzy selector
#!/usr/bin/env bash
# HACK: edit config[1] location until `wezterm config` or `wezterm debug` is
# exposed for programmatic access.
# [1]: https://github.com/wez/wezterm/issues/4238
config_file="$XDG_CONFIG_HOME/wezterm/wezterm.lua"
script_dir="$(dirname "$(realpath "$0")")"
color_scheme_cache="$script_dir/color_schemes.txt"
one_day_in_seconds=$((24 * 60 * 60))
# functions --------------------------------------------------------------------
check_configuration() {
if [ ! -f "$config_file" ]; then
echo "[error]: configuration '$config_file' not found"
exit 1
fi
local color_scheme_count
color_scheme_count="$(grep --invert-match \
--extended-regexp '^\\s*--' "$config_file" |
grep --count 'color_scheme')"
if [ "$color_scheme_count" -gt 1 ]; then
echo "[error]: multiple 'color_scheme' configurations found, expecting 1"
elif [ "$color_scheme_count" -eq 0 ]; then
echo "[error]: expecting at least 1 'color_scheme' configuration"
fi
}
check_required_commands() {
local missing=false
for cmd in "$@"; do
if ! command -v "$cmd" >/dev/null 2>&1; then
echo "[error]: $cmd command not found"
missing=true
fi
done
if [ "$missing" = true ]; then
exit 1
fi
}
update_color_cache() {
local should_update=true
if [ -f "$color_scheme_cache" ]; then
current_time=$(date +'%s')
modified_time=$(stat -f %m "$color_scheme_cache")
difference=$((current_time - modified_time))
if [ $difference -le $one_day_in_seconds ]; then
should_update=false
fi
fi
if [ ! -f "$color_scheme_cache" ] || [ "$should_update" = true ]; then
echo "[info]: caching colors to $color_scheme_cache"
# HACK: `wezterm ls-fonts` doesn't have an equivalent like `wezterm ls-schemes`
url="https://raw.githubusercontent.com/wez/wezterm/refs/heads/main/docs/colorschemes/data.json"
curl --silent "$url" |
jq --raw-output '.[].metadata.name' |
sort --ignore-case > "$color_scheme_cache"
fi
if [ ! -f "$color_scheme_cache" ]; then
echo "[error]: failed to create $color_scheme_cache"
fi
}
select_color() {
previous_color="$(awk -F"'" '/color_scheme/ {print $2}' "$config_file")"
# HACK: use --bind to include highlighted item before exiting
selected_colors="$(
cat "$color_scheme_cache" | fzf \
--reverse \
--multi \
--no-sort \
--bind 'enter:execute(echo set color scheme to: {})+accept' \
--preview-window="down,0" \
--preview "sed -i \"\" \"s/color_scheme.*/color_scheme = {},/\" \"$config_file\""
)"
if [ -n "$selected_colors" ]; then
IFS=$'\n' read -r -d '' -a selected_colors_array <<< "$selected_colors"
if [ "${#selected_colors_array[@]}" -gt 1 ]; then
echo "selected: "
for x in "${selected_colors_array[@]}"; do
echo " - $x"
done
fi
elif [ -n "$previous_color" ]; then
echo "[warn]: no colors selected, restoring previous color '$previous_color'"
sed -i "" "s/color_scheme.*/color_scheme = '$previous_color',/" "$config_file"
else
sed -i "" "s/color_scheme.*/color_scheme = 'Builtin Dark',/" "$config_file"
fi
}
# main -------------------------------------------------------------------------
check_configuration
check_required_commands \
"curl" \
"fzf" \
"jq"
update_color_cache
select_color
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment