馃 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 hl = config.get().hls[mode]
36 local name, revname = utils.get_modehl_name(mode)
37 if hl then
38 vim.api.nvim_set_hl(0, name, hl)
39 elseif vim.tbl_isempty(vim.api.nvim_get_hl(0, { name = name })) then
40 vim.api.nvim_set_hl(0, name, defaulthl)
41 end
42 vim.api.nvim_set_hl(0, revname, utils.reverse_hl(name))
43 end)
44end
45
46function M.resethl()
47 vim.iter(pairs(default_hls)):each(function(mode, _)
48 local name, revname = utils.get_modehl_name(mode)
49 vim.api.nvim_set_hl(0, name, {})
50 vim.api.nvim_set_hl(0, revname, {})
51 end)
52end
53
54M.initialized = false
55
56function M.init()
57 if M.initialized then
58 return
59 end
60
61 M.initialized = true
62
63 vim.api.nvim_create_autocmd({ "UIEnter", "WinNew", "WinEnter" }, {
64 group = vim.api.nvim_create_augroup("lylla:win", { clear = true }),
65 callback = function()
66 local win = vim.api.nvim_get_current_win()
67 if not require("lylla.statusline").wins[win] then
68 require("lylla.statusline"):new(win):init()
69 end
70 end,
71 })
72
73 vim.api.nvim_create_autocmd("WinClosed", {
74 group = vim.api.nvim_create_augroup("lylla:close", { clear = true }),
75 callback = function(ev)
76 local stl = require("lylla.statusline").wins[ev.match]
77 if stl then
78 stl:close()
79 end
80 end,
81 })
82
83 if config.get().tabline ~= vim.NIL then
84 require("lylla.tabline").setup()
85 end
86
87 vim.api.nvim_create_autocmd("ColorSchemePre", {
88 group = vim.api.nvim_create_augroup("lylla:resethl", { clear = true }),
89 callback = function()
90 M.resethl()
91 end,
92 })
93 vim.api.nvim_create_autocmd("ColorScheme", {
94 group = vim.api.nvim_create_augroup("lylla:inithls", { clear = true }),
95 callback = function()
96 M.inithls()
97 end,
98 })
99 M.inithls()
100end
101
102-- helpers
103
104---@param fn fun(): string|any[]
105---@param opts? { events: string[] }
106---@return table
107function M.component(fn, opts)
108 local t = {}
109 t.fn = fn
110 t.opts = opts
111 return t
112end
113
114return M