Something went wrong. Try again.
🪴 a tiny, customizable statusline for neovim
Something went wrong. Try again.
3.8 kB · 160 lines
Lua
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161local utils = require("lylla.utils")
---@class (partial) lylla.proto---@field wins table<integer, lylla.proto>---@field win integer---@field modules any[]---@field winbar any[]local statusline = {}
statusline.wins = {}
---@class (partial) lylla.proto---@field new fun(win: integer): lylla.protofunction statusline.new(win) if statusline.wins[win] then statusline.wins[win]:close() end local stl = setmetatable({ win = win, modules = vim.deepcopy(require("lylla.config").get().modules, true), winbar = vim.deepcopy(require("lylla.config").get().winbar, true), }, { __index = statusline }) statusline.wins[win] = stl return stlend
---@class (partial) lylla.proto---@field try_new fun(win: integer?): lylla.protofunction statusline.try_new(win) win = win or vim.api.nvim_get_current_win()
if statusline.wins[win] then return statusline.wins[win] end return statusline.new(win)end
---@class (partial) lylla.proto---@field close fun(self)function statusline:close() statusline.wins[self.win] = nilend
---@param v any---@return string?local function display(v) if type(v) == "table" then return utils.fold(v) end if type(v) == "function" then local ok, result = pcall(v) if not ok then return end
return display(result) end
return tostring(v)end
---@class (partial) lylla.proto---@field fold fun(ev?: vim.api.keyset.create_autocmd.callback_args|true, modules: any[]): stringfunction statusline.fold(ev, modules) if type(modules) ~= "table" or modules == nil then return "" end
local lst = vim .iter(ipairs(modules)) :map(function(_, module) if type(module) ~= "table" then return display(module) end
if not module.fn or type(module.fn) ~= "function" then return display(module) end
if not (type(ev) == "boolean" and ev == true) and module.opts and module.opts.events then -- refresh from timer if not ev and module.prev then return module.prev end -- refresh from non-match event if ev and not vim.tbl_contains(module.opts.events, ev.event) and module.prev then return module.prev end end
module.prev = display(module.fn)
return module.prev end) :totable() return table.concat(lst)end
---@class (partial) lylla.proto---@field get fun(self, ev?: vim.api.keyset.create_autocmd.callback_args): stringfunction statusline:get(ev) return self.fold(ev, self.modules)end
---@class (partial) lylla.proto---@field getwinbar fun(self, ev?: vim.api.keyset.create_autocmd.callback_args): stringfunction statusline:getwinbar(ev) return self.fold(ev, self.winbar)end
---@class (partial) lylla.proto---@field setwinbar fun(self, ev?: vim.api.keyset.create_autocmd.callback_args)function statusline:setwinbar(ev) local buf = vim.api.nvim_win_get_buf(self.win) if vim.bo[buf].buftype ~= "" and vim.bo[buf].buftype ~= "nowrite" then return end
local ok, result = pcall(vim.api.nvim_win_call, self.win, function() return self:getwinbar(ev) end) assert( ok, string.format( "error occured while trying to evaluate winbar:\n\t%s", result ) ) ---@cast result string
vim.wo[self.win].winbar = resultend
---@class (partial) lylla.proto---@field set fun(self, ev?: vim.api.keyset.create_autocmd.callback_args)function statusline:set(ev) local ok, result = pcall(vim.api.nvim_win_call, self.win, function() return self:get(ev) end) assert( ok, string.format( "error occured while trying to evaluate statusline:\n\t%s", result ) ) ---@cast result string
vim.wo[self.win].statusline = resultend
return statusline