馃 a tiny, customizable statusline for neovim
1local M = {}
2
3local lzrq = function(modname)
4 return setmetatable({
5 modname = modname,
6 }, {
7 __index = function(t, k)
8 local m = rawget(t, "modname")
9 return m and require(m)[k] or nil
10 end,
11 })
12end
13
14local config = lzrq("lylla.config")
15local utils = lzrq("lylla.utils")
16
17---@type table<'normal'|'visual'|'command'|'insert'|'replace'|'operator', vim.api.keyset.highlight>
18local default_hls = {
19 normal = { link = "@property" },
20 visual = { link = "@constant" },
21 command = { link = "@function" },
22 insert = { link = "@variable" },
23 replace = { link = "@type" },
24 operator = { link = "NonText" },
25}
26
27---@param cfg? lylla.config
28function M.setup(cfg)
29 cfg = cfg or {}
30 config.set(config.override(cfg))
31end
32
33function M.inithls()
34 vim.iter(pairs(default_hls)):each(function(mode, defaulthl)
35 local name = utils.get_modehl_name(mode)
36
37 local hl = config.get().hls[mode]
38 if hl then
39 vim.api.nvim_set_hl(0, name, hl)
40 return
41 end
42
43 if vim.tbl_isempty(vim.api.nvim_get_hl(0, { name = name })) then
44 vim.api.nvim_set_hl(0, name, defaulthl)
45 end
46 end)
47end
48
49function M.resethl()
50 vim.iter(pairs(default_hls)):each(function(mode, _)
51 local name = utils.get_modehl_name(mode)
52 vim.api.nvim_set_hl(0, name, {})
53 end)
54end
55
56M.initialized = false
57
58function M.init()
59 if M.initialized then
60 return
61 end
62
63 M.initialized = true
64
65 vim.api.nvim_create_autocmd({ "UIEnter", "WinNew", "WinEnter" }, {
66 group = vim.api.nvim_create_augroup("lylla:win", { clear = true }),
67 callback = function()
68 local win = vim.api.nvim_get_current_win()
69 if not require("lylla.statusline").wins[win] then
70 require("lylla.statusline"):new(win):init()
71 end
72 end,
73 })
74
75 vim.api.nvim_create_autocmd("WinClosed", {
76 group = vim.api.nvim_create_augroup("lylla:close", { clear = true }),
77 callback = function(ev)
78 local stl = require("lylla.statusline").wins[ev.match]
79 if stl then
80 stl:close()
81 end
82 end,
83 })
84
85 if config.get().tabline ~= vim.NIL then
86 require("lylla.tabline").setup()
87 end
88
89 vim.api.nvim_create_autocmd("ColorSchemePre", {
90 group = vim.api.nvim_create_augroup("lylla:resethl", { clear = true }),
91 callback = function()
92 M.resethl()
93 end,
94 })
95 vim.api.nvim_create_autocmd("ColorScheme", {
96 group = vim.api.nvim_create_augroup("lylla:inithls", { clear = true }),
97 callback = function()
98 M.inithls()
99 end,
100 })
101 M.inithls()
102end
103
104-- helpers
105
106---@param fn fun(): string|any[]
107---@param opts? { events: string[] }
108---@return table
109function M.component(fn, opts)
110 local t = {}
111 t.fn = fn
112 t.opts = opts
113 return t
114end
115
116return M