🪴 a tiny, customizable statusline for neovim

refactor: move `fold` to utils and allow hl sets

+40 -21
+1 -18
lua/lylla/statusline.lua
··· 149 149 return module 150 150 end) 151 151 :totable() 152 - lst = utils.flatten(lst, 1) 153 - return vim.iter(lst):fold("", function(str, module) 154 - if type(module) == "string" and #module > 0 then 155 - return str .. module 156 - end 157 - if type(module) ~= "table" or #module == 0 then 158 - return str 159 - end 160 - local text = module[1] 161 - if text == nil or type(text) ~= "string" or #text == 0 then 162 - return str 163 - end 164 - local hl = module[2] 165 - if hl and type(hl) == "string" and #hl > 0 then 166 - return str .. "%#" .. hl .. "#" .. text .. "%*" 167 - end 168 - return str .. "%*" .. text 169 - end) 152 + return utils.fold(lst) 170 153 end 171 154 172 155 ---@class lylla.proto
+39 -3
lua/lylla/utils.lua
··· 18 18 ---@param lst table 19 19 ---@param maxdepth integer 20 20 function utils.flatten(lst, maxdepth) 21 + vim.validate("lst", lst, "table") 22 + vim.validate("maxdepth", maxdepth, "number") 23 + 21 24 ---@param _t any[] 22 25 ---@return integer 23 26 local function _depth(_t) 24 - return vim.iter(_t):fold(1, function(maxd, v) 25 - if type(v) == "table" then 27 + return vim.iter(ipairs(_t)):fold(1, function(maxd, _, v) 28 + if type(v) == "table" and vim.islist(v) then 26 29 local d = 1 + _depth(v) 27 30 if d > maxd then 28 31 return d ··· 38 41 local n = #_t 39 42 for i = 1, n do 40 43 local v = _t[i] 41 - if type(v) ~= "table" or _depth(v) <= maxdepth then 44 + if type(v) ~= "table" or (not vim.islist(v)) or _depth(v) <= maxdepth then 42 45 table.insert(result, v) 43 46 else 44 47 _flatten(v) ··· 47 50 end 48 51 _flatten(lst) 49 52 return result 53 + end 54 + 55 + function utils.fold(lst) 56 + vim.validate("lst", lst, "table") 57 + 58 + lst = utils.flatten(lst, 1) 59 + return vim.iter(ipairs(lst)):fold("", function(str, _, module) 60 + if type(module) == "string" and #module > 0 then 61 + return str .. module 62 + end 63 + if type(module) ~= "table" or #module == 0 then 64 + return str 65 + end 66 + local text = module[1] 67 + if text == nil or type(text) ~= "string" or #text == 0 then 68 + return str 69 + end 70 + local hl = module[2] 71 + if not hl then 72 + return string.format("%s%%*%s", str, text) 73 + end 74 + if type(hl) == "string" and #hl > 0 then 75 + return string.format("%s%%#%s#%s%%*", str, hl, text) 76 + elseif type(hl) == "table" and (hl.fg or hl.bg or hl.link) then 77 + local hl_name = string.format("@lylla.%s", vim.fn.sha256(vim.inspect(hl))) 78 + vim.schedule(function() 79 + vim.api.nvim_set_hl(0, hl_name, hl) 80 + end) 81 + return string.format("%s%%#%s#%s%%*", str, hl_name, text) 82 + end 83 + 84 + return string.format("%s%%*%s", str, text) 85 + end) 50 86 end 51 87 52 88 function utils.getfilename()