🪴 a tiny, customizable statusline for neovim

fix: register events dynamically

authored by

robin and committed by robinwobin.dev a2251dd0 75f9916d

+31 -34
-13
README.md
··· 46 46 ```lua 47 47 require("lylla").setup({ 48 48 refresh_rate = 300, 49 - events = { 50 - "WinEnter", 51 - "BufEnter", 52 - "BufWritePost", 53 - "SessionLoadPost", 54 - "FileChangedShellPost", 55 - "VimResized", 56 - "Filetype", 57 - "CursorMoved", 58 - "CursorMovedI", 59 - "ModeChanged", 60 - "CmdlineEnter", 61 - }, 62 49 hls = {}, 63 50 modules = { 64 51 "%<%f %h%w%m%r",
-14
lua/lylla/config.lua
··· 8 8 9 9 ---@class lylla.config 10 10 ---@field refresh_rate integer 11 - ---@field events string[] 12 11 ---@field hls table<'normal'|'visual'|'command'|'insert', vim.api.keyset.highlight> 13 12 ---@field modules (lylla.item|lylla.item.tuple|string)[] 14 13 ---@field winbar any[] ··· 20 19 ---@text # Default ~ 21 20 M.default = { 22 21 refresh_rate = 300, 23 - events = { 24 - "WinEnter", 25 - "BufEnter", 26 - "BufWritePost", 27 - "SessionLoadPost", 28 - "FileChangedShellPost", 29 - "VimResized", 30 - "Filetype", 31 - "CursorMoved", 32 - "CursorMovedI", 33 - "ModeChanged", 34 - "CmdlineEnter", 35 - }, 36 22 hls = {}, 37 23 modules = { 38 24 "%<%f %h%w%m%r",
+31 -7
lua/lylla/statusline.lua
··· 43 43 self:refresh() 44 44 end) 45 45 46 - self.refreshau = vim.api.nvim_create_autocmd(require("lylla.config").get().events, { 47 - group = vim.api.nvim_create_augroup("lylla:refresh", { clear = false }), 48 - callback = function(ev) 49 - self:refresh(ev) 50 - end, 51 - }) 46 + self.refreshau = vim.api.nvim_create_augroup(("lylla:refresh[%d]"):format(self.win), { clear = true }) 47 + 48 + local events = self:getevents() 49 + 50 + for i = 1, #events do 51 + vim.api.nvim_create_autocmd(events[i], { 52 + group = self.refreshau, 53 + callback = function(ev) 54 + self:refresh(ev) 55 + end, 56 + }) 57 + end 52 58 end 53 59 54 60 ---@class lylla.proto ··· 56 62 function statusline:close() 57 63 self.timer:stop() 58 64 self.timer:close() 59 - vim.api.nvim_del_autocmd(self.refreshau) 65 + pcall(vim.api.nvim_del_augroup_by_id, self.refreshau) 60 66 statusline.wins[self.win] = nil 67 + end 68 + 69 + ---@class lylla.proto 70 + ---@field getevents fun(self): string[] 71 + function statusline:getevents() 72 + local t = vim.iter(ipairs(self.modules)):fold({}, function(acc, _, module) 73 + if type(module) == "table" and module.fn and type(module.fn) == "function" then 74 + if module.opts and module.opts.events then 75 + return vim.iter(module.opts.events):fold(acc, function(a, event) 76 + a[event] = true 77 + return a 78 + end) 79 + end 80 + end 81 + return acc 82 + end) 83 + 84 + return vim.tbl_keys(t) 61 85 end 62 86 63 87 local function refreshcomponent(self, fn, ev)