robin/lylla.nvim
a tiny, customizable statusline for neovim
git clone https://git.robinwobin.dev/robin/lylla.nvim
c7b169c
main
1---@type vim.Log? 2local logger =vim . log . new 3and vim . log . new ( { 4name ="lylla" , 5current_level =vim . log . levels . DEBUG , 6} ) 7or nil 8 9local lylla ={} 10 11local lzrq =function ( modname ) 12return vim . defaulttable ( function ( k ) 13return require ( modname )[ k ] 14end ) 15end 16 17local config =lzrq ( "lylla.config" ) 18local statusline =lzrq ( "lylla.statusline" ) 19 20-- setup ====================================================================== 21 22---@param cfg lylla.config 23local function getevents ( cfg ) 24local modules =cfg . modules 25 26local t =vim . iter ( ipairs ( modules )): fold ( {} , function ( acc , _ , module ) 27if 28type ( module ) == "table" 29and module . fn 30and type ( module . fn ) == "function" 31then 32if module . opts and module . opts . events then 33return vim . iter ( module . opts . events ): fold ( acc , function ( a , event ) 34a [ event ] =true 35return a 36end ) 37end 38end 39return acc 40end ) 41 42return vim . tbl_keys ( t ) 43end 44 45lylla . timer =nil 46lylla . group =nil 47 48---@param cfg? lylla.config 49function lylla . setup ( cfg ) 50---@diagnostic disable-next-line: assign-type-mismatch 51cfg =cfg or {} 52config . set ( config . override ( cfg )) 53 54lylla . group =vim . api . nvim_create_augroup ( "@lylla.refresh" , { clear =true } ) 55local events =getevents ( config . get ()) 56 57for i =1 , # events do 58local event =events [ i ] 59local eventname , eventpattern =unpack ( vim . split ( event , " " ), 1 , 2 ) 60---@cast eventname vim.api.keyset.events? 61if eventname then 62vim . api . nvim_create_autocmd ( eventname , { 63group =lylla . group , 64pattern =eventpattern , 65callback =function ( ev ) 66lylla . refresh ( ev ) 67end , 68} ) 69end 70end 71end 72 73lylla . _init =false 74 75function lylla . init () 76if lylla . _init then 77vim . notify ( "lylla has already been initialized" , vim . log . levels . ERROR ) 78return 79end 80 81local err , err_kind 82---@diagnostic disable-next-line: assign-type-mismatch 83lylla . timer , err , err_kind =vim . uv . new_timer () 84if not lylla . timer or err then 85vim . notify ( string . format ( "%s\n\t%s" , err_kind , err ), vim . log . levels . ERROR ) 86return 87end 88 89-- TODO: only refresh visible windows 90local refresh =config . get (). refresh_rate --[[@as integer|false]] 91if refresh then 92lylla . timer : start ( 0 , refresh , function () 93lylla . refresh () 94end ) 95end 96 97lylla . _init =true 98end 99 100---@param self lylla.proto 101---@param fn fun(self: lylla.proto, ev?: vim.api.keyset.create_autocmd.callback_args|true) 102---@param ev? vim.api.keyset.create_autocmd.callback_args|true 103local function refreshcomponent ( self , fn , ev ) 104do 105local ok , result =pcall ( fn , self , ev ) 106if not ok then 107if logger then 108logger . error ( "error occured on refresh:\n\t" .. result ) 109end 110end 111end 112end 113 114---@param ev? vim.api.keyset.create_autocmd.callback_args|true 115function lylla . refresh ( ev ) 116if not lylla . _init then 117return 118end 119 120vim . schedule ( function () 121local wins =statusline . wins 122for win , st in pairs ( wins ) do 123if not vim . api . nvim_win_is_valid ( win ) then 124st : close () 125else 126refreshcomponent ( st , statusline . set , ev ) 127refreshcomponent ( st , statusline . setwinbar , ev ) 128end 129end 130end ) 131end 132 133-- helpers ==================================================================== 134 135---@param fn fun(): string|any[] 136---@param opts? { events: string[] } 137---@return table 138function lylla . component ( fn , opts ) 139return { fn =fn , opts =opts } 140end 141 142return lylla