Skip to content

πŸ”†πŸŒ‘ Apply custom Neovim configs based on system theme

License

Notifications You must be signed in to change notification settings

sheharyarn/werewolf.nvim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

19 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Werewolf.nvim

Neovim Lua LICENSE

Apply custom Neovim configs based on system theme πŸ”† πŸŒ‘


Werewolf is a handy neovim plugin that automatically changes the theme, applies configs or runs other custom code depending on the system theme or the time of day. It actively listens for changes, so it can do this at runtime when the system theme changes from light to dark or vice versa, without needing to restart Neovim.


(Term: Alacritty β€’ Themes: Material & Solarized β€’ Config: Dotfiles)

Installation

Packer:

use 'sheharyarn/werewolf.nvim'

Lazy:

{ 'sheharyarn/werewolf.nvim', lazy = false }

Configuration

Default Options

By default, werewolf does not apply any custom code/theme on system theme change or other events. Any options passed to setup() will be merged with these default options before being applied.

{
  system_theme = {
    get = require('werewolf.utils').get_theme,
    on_change = nil,
    run_on_start = true,
    period = 500,
  },
}

Apply Neovim theme on system theme change

In your init.lua, add the following:

-- Example assumes material.nvim plugin is installed

require('werewolf').setup({
  system_theme = {
    on_change = function(theme)
      -- Apply custom config based on new `theme` value

      if theme == 'Dark' then
        vim.g.material_style = 'deep ocean'
        vim.o.background = 'dark'
        vim.cmd('colorscheme material')
      else
        vim.g.material_style = 'lighter'
        vim.o.background = 'light'
        vim.cmd('colorscheme material')
      end

    end,

    -- Change the check interval (optional)
    period = 200,
  },
})

If you have a more complex theme/styling configuration or want to run additional code on system theme change events, it's better to move that code out into separate functions and call them inside on_change:

if theme == 'Dark' then
  MyUtils.dark_theme()
else
  MyUtils.light_theme()
end

Also see this example from my dotfiles.


Contributing

  • Fork, Enhance, Send PR
  • Lock issues with any bugs or feature requests
  • Implement something from Roadmap
  • Spread the word ❀️

License

This package is available as open source under the terms of the MIT License.

The Werewolf logo/image has been generated using OpenAI's DALL-E and does not have any copyrights, effectively being CC0.