Skip to content
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

Automatic day/night colors switching based on vim.o.background #63

Closed
strayer opened this issue Jun 7, 2024 · 10 comments · Fixed by #62
Closed

Automatic day/night colors switching based on vim.o.background #63

strayer opened this issue Jun 7, 2024 · 10 comments · Fixed by #62
Labels
autoclosed No activity for more than 7 days with the "waiting for op" label. question Further information is requested

Comments

@strayer
Copy link

strayer commented Jun 7, 2024

Question or Suggestion

I'm currently using tokyonight and it automatically switches between day and night themes based on vim.o.background being dark or light. This is very helpful in combination with tools that switch colorschemes based on system preferences.

For example, I have a system daemon that sends SIGUSR1 to all running neovim instances and have this lua code that switches the vim background:

local os_is_dark = function()
  return (vim.call(
    'system',
    [[echo $(defaults read -globalDomain AppleInterfaceStyle &> /dev/null && echo 'dark' || echo 'light')]]
  )):find('dark') ~= nil
end

local is_dark = function()
  return vim.o.background == 'dark'
end

local set_from_os = function()
  if os_is_dark() then
    vim.o.background = 'dark'
  else
    vim.o.background = 'light'
  end
end

local init = function()
  set_from_os()

  vim.api.nvim_create_autocmd('Signal', {
    pattern = '*',
    callback = function()
      set_from_os()
    end,
  })
end

local toggle = function()
  if is_dark() then
    vim.o.background = 'light'
  else
    vim.o.background = 'dark'
  end
end

return {
  toggle = toggle,
  init = init,
}

Cyberdream currently doesn't handle the color switch based on the background color. There now is a CyberdreamToggleMode vim cmd that toggles the theme that I can try to reimplement that in my lua code above to explicitly switch to dark or light colors, but ideally cyberdream could handle this on its own based on the vim.o.background value.

@strayer strayer added the question Further information is requested label Jun 7, 2024
@scottmckendry
Copy link
Owner

Thanks for the suggestion! I will try and implement this around the logic I've already got for the toggle command.

Quick question - with your current configuration with tokyonight.nvim, do you have to close and re-open Neovim, or does it change in realtime? I'm just wondering if I need to hook into the OptionSet event or just apply the change on initial setup.

@scottmckendry
Copy link
Owner

So there is voodoo magic that auto-refreshes the colorscheme to the new variant when setting vim.o.background that made this nice and easy to implement. Wish I'd known about this before I wrote all the logic for the user command!

Either way, both options can live harmoniously together and people can pick and choose what they want.

If you want to take advantage of this, you can set theme.variant in your setup function to auto and it will use the value of vim.o.background to set light/dark themes.

I did end up creating an autocmd after all so we can also use this to set the lualine theme on the fly as well as trigger any custom user autocmds with the CyberdreamToggleMode event.

Hope you like it! Thanks again for the suggestion, I appreciate your support for the project 🙂

@strayer
Copy link
Author

strayer commented Jun 10, 2024

Thanks, it seems to work fine in general! Some things I noticed:

I think the autocmd would run even if the active colorscheme isn't cyberdream, right? Maybe it makes sense to add a check for the active colorscheme for people frequently switching (like me 😅)

I think there are issues with lualine:


System theme is light, vim.o.background as well. After launching neovim the lualine is still in dark mode:

53331

Running :CyberdreamToggleMode twice fixes the lualine colorscheme (but breaks subsequent system theme changes for some reason, didn't look to much into it)


System theme is dark, vim.o.background as well. After launching neovim everything is fine:

20062

Switching the system theme to light causes the lualine to go black and white:

3450

It stays like that when switching system themes again.

@scottmckendry
Copy link
Owner

Thanks for the extensive testing @strayer !

I've fixed the logic of the autocmd so it only runs when cyberdream is the current selected Neovim colorscheme.

I'm gonna need some time to think about the other problems and how best to approach them.

I've made it somewhat difficult for myself with lualine support and two distinct ways of switching the theme.

Two issues left to resolve:

  1. Lualine should select the correct theme based on variant in cyberdream opts. It needs to support both light, default and the current setting of vim.o.background if auto is selected. Currently it can only do the first two at launch.
  2. The user command for toggling the mode works as intended when using light or default themes, but has unintended side effects if using the auto. The workaround for this currently is to only use :set bg=[dark or light] when changing modes.

@scottmckendry
Copy link
Owner

Okay, both of the issues mentioned in my previous comment should now be resolved. Unfortunately the fix has resulted in a breaking change for the lualine theme which I've documented in #65 (pinned issue).

Can you retest after updating to v2.0.0 and making the required change to your lualine.nvim config?

@scottmckendry
Copy link
Owner

Ignore my last comment, it turns out I didn't need to introduce breaking changes after all. You shouldn't need to make any config updates for this to work now.

Knowing my luck, I've probably missed an edge case somewhere along the way. Appreciate if you could test and let me know 🙂

@scottmckendry scottmckendry added the waiting for op This issue is waiting for a response from the original poster label Jun 13, 2024
@strayer
Copy link
Author

strayer commented Jun 16, 2024

Thanks for taking your time to work on this! The lualine is now always the correct color scheme on start. The issue with the black & white lualine is still there for running instances after toggling the system theme. It still fixes itself when running :CyberdreamToggleMode afterwards.

@scottmckendry scottmckendry removed the waiting for op This issue is waiting for a response from the original poster label Jun 16, 2024
@scottmckendry
Copy link
Owner

Can you please share your lualine config? I haven’t been able to replicate this behaviour (yet 🙂)

@strayer
Copy link
Author

strayer commented Jun 16, 2024

Will do! Might need a few days though, busy calendar ahead 🚢

@scottmckendry scottmckendry added the waiting for op This issue is waiting for a response from the original poster label Jun 17, 2024
@scottmckendry scottmckendry added the autoclosed No activity for more than 7 days with the "waiting for op" label. label Jun 24, 2024
@scottmckendry scottmckendry closed this as not planned Won't fix, can't repro, duplicate, stale Jun 24, 2024
@strayer
Copy link
Author

strayer commented Jul 12, 2024

I finally had time to look into this (sorry for the silence!). Turns out this was not an issue of cyberdream but default lualine behaviour. I just run the lualine setup function again after changing themes, this then resets the auto lualine theme and shows correct colors.

@scottmckendry scottmckendry removed the waiting for op This issue is waiting for a response from the original poster label Jul 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
autoclosed No activity for more than 7 days with the "waiting for op" label. question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants