Skip to content

Instantly share code, notes, and snippets.

@UrsaDK
Last active June 2, 2023 05:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save UrsaDK/75b4e0d09f07993ffcf07b8abe2cd1ec to your computer and use it in GitHub Desktop.
Save UrsaDK/75b4e0d09f07993ffcf07b8abe2cd1ec to your computer and use it in GitHub Desktop.
Make command line tools respect macOS appearance

MacOS dark mode & command line tools

  1. Beta version of iTerm2 has built-in support for system appearance and will change themes in sync with macOS;
  2. Setup dark-mode-notify to launch on start and call a custom shell script (eg: appearance-change.sh) when appearance changes;
  3. Update the script as required

NOTE:

  • neovim v0.8.0+ can issue self referencing commands. For example, try putting this into your init.lua to prevent nested vim instances when using built-in terminal:
    vim.env.EDITOR = string.format('nvim --server %s --remote --', vim.api.nvim_get_vvar('servername'))
  • A single command can be executed in all instances of neovim using the vollowing shell function:
    in_all_nvim() {
      : "${1:?Missing required argument: cmd}"
      ls "${XDG_RUNTIME_DIR:-${TMPDIR}nvim.${USER}}"/*/nvim.*.0 \
        | xargs -I SERVER /usr/local/bin/nvim --server SERVER --remote-send "<cmd>${1}<cr>"
    }
    • The use of <cmd>, rather than :, when sending a command to vim, ensures that the command is executed without affecting the user's current mode. This avoid a flickering effect if the user is not in Normal mode.
#!/usr/bin/env sh
_is_dark_mode() {
defaults read -g AppleInterfaceStyle 1>&- 2>&-
}
_in_all_nvim() {
: "${1:?Missing required argument: cmd}"
ls "${XDG_RUNTIME_DIR:-${TMPDIR}nvim.${USER}}"/*/nvim.*.0 \
| xargs -I SERVER /usr/local/bin/nvim --server SERVER --remote-send "<cmd>${1}<cr>"
}
# Set by appearance-monitor, but missing if
# the script is invoked from the command line.
if [ -z "${DARKMODE}" ]; then
if _is_dark_mode; then
DARKMODE=1
else
DARKMODE=0
fi
fi
if [ "${DARKMODE}" = '1' ]; then
# This code is executed when macOS switches from Light to Dark mode
_in_all_nvim "lua \
require('projekt0n.github-theme_helper').colorscheme('dark')"
ln -sf dark.yaml ~/.config/lsd/themes/local.yaml
else
# This code is executed when macOS switches from Dark to Light mode
_in_all_nvim "lua \
require('projekt0n.github-theme_helper').colorscheme('light')"
ln -sf light.yaml ~/.config/lsd/themes/local.yaml
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment