dotfiles.nix/home/extras/fish/auto-theme.nix

115 lines
3.4 KiB
Nix
Raw Normal View History

2024-07-21 04:25:16 +00:00
{
config,
pkgs,
lib,
...
}:
let
cfg = config.programs.fish;
in
{
options.programs.fish = {
autoTheme = {
enable = lib.mkEnableOption "Automatically change terminal and shell themes with night mode";
light = lib.mkOption {
type = lib.types.str;
description = "Light mode theme";
};
dark = lib.mkOption {
type = lib.types.str;
description = "Dark mode theme";
};
};
};
config = lib.mkIf (cfg.enable && cfg.autoTheme.enable) {
programs.fish = {
shellInit = ''
set -g theme_dark "${config.programs.fish.autoTheme.dark}"
set -g theme_light "${config.programs.fish.autoTheme.light}"
set-background-to-system
'';
interactiveShellInit = ''
set-shell-colors
set-term-colors
'';
functions =
{
# toggles `$term_background` between "light" and "dark". other Fish functions trigger when this
# variable changes. we use a universal variable so that all instances of Fish have the same
# value for the variable.
toggle-background.body = ''
if test "$term_background" = light
set -U term_background dark
else
set -U term_background light
end
'';
# set `$term_background` based on whether macOS is light or dark mode
set-background-to-system.body = ''
if is-dark-mode
set -U term_background dark
else
set -U term_background light
end
'';
# set `$term_background` based on an env var
set-background-to-env = {
argumentNames = [ "env_var" ];
body = ''
switch $$env_var
case 1
echo "Setting dark mode"
set -U term_background dark
case 0
echo "Setting light mode"
set -U term_background light
end
'';
};
# sets shell utilities to light or dark colorscheme based on `$term_background`.
set-shell-colors = {
body = ''
# Use correct theme for `btm` and `bat`
if test "$term_background" = light
alias btm "btm --color nord-light"
set -xg BAT_THEME OneHalfLight
else
alias btm "btm --color nord"
set -xg BAT_THEME OneHalfDark
end
# Set LS_COLORS
set -xg LS_COLORS (${pkgs.vivid}/bin/vivid generate one-$term_background)
'';
onVariable = "term_background";
};
# use theme.sh to update terminal colors
set-term-colors = {
body = ''
set -l theme theme_$term_background
theme.sh $$theme
'';
onVariable = "term_background";
};
# do i need to implement for linux?
is-dark-mode = abort "is-dark-mode is not defined on ${pkgs.stdenv.system} yet.}";
}
// lib.optionalAttrs pkgs.stdenv.isDarwin {
# determine if dark mode is active
is-dark-mode.body = ''
defaults read -g AppleInterfaceStyle &>/dev/null
'';
};
shellAbbrs = {
tb = "toggle-background";
sb = "set-background-to-system";
};
};
};
}