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

feature: Switch between dark and light variants #369

Open
ifab opened this issue Sep 5, 2023 · 9 comments
Open

feature: Switch between dark and light variants #369

ifab opened this issue Sep 5, 2023 · 9 comments
Labels
enhancement New feature or request

Comments

@ifab
Copy link

ifab commented Sep 5, 2023

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

Many popular Neovim themes automatically choose between a dark and a light variant, depending on whether vim.o.background is set to "dark" or "light". This is very useful if one frequently switches between a dark and a light theme in the terminal. Currently, Nightfox seems to be missing this feature.

Describe the solution you'd like

The solution implemented in Catppuccin, for example, is great. It provides the following configuration option:

background = { -- :h background
    light = "latte",
    dark = "mocha",
},

Other themes such as TokyoNight and Kanagawa have similar options.

Describe alternatives you've considered

I currently use the following workaround, which is far from ideal:

vim.api.nvim_create_autocmd({ "VimEnter" }, {
  callback = function()
    vim.cmd("sleep 1m")
    if vim.o.background == "dark" then
      vim.cmd("colorscheme nightfox")
    else
      vim.cmd("colorscheme dayfox")
    end
  end,
})

Additional context

No response

@ifab ifab added the enhancement New feature or request label Sep 5, 2023
@EdenEast
Copy link
Owner

EdenEast commented Sep 6, 2023

Hi there,

Firstly, thank you for reaching out with your suggestions for Nightfox!

However, I want to clarify a few things about Nightfox's current structure. As it stands, Nightfox differs from the other styles you've mentioned in a fundamental way. The main issue is that Nightfox doesn't have a concept of a style or variant. In contrast, the other color schemes you referred to can change their primary theme based on a configuration setting. For instance, invoking catppuccin might yield either a mocha or latte theme, depending on a config option.

Nightfox, on the other hand, has distinct, standalone color schemes. When you call colorscheme nightfox, it will consistently apply the Nightfox theme. I are hesitant to introduce a configuration option that could make the call to Nightfox ambiguous regarding the actual color scheme applied.

@ifab
Copy link
Author

ifab commented Sep 6, 2023

Thanks for your quick response! I understand that you do not want to introduce any ambiguity into the interface.

So maybe it is better if I ask the following question: What is the recommended way to automatically switch between two colorschemes, say Nightfox and Dayfox, depending on the value of vim.o.background?

I'm not too happy with my current workaround solution (mentioned above) because:

  • It only works on startup, i.e., not when I suspend Neovim, change the terminal theme, and resume Neovim. Of course I could add more code (e.g., an autocommand for when the background option is set), but this starts to get cumbersome and does not seem to work as well as the built-in solution provided by the other themes I mentioned.
  • It hardcodes the colorschemes in a block of code that I have to comment out whenever I switch to a theme that provides a built-in solution.

@EdenEast
Copy link
Owner

EdenEast commented Sep 6, 2023

When you use the command colorscheme vim checks the colors folder in your runtimepath (example). Using this you could create your own file and call that instead of the ones shipped with nightfox.

-- ~/.config/nvim/colors/something.lua

local style = vim.o.background == "light" and "dayfox" or "nightfox"
require("nightfox.config").set_fox(name)
require("nightfox").load()

Then you can call colorscheme something and that file will be applied.

Just an idea on how I would handle this use case.

@ifab
Copy link
Author

ifab commented Sep 6, 2023

Thanks for the suggestion! Unfortunately this does not seem to work. The selected colorscheme is always Nightfox, regardless of the terminal background color. I think the reason is that Neovim sets vim.o.background too late (see this issue). This is why the workaround mentioned above is inside a VimEnter handler and also adds an additional delay.

I guess I will keep using the workaround for the time being. But just out of curiosity, how do you make use of your different colorschemes? Do you just switch them manually, or perhaps always use the same?

@EdenEast
Copy link
Owner

EdenEast commented Sep 7, 2023

