Skip to content

Instantly share code, notes, and snippets.

@xprnio
Created May 2, 2024 11:37
Show Gist options
  • Save xprnio/6c0ed530276f9a28ec44760cd5a47fb0 to your computer and use it in GitHub Desktop.
Save xprnio/6c0ed530276f9a28ec44760cd5a47fb0 to your computer and use it in GitHub Desktop.
A script for managing the Alacritty theme

alacritty-theme.sh

A script for setting your Alacritty theme that uses an intermediary theme.toml file to keep track of the current theme in order to keep your primary Alacritty configuration safe from accidental modifications.

This script makes the following assumptions:

  1. You have cloned alacritty/alacritty-theme into $HOME/.config/alacritty/themes
  2. You import $HOME/.config/alacritty/theme.toml from $HOME/.config/alacritty/alacritty.toml

Additionally, when setting a theme with an underscore in the filename (eg. github_dark.toml) you can instead use a dash instead: github-dark.

Usage

Getting the current theme

~/.config/alacritty
❯ ./alacritty-theme.sh current
Current theme: github_light

Listing available themes

~/.config/alacritty
❯ ./alacritty-theme.sh list   
Available themes:
  afterglow
  alabaster_dark
  alabaster
  ...

Setting the theme

~/.config/alacritty
❯ ./alacritty-theme.sh set github-dark
Theme changed to github_dark
#!/bin/bash
CONFIG="$HOME/.config/alacritty"
THEMES="$CONFIG/themes/themes"
THEME_TOML="$CONFIG/theme.toml"
SCRIPT="$(basename "$0")"
transform_theme() {
theme="$1"
if [[ "$theme" == "" ]]; then
return
fi
echo "${theme//-/_}"
}
print_usage() {
echo "Usage:"
echo " $SCRIPT <command>"
echo "Commands:"
echo " list List all themes"
echo " current Get current theme"
echo " set <theme> Set theme"
}
verify_theme() {
theme="$1"
if [[ "$theme" == "" ]]; then
echo "[Error] Missing theme"
print_usage
exit 1
fi
path="$THEMES/$theme.toml"
if [[ ! -f "$path" ]]; then
echo "[Error] Invalid theme: $theme"
print_usage
exit 1
fi
}
set_theme() {
theme="$1"
echo "import = [\"$THEMES/$theme.toml\"]" > "$THEME_TOML"
}
print_current() {
current="$(grep -oP '(?<=themes/)[^/]+(?=\.toml")' "$THEME_TOML")"
echo "Current theme: $current"
}
print_themes() {
echo "Available themes:"
for file in "$THEMES"/*.toml; do
theme="$(basename "$file")"
echo " ${theme%.*}"
done
}
case "$1" in
"set")
theme="$(transform_theme "$2")"
verify_theme "$theme"
set_theme "$theme"
;;
"current")
print_current
;;
"list")
print_themes
;;
"help")
print_usage
;;
*)
echo "[Error] Invalid command: $1"
print_usage
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment