Skip to content

Instantly share code, notes, and snippets.

@enihsyou
Last active July 13, 2023 07:31
Show Gist options
  • Save enihsyou/976e6ff47ad1bfe55c650e4556125e44 to your computer and use it in GitHub Desktop.
Save enihsyou/976e6ff47ad1bfe55c650e4556125e44 to your computer and use it in GitHub Desktop.
Make kitty terminal emulator aware macOS system appearance change (aka Dark Mode).

What's this

Make kitty terminal emulator aware macOS system appearance change. But not perfect, since this script is not "listening" change event, but rather be triggered by new shell creation.

How to use

  1. pick two theme conf, one for light (basic-light.conf here) and one for dark (basic-dark.conf)
  2. create two file inside KITTY_CONFIG_DIRECTORY(~/.config/kitty here), macos-launch-services-cmdline-dark.conf and macos-launch-services-cmdline-light.conf
  3. tweak macos-launch-services-cmdline-*.conf, add kitty.conf and theme.conf, note that args should use full path.
  4. make kitty-appearance-switch.zsh sourced automatically by adding it to ~/.zshrc

How it's done

Simply source an zsh script(kitty-appearance-switch.zsh) when a new shell starts.

It does folling things:

  1. check and only apply to macOS(darwin) system.
  2. read AppleInterfaceStyle to tell if "Dark Mode" is on.
  3. conditionally symlink corresponding cmdline file inside KITTY_CONFIG_DIRECTORY.
  4. tell all exising kitty instance use new color theme through kitty set_colors action

extacted from my kitty dotfile, updates will goes to the repo.

# tell whether is macOS Dark mode is on.
is_system_appearence_dark() {
if [ "Dark" = "$(defaults read -g AppleInterfaceStyle 2> /dev/null)" ]; then
return 0
else
return 1
fi
}
link_kitty_launch_cmdline() {
base="$HOME/.config/kitty"
link="$HOME/.config/kitty/macos-launch-services-cmdline"
case $1 in
dark)
want="$base/macos-launch-services-cmdline-dark.conf"
file="$base/basic-dark.conf"
;;
light)
want="$base/macos-launch-services-cmdline-light.conf"
file="$base/basic-light.conf"
;;
esac
if [ ! "$link" -ef "$want" ]; then
(cd "$base" && gln --symbolic --force --verbose "$want" "$(basename "$link")")
kitty @ set-colors --configured --all "$file"
fi
}
if is_system_appearence_dark; then
link_kitty_launch_cmdline "dark"
else
link_kitty_launch_cmdline "light"
fi
unset -f link_kitty_launch_cmdline
--config /Users/enihsyou/.config/kitty/kitty.conf
--config /Users/enihsyou/.config/kitty/basic-dark.conf
--override macos_thicken_font=0.75
--config /Users/enihsyou/.config/kitty/kitty.conf
--config /Users/enihsyou/.config/kitty/basic-light.conf
--override macos_thicken_font=0.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment