-
Notifications
You must be signed in to change notification settings - Fork 405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🚀 Use light/dark theme base on terminal theme or system variable. #447
Comments
I second that. I'm struggling with the same issue. I don't care about the Out of the curiosity, what terminal emulator are you using @tshu-w? Does it support this out-of-the-box? Thank you. |
Hi @tshu-w and @lourenci, yes let's figure out a good way to do this. What do you think of this proposal:
[delta "my-light-mode"]
light = true
file-style = magenta # or whatever other settings you want; this is just an example
[delta "my-dark-mode"]
dark = true
file-style = green # or whatever other settings you want; this is just an example If you're able to build delta from source then you can play around with this now as I've added support for the If this seems like a good way forward then let's put together a concrete example of doing this with a particular OS/terminal emulator/shell combination. |
@tshu-w @lourenci here's a proof-of-principle that uses iTerm2 proprietary escape codes and zsh's preexec hook. Unfortunately the iterm2 escape codes do not work in tmux. The Instructions:
Now run |
Hi, I agree with dandavison that this is not delta's responsibility, but I feel not a few people want this. https://github.com/dalance/termbg If we'll approach as delta side will solve this, I will fix. |
@ulwlu oh, that library looks perfect. I am absolutely happy for us to build in background color detection to Delta. You're right: that would be a much better solution. Terminal color detection is important enough for delta to do it. And I see it has some tmux support... the code I posted above doesn't work in tmux. |
Yes it works in tmux. There are many informations about this library in Japanese articles, so I'm going to give it a try in a few days! |
Ok, sounds great -- I'll leave this one for you to work on. |
I've been trying to think about how we want this to work ultimately. Basically, Ultimately, I think it would be nice if users could have two features defined in their gitconfig, one for their light mode settings, and one for their dark mode settings. These would contain the A related issue is that the code for |
@dandavison @ulwlu Thank you for your efforts, I've been a bit busy recently. Although the solution provided by @dandavison is sufficient, I agree that it would be great for delta to have built-in color detection, so I will leave this issue open. |
I have a custom script to set/get/toggle darkmode states on different OS’. Maybe delta could expose an environment variable (e.g., |
@sscherfke |
bat "solved" this by providing an "ansi" theme, replacing ansi-dark and ansi-light, which automatically switches colors to work in either a dark or light theme. sharkdp/bat#1104 Could delta do the same? |
Hi @skyfaller I'm sorry, I was on the verge of replying to this 3 weeks ago when you said it and then somehow didn't! What I was going to say is thanks, and you prompted me to update delta to bat's latest themes, so |
@dandavison Thank you! It doesn't solve the problem for people who want more advanced themes, no, but for people who just want legible text for now, the "ansi" theme is a fine workaround. I look forward to the next release! Here's what I did to "solve" the problem for myself in the meantime, on Gnome with fish shell using sd. I made a fish function that detects my Gnome theme and uses that to change my .gitconfig to my desired delta theme: function theme-delta --description 'Set delta theme based on gtk-theme'
switch (gsettings get org.gnome.desktop.interface gtk-theme)
case "'Pop'"
sd '^\tsyntax-theme = .*$' '\tsyntax-theme = gruvbox' ~/.gitconfig
case "'Adwaita'"
sd '^\tsyntax-theme = .*$' '\tsyntax-theme = GitHub' ~/.gitconfig
case "'Pop-dark'"
sd '^\tsyntax-theme = .*$' '\tsyntax-theme = Monokai Extended' ~/.gitconfig
case '*'
echo "error: unidentified GTK theme"
end
end Then I use the Night Theme Switcher GNOME Shell extension to run that fish function (and similar functions for Alacritty and bat) at the same time as it changes my theme between light and dark, like so: I'm very interested to see how you finally solve this, termbg looks cool! |
Cool, thanks, that's a lot of helpful links and example code! (And I hadn't come across |
I just tried out the |
I built and tested off of that branch. Works as expected now :) |
* Update to latest version of bat::terminal::to_ansi_color Fixes #447 * Delete mention of historical syntax theme ansi-light * Update tests
Thanks @jtroo! There were just some fairly superficial test failures; in master now. |
@lourenci that looks to be behaving correct to me. The issue is that the green/red bgs are too dark. You may want to adjust your terminal colours, choose a custom green/red for delta, or choose custom text output (as opposed to syntax highlighted). |
Actually, I think there might be an even easier fix. @lourenci, I think you need to add [delta]
light = true
navigate = true
syntax-theme = ansi By default, delta assumes your terminal background is dark, since I think most people use dark (but I'm with you, mine is light too). |
For the record, I'll point out that |
Automatic would be nice dandavison/delta#447
Yes exactly. I use ansi compatible colors for syntax highlighting but still need to revert to 24-bit colors for background highlighting the plus and minus lines. So using ansi syntax is not a full workaround. What is the status of this issue? Has the termbg support been released yet? |
I also want to comment on the
|
I tried termbg, but it needs stdin and stdin is already in use for the pipe. |
@chtenb right, that's true. But you can use some suitable mutable state such as a file on disk, and create a wrapper script around delta that looks at the file to see whether you're in light/dark mode, and invokes delta accordingly.
@floriankisser thanks, yes, I looked into |
That is true, but I'm not sure how I would have the filesystem mutated based on my VSCode theme. I'm sure I could hack something together like inspecting the VSCode config, but a generic solution that is able to inspect the background of the terminal sounds so much more clean and easy to use for everyone. But it sounds like that problem is not solvable in a generic way for every terminal?
It seems that VSCode is not supported so I wouldn't benefit from termbg. |
Fwiw, any of the standard file descriptors, when dhey ase connected to the terminal, are bidirictional. |
I explored solving this problem from a different angle: by only using ansi colors. I used the basic ansi colors (0-15 for syntax highlighting and repurposed some of the extended ansi colors (16-255) as background for colored lines as follows [delta]
syntax-theme = terminal-ansi16
features = zebra-ansi
[delta "zebra-ansi"]
minus-style = syntax 52
minus-emph-style = syntax 88
plus-style = syntax 22
plus-emph-style = syntax 28
map-styles = \
bold purple => syntax 53, \
bold blue => syntax 17, \
bold cyan => syntax 23, \
bold yellow => syntax 58
zero-style = syntax
whitespace-error-style = "#aaaaaa" The syntax theme I used is a custom bat theme terminal-neo-ansi. It only uses ansi colors just like the bat ansi theme, but it's better than the default included ansi theme imho :). If your terminal supports customization of the entire range of ansi colors (0-255) and is able to switch colorschemes based on your system darkmode setting, then you can leverage both these things to achieve an adaptive experience. Here is a video that shows a diff while I switch my system darkmode setting (on Windows 10) using wezterm as my terminal. less.exe.2022-04-15.22-46-28.mp4For the record, this is the wezterm configuration that I used. It's a bit hacky with all the hardcoded colors imho, but it works. Furthermore, I have flux installed and configured to switch systemwide darkmode on/off based on the time of day. |
If you're on a Mac, you can [core]
pager = delta --features "$(defaults read -globalDomain AppleInterfaceStyle &> /dev/null && echo dark-mode || echo light-mode)"
[delta "light-mode"]
light = true
syntax-theme = GitHub
[delta "dark-mode"]
light = false
syntax-theme = Visual Studio Dark+ This precludes the need to wire up something to listen to changes to the system theme and to write that to some environment variable somewhere. |
@seansfkelley Thanks a lot for that snippet! I had to add the following for it to work for [interactive]
diffFilter = delta --color-only --features "$(defaults read -globalDomain AppleInterfaceStyle &> /dev/null && echo dark-mode || echo light-mode)" |
The gitconfig gets executed with
macOS ships with |
This is what works for me on GNOME (Ubuntu 22.10). The below settings checks the system theme and if it sees "dark" in the theme name, delta will use the dark-theme; otherwise light-theme. Had to use [core]
pager = LESS=-R delta --features "$(case "$(gsettings get org.gnome.desktop.interface gtk-theme)" in *"dark"*) echo "dark-theme" ;; *) echo "light-theme" ;; esac)"
[delta]
syntax-theme = none
side-by-side = true
line-numbers = true
[delta "dark-theme"]
minus-style = syntax "#330f0f"
minus-emph-style = syntax "#4f1917"
plus-style = syntax "#0e2f19"
plus-emph-style = syntax "#174525"
map-styles = \
bold purple => syntax "#330f29", \
bold blue => syntax "#271344", \
bold cyan => syntax "#0d3531", \
bold yellow => syntax "#222f14"
zero-style = syntax
whitespace-error-style = "#aaaaaa"
[delta "light-theme"]
minus-style = syntax "#fbdada"
minus-emph-style = syntax "#f6b6b6"
plus-style = syntax "#d6ffd6"
plus-emph-style = syntax "#adffad"
map-styles = \
bold purple => syntax "#feecf7", \
bold blue => syntax "#e5dff6", \
bold cyan => syntax "#d8fdf6", \
bold yellow => syntax "#f4ffe0"
zero-style = syntax
whitespace-error-style = "#aaaaaa" |
FWIW it can be simplified if you don't care which light/dark theme: [core]
pager = git delta
[interactive]
diffFilter = git delta --color-only
[alias]
delta = !delta $(defaults read -g AppleInterfaceStyle &>/dev/null || echo --light)
# or to ensure dark is default in Codespaces (Linux)
delta = !delta $(defaults read -g AppleInterfaceStyle 2>&1 | grep 'does not exist' > /dev/null && echo --light) |
Use the termbg crate to try to read the current terminal background theme (light or dark) and use it as the default if no --light or --dark was specified (or light=true in .gitconfig). This doesn't work in all cases: delta is frequently used with a pipe, in which case it's not really possible to obtain the background color using OSC 11. The fallback is the environment variable COLORFGBG which is supported by most terminals, but it doesn't immediately update when the system theme changes (it only applies to newly opened terminals). Still, I think this is better than assuming a dark background. See dandavison#447 for discussion.
I did some investigating in #1493 (comment) and came to the conclusion that reading the terminal background color using the xterm trick is not reliably possible when used together with less, because of a race condition between less and delta (see that comment for details). I guess @dandavison came to the same conclusion. (stdin is not actually the issue, we can use stderr for read and write: the real issue is a race condition). However, it would be possible to implement support for |
WFM with Lazygit integration in latest macOS on Intel ✅ |
I must have been using my old config. |
@texastoland Yepp, unfortunately automatic detection does not work inside of lazygit, see also #1664. |
Informative thread thank you 🙏🏼 |
Hi, I have set my terminal to auto change themes based on macOS appearance. It would be much appreciated if delta can use light/dark theme base on terminal theme or system variable.
The text was updated successfully, but these errors were encountered: