lazygit icon indicating copy to clipboard operation
lazygit copied to clipboard

Automatically select light/dark theme based on `color-palette-update-notifications`

Open cor opened this issue 9 months ago • 9 comments

Is your feature request related to a problem? Please describe.

Most modern operating systems and desktop environments do support Dark and Light themes, this includes at least MacOS, Windows, KDE Plasma, Gnome, and probably others.

Some even support switching from dark to light and light to dark mode based on sun rise / sun set.

In order to not make the terminal emulator look bad after such switch, we must enable the applications inside the terminal to detect when the terminal has updated the color palette. This may happen either due to the operating system having changed the current theme or simply because the user has explicitly requested to reconfigure the currently used theme (e.g. because the user requested to change the terminal profile, also containing a different color scheme).

Describe the solution you'd like Adopt the color-palette-update-notifications standard.

Describe alternatives you've considered All other solutions are kinda bad. It's good that we finally have a standard for this.

Additional context Already supported by Ghostty, Kitty, Neovim, Tmux. WezTerm, Zellij, and Helix are adding support at the moment.

cor avatar Mar 06 '25 13:03 cor

Lazygit doesn't have a concept of light or dark themes; it is your terminal emulator that does. And as you say, many of them have support for this already or are adding support for it, so I'm not sure what you think is left for lazygit to do here.

stefanhaller avatar Mar 06 '25 15:03 stefanhaller

Lazygit doesn't have a concept of light or dark themes; it is your terminal emulator that does. And as you say, many of them have support for this already or are adding support for it, so I'm not sure what you think is left for lazygit to do here.

Currently, lazygit only allows you to have one theme configured: https://github.com/jesseduffield/lazygit/blob/19ac926116863740e63cf7dabcb46845c8d41b91/docs/Config.md?plain=1#L112

You can however override this theme by doing something like this on startup: https://github.com/jesseduffield/lazygit/blob/19ac926116863740e63cf7dabcb46845c8d41b91/docs/Config.md?plain=1#L746

but that is an inferior solution, as it means that if you launch Lazygit with your "light theme configuration", and then switch your OS to dark mode, any instance of lazygit that was spawned before the switch will be stuck in the wrong theme.

The ideal solution would be to have two themes in the config, a dark_theme and a light_theme, and implement the color-palette-update-notifications protocol such that currently running instances automatically change on OS theme switch

cor avatar Mar 06 '25 16:03 cor

I see. So the main feature request is not so much to support automatic switching of light/dark themes, but to add a distinction between light and dark themes in the first place; that's probably the bigger challenge, because it involves some design work (for example, to make it so that the distinction is only relevant for people who care about it, but keep it convenient for those who don't. Personally I don't, and I would like to keep setting my colors unconditionally without having to care about putting them in the right sub-theme).

Once we have that, adding the automatic switching shouldn't be a big deal.

stefanhaller avatar Mar 06 '25 16:03 stefanhaller

Another quirk is that the theme information should be ideally passed down to the diff engine, e.g. pager: delta --dark vs pager: delta --light. (though ideally the diff engine itself would also have support for CSI 2031, and then this isn't needed)

Dom324 avatar Mar 11 '25 21:03 Dom324

I think delta does support detecting the terminal colors and adjusting its light/dark color presets based on them and it seems to work as expected when used as a native pager for git. However, when used as a pager for lazygit the delta color preset does not seem to be applied correctly which means when system switches from dark to light mode or vice-versa I have to manually update the lazygit config to add --dark or --light to the pager settings.

Not sure if there is a better way to handle that.

rbhanot4739 avatar Jun 25 '25 10:06 rbhanot4739

Related: https://github.com/jesseduffield/lazygit/issues/4550

pricci1 avatar Oct 29 '25 13:10 pricci1

I did this as a workaround on macOS - This may not check my actual terminal setup, but it will automatically explicitly add --dark or --light to the delta pager based on whether my macOS system is currently in dark mode or not.

#!/bin/bash
# ~/.config/lazygit/delta.sh - Make sure to chmod +x this file

# Check if dark mode is enabled
if defaults read -g AppleInterfaceStyle &>/dev/null; then
  exec delta --dark "$@"
else
  exec delta --light "$@"
fi

And then use it:

# ~/.config/lazygit/config.yml
git:
  paging:
    colorArg: always
    pager: ~/.config/lazygit/delta.sh --paging=never --line-numbers --hyperlinks --hyperlinks-file-link-format="lazygit-edit://{path}:{line}"

pelletencate avatar Nov 06 '25 11:11 pelletencate

Good idea. This works for me in KDE Plasma

#!/usr/bin/env bash

detect_theme() {
  # KDE Plasma
  if command -v plasma-apply-colorscheme &>/dev/null; then
    local theme_list=$(plasma-apply-colorscheme -l 2>/dev/null)
    local current_theme=$(echo "$theme_list" | grep -E '\*.*current' | sed 's/\*//g' | awk '{print $1}')

    if [[ "$current_theme" =~ [Ll]ight ]]; then
      echo "light"
      return
    elif [[ "$current_theme" =~ [Dd]ark ]]; then
      echo "dark"
      return
    fi
  fi

  # Default
  echo "light"
}

theme=$(detect_theme)

if [[ "$theme" == "light" ]]; then
  delta --light "$@"
else
  delta --dark "$@"
fi

pricci1 avatar Nov 11 '25 11:11 pricci1

Another workaround: Since delta respects bat theme, you can set themes via env variables.

# Bat setting
export BAT_THEME=""
export BAT_THEME_LIGHT="Catppuccin Latte"
export BAT_THEME_DARK="Catppuccin Frappe"
export BAT_STYLE="full,-grid,-header-filesize,-header-filename"

# BAT_THEME
x=$(defaults read .GlobalPreferences AppleInterfaceStyle 2>/dev/null) # Macos
if [[ $x = "Dark" ]]
  then
    BAT_THEME=$BAT_THEME_DARK
    FZF_DEFAULT_OPTS=$FLEXOKI_DARK
  else
    BAT_THEME=$BAT_THEME_LIGHT
    FZF_DEFAULT_OPTS=$FLEXOKI_LIGHT
fi

t40mas avatar Nov 14 '25 13:11 t40mas