I have what some would say an overly complicated setup for handling themes. I create an autocmd at startup on the event ColorScheme that registers a hook. That hook will write the name of the colorscheme getting appiled to a text file. I then load my plugins, and after apply my theme based on that text file.

As for syncing my theme with my terminal I have a shell script that I use to write to all the appropriate places. I use wezterm as my terminal so when I make the change to the wezterm config it will hot reload the change. That script also writes to that neovim theme cache file. This means that if I am on my laptop outside and I need a light theme I call tcc dayfox and everything is now light theme.

You can find my dotfiles in the repo nyx (another way to say nix). My config is built around nix but you can find my config files for nvim here.

Hope you find some of these ideas interesting.

@ifab
Copy link
Author

ifab commented Sep 7, 2023

Thank you very much for the detailed reply, I really appreciate the time you took to answer my questions! Indeed your setup seems a bit complicated, but now I understand why you have no personal need for the feature described above.

Anyway, just in case you want to provide this feature in nightfox.nvim some day, here is a suggestion for the user interface that avoids the ambiguity you mentioned. It's basically just a variation of your own idea:

  • Add a new color scheme to the collection, e.g., “Metafox”.
  • Add a configuration option that applies only to this new color scheme, e.g:
metafox = {
  light = "dayfox",
  dark = "nightfox",
},

This way you keep the behavior of colorscheme nightfox unambiguous, since the user would have to explicitly call colorscheme metafox to benefit from the feature.

@abmantis
Copy link

abmantis commented Feb 1, 2024

I have a script with gsettings --monitor to react on changes to the global dark/light mode, and that toggles nvim color by using the background setting: https://github.com/abmantis/hypr.dotfiles/blob/main/scripts/color_scheme_handler.sh#L4

It would be great if this theme would support that, as it makes that a generic solution so I can manage themes inside nvim config, without having to change that script too

@angelidis
Copy link

the metafox idea from ifab is really good.

i think a lot of us are used from websites/phones where you can switch between dark and light themes easily depending on time of the day or surrounding lighting conditions.

please consider adding this!

regardless, the whole nightfox collection is amazing -- thank you for all your work!

@vihu
Copy link
Contributor

vihu commented Jul 27, 2024

Another different idea, zsh/bash functions:

Example using zsh to change nvim + wezterm colors:

# set nightfox
nightfox() {
    if [[ "$OSTYPE" == "darwin"* ]]; then
        gsed -i 's/color_scheme = .*/color_scheme = \"nightfox",/g' $HOME/.config/wezterm/wezterm.lua
        gsed -i "s/colorscheme .*/colorscheme nightfox\'/g" $HOME/.config/nvim/lua/colorschemes/nightfox.lua
    else
        sed -i 's/color_scheme = .*/color_scheme = \"nightfox",/g' $HOME/.config/wezterm/wezterm.lua
        sed -i "s/colorscheme .*/colorscheme nightfox\'/g" $HOME/.config/nvim/lua/colorschemes/nightfox.lua
    fi
}

# set dawnfox
dawnfox() {
    if [[ "$OSTYPE" == "darwin"* ]]; then
        gsed -i 's/color_scheme = .*/color_scheme = \"dawnfox",/g' $HOME/.config/wezterm/wezterm.lua
        gsed -i "s/colorscheme .*/colorscheme dawnfox\'/g" $HOME/.config/nvim/lua/colorschemes/nightfox.lua
    else
        sed -i 's/color_scheme = .*/color_scheme = \"dawnfox",/g' $HOME/.config/wezterm/wezterm.lua
        sed -i "s/colorscheme .*/colorscheme dawnfox\'/g" $HOME/.config/nvim/lua/colorschemes/nightfox.lua
    fi
}

With this you can just do nightfox or dayfox on your terminal and it will switch the colorscheme.

Note: Change the path to the colorschemes accordingly.
Caveat: you might have to reopen nvim manually for the change to take effect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants