goolord / alpha-nvim

a lua powered greeter like vim-startify / dashboard-nvim
MIT License
1.84k stars 109 forks source link

Allow layout sections to be functions #105

Open bennypowers opened 2 years ago

bennypowers commented 2 years ago

Alternative / additional to #34

With the current API, layouts are computed up-front in the setup function, whether or not the user is actually requesting a render. For example, via something like #104, a user would disable autostart until their conditions are met (e.g. "when there is no existing session, or when all windows are closed")

In that case, if the user's alpha setup function is expensive, it will still affect nvim startup time, as the layouts will all be computed up-front. To solve this, layout sections should be allowed to be functions:

local function header()
  -- do some expensive / slow stuff here
end

-- relatively cheap to compute
local buttons = {
  type = "group",
  val = {
    { type = "text", val = "Quick links", opts = { hl = "SpecialComment", position = "center" } },
    { type = "padding", val = 1 },
    dashboard.button("spc /",  "  File Explorer",   ":NeoTreeFloatToggle<CR>"),
    dashboard.button("spc p",  "  Find file",       ":Telescope find_files <CR>"),
    dashboard.button("spc fg", "  Find text",       ":Telescope live_grep <CR>"),
    dashboard.button("n",      "  New file",        ":ene <BAR> startinsert <CR>"),
    dashboard.button("c",      "  Configuration",   ":e ~/.config/nvim/init.lua <CR>"),
    dashboard.button("u",      "  Update plugins" , ":PackerSync<CR>"),
    dashboard.button("G",      "  Git",             "gG"),
    dashboard.button("q",      "  Quit" ,           ":qa<CR>"),
  },
  position = "center",
}

alpha.setup {
    layout = {
        { type = "padding", val = 2 },
        header,
        { type = "padding", val = 2 },
        section_mru,
        { type = "padding", val = 2 },
        buttons,
    },
    opts = {
        margin = 5,
        autostart = false,
    },
}
goolord commented 1 year ago

most layout types alreadyaccept a function in val which gets resolved on each draw (you can just mutate a variable in your config and check it in the function or something)