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

Config with lua functions #244

Open
EdenEast opened this issue Nov 17, 2022 · 3 comments
Open

Config with lua functions #244

EdenEast opened this issue Nov 17, 2022 · 3 comments
Labels
enhancement New feature or request

Comments

@EdenEast
Copy link
Owner

EdenEast commented Nov 17, 2022

Now that pr #241 has landed I would like to change now nightfox is configured. Instead of having a lua table that you fill in and extend with nightfox I would like to expose this as a lua function instead. Here is a mock up of what a config would look like.

require("nightfox").setup({
  options = {},
  on_palette = function(palette, name, color)
    if name == "nightfox" then
      local red = color.new("#ff0000")
      palette.red.base = red:to_css()
      palette.red.bright = red:brighten(10):to_css()
      palette.red.dim = red:lighten(-10):to_css()
    elseif name == "dayfox" then
      local shade = require("nightfox.lib.shade")
      palette.blue = shade.new("#0000ff", 0.15, -0, 15)
    elseif name == "terafox" then
      palette.green.base = "#00ff00"
    end
  end,
  on_spec = function(spec, name, color)
    local p = spec.palette
    -- Note: Only overrding a single value as setting syntax to a new table would remove other values
    spec.syntax.operator = p.orange.base
    spec.syntax.builtin0 = p.pink.bright
    spec.syntax.builtin1 = p.cyan:harsh() -- bright for dark themes and dim for light
    spec.syntax.builtin2 = p.yellow:subtle() -- dim for dark themes and bright for light
  end,
  on_highlight = function(spec, hl, name, color)
    local p = spec.palette
    hl.TelescopeBorder = { fg = spec.bg4 }
    hl.TelescopeTitle = { fg = spec.fg2, bg = spec.bg4 }

    hl.CmpItemKindFunction = { fg = p.magenta.base }
    hl.CmpItemKindMethod = { fg = p.magenta.base }
    hl.CmpWindowBorder = { fg = spec.bg0, bg = spec.bg0 }

    hl["@attribute"] = { link = "Constant" }
  end,
})

In the example above, name would be the name of the style, and color would be nightfox's Color manipulation library.

User's configuration is now hashed and cached these functions would only be executed when there is a change to nightfox or the file that the nightfox setup function is called.

@EdenEast EdenEast added the enhancement New feature or request label Nov 17, 2022
@nullchilly
Copy link
Collaborator

I am not sure I like the hl. spec. prefixes

hl = {
    TelescopeBorder = { fg = spec.bg4 }
    TelescopeTitle = { fg = spec.fg2, bg = spec.bg4 }

    CmpItemKindFunction = { fg = p.magenta.base }
    CmpItemKindMethod = { fg = p.magenta.base }
    CmpWindowBorder = { fg = spec.bg0, bg = spec.bg0 }
}

Seems better to me as an user

@EdenEast
Copy link
Owner Author

The idea would be that the function would be passed the actual lua tables that nightfox uses. You can then do what you want and reference what you want with that lua table.

One of the issues with that is that users can override the entire highlight group table for example. Since this is also just a lua function the user can just deep extend a local table that they create.

on_highlight = function(spec, hl, name, color)
  local p = spec.palette

  local override = {
    TelescopeBorder = { fg = spec.bg4 },
    TelescopeTitle = { fg = spec.fg2, bg = spec.bg4 },

    CmpItemKindFunction = { fg = p.magenta.base },
    CmpItemKindMethod = { fg = p.magenta.base },
    CmpWindowBorder = { fg = spec.bg0, bg = spec.bg0 },
  }

  vim.tbl_deep_extend("force", hl, override)
end

Maybe instead of modding the actual table the function would expect to return a table that would would then be extended in nightfox.

@nullchilly
Copy link
Collaborator

nullchilly commented Nov 17, 2022

Keep in mind that there isn't a vim.tbl_deep_extend inside vim's lua

As a result I added this https://github.com/catppuccin/nvim/blob/main/lua/catppuccin/lib/vim/init.lua

Maybe instead of modding the actual table the function would expect to return a table that would would then be extended in nightfox.

Yes, that's what I do in catppuccin

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

2 participants