robin/lylla.nvim
a tiny, customizable statusline for neovim
git clone https://git.robinwobin.dev/robin/lylla.nvim
d07bc2d
main
1local utils ={} 2 3--- flatten list so all children have level of depth 4---@param lst table 5---@param maxdepth integer 6function utils . flatten ( lst , maxdepth ) 7vim . validate ( "lst" , lst , "table" ) 8vim . validate ( "maxdepth" , maxdepth , "number" ) 9 10---@param _t any[] 11---@return integer 12local function _depth ( _t ) 13return vim . iter ( ipairs ( _t )): fold ( 1 , function ( maxd , _ , v ) 14if type ( v ) == "table" and vim . islist ( v ) then 15local d =1 + _depth ( v ) 16if d > maxd then 17return d 18end 19end 20return maxd 21end ) 22end 23 24local result ={} 25---@param _t any[] 26local function _flatten ( _t ) 27local n =# _t 28for i =1 , n do 29local v =_t [ i ] 30if type ( v ) ~= "table" or ( not vim . islist ( v )) or _depth ( v ) <= maxdepth then 31table . insert ( result , v ) 32else 33_flatten ( v ) 34end 35end 36end 37_flatten ( lst ) 38return result 39end 40 41local sfmt ="%%*%s" 42local sfmt_inherit ="%s" 43-- str, hl_name, text 44local hlfmt ="%%#%s#%s" 45local hlfmt_inherit ="%%$%s$%s%%#%s#" 46 47---@param text string 48---@param inherit string|false 49---@param hl? string 50---@return string 51local function strfmt ( text , inherit , hl ) 52if hl then 53return string . format ( 54inherit and hlfmt_inherit or hlfmt , 55hl , 56text , 57inherit or nil 58) 59end 60return string . format ( inherit and sfmt_inherit or sfmt , text ) 61end 62 63---@param lst any[] 64---@return string 65function utils . fold ( lst ) 66vim . validate ( "lst" , lst , "table" ) 67 68lst =utils . flatten ( lst , 1 ) 69---@type string|false 70local section =false 71local l =vim 72. iter ( ipairs ( lst )) 73: map ( function ( _ , module ) 74if type ( module ) == "string" and # module > 0 then 75return strfmt ( module , section ) 76end 77if type ( module ) ~= "table" then 78return 79end 80if 81module . section ~= nil 82and ( type ( module . section ) == "string" or module . section == false ) 83then 84section =module . section 85if section then 86return string . format ( "%%#%s#" , module . section ) 87end 88return 89end 90local text =module [ 1 ] 91if text == nil or type ( text ) ~= "string" or # text == 0 then 92return 93end 94local hl =module [ 2 ] 95if not hl then 96return strfmt ( text , section ) 97end 98if type ( hl ) == "string" and # hl > 0 then 99return strfmt ( text , section , hl ) 100elseif type ( hl ) == "table" and ( hl . fg or hl . bg or hl . link ) then 101local inherit =section or hl . inherit 102hl . inherit =nil 103local hl_name =hl . link or utils . create_hl ( hl ) 104return strfmt ( text , inherit , hl_name ) 105elseif type ( hl ) == "table" and hl . inherit then 106return strfmt ( text , hl . inherit ) 107end 108 109return strfmt ( text , section ) 110end ) 111: totable () 112 113return table . concat ( l ) 114end 115 116---@param hl string|vim.api.keyset.highlight|vim.api.keyset.get_hl_info name or keyset 117---@return vim.api.keyset.highlight 118function utils . reverse_hl ( hl ) 119if type ( hl ) == "string" then 120---@diagnostic disable-next-line: cast-local-type 121hl =vim . api . nvim_get_hl ( 0 , { name =hl , link =false } ) 122end 123 124if vim . tbl_isempty ( hl ) or ( not hl . fg and not hl . bg and not hl . link ) then 125return {} 126end 127if hl . link then 128return utils . reverse_hl ( hl . link --[[@as string]] ) 129end 130local rev =vim . deepcopy ( hl ) 131rev . fg =hl . bg 132rev . bg =hl . fg 133---@diagnostic disable-next-line: return-type-mismatch 134return rev 135end 136 137---@param hl vim.api.keyset.highlight 138function utils . create_hl ( hl ) 139local hl_name =string . format ( "@lylla.%s" , vim . fn . sha256 ( vim . inspect ( hl ))) 140vim . schedule ( function () 141vim . api . nvim_set_hl ( 0 , hl_name , hl ) 142end ) 143return hl_name 144end 145 146function utils . getfilename () 147local _ , default_file_hl =require ( "mini.icons" ). get ( "default" , "file" ) 148 149local bufname =vim . api . nvim_buf_get_name ( 0 ) 150local name =vim . fs . basename ( bufname ) 151 152local file_icon_raw , file_icon_hl 153 154local buftype =vim . bo . buftype 155if buftype == "" or buftype == "nowrite" then 156local stat =vim . uv . fs_stat ( bufname ) 157local type_ =stat and stat . type == "directory" and "directory" or "file" 158file_icon_raw , file_icon_hl =require ( "mini.icons" ). get ( type_ , name ) 159elseif buftype == "nofile" or buftype == "acwrite" then 160local filetype =vim . bo . filetype 161file_icon_raw , file_icon_hl = 162require ( "mini.icons" ). get ( "filetype" , filetype ) 163name =string . format ( "(%s)" , filetype ) 164else 165name =string . format ( "(%s)" , buftype ) 166return { { name , default_file_hl } } 167end 168 169return { { name , default_file_hl } , { " " } , { file_icon_raw , file_icon_hl } } 170end 171 172function utils . getfilepath () 173if vim . bo . buftype ~= "" and vim . bo . buftype ~= "nowrite" then 174return {} 175end 176 177local bufname =vim . api . nvim_buf_get_name ( 0 ) 178local path =vim . fn . fnamemodify ( bufname , ":p:~:." ) 179 180local file_path_list ={} 181local _ =string . gsub ( path , "[^/]+" , function ( w ) 182table . insert ( file_path_list , w ) 183end ) 184 185local filepath =vim 186. iter ( ipairs ( file_path_list )) 187: fold ( "" , function ( acc , i , fragment ) 188if i == # file_path_list then 189return acc 190end 191acc =acc .. fragment .. "/" 192return acc 193end ) 194 195return { filepath , "Directory" } 196end 197 198function utils . get_searchcount () 199local result =vim . fn . searchcount ( { recompute =1 } ) 200if vim . v . hlsearch ~= 1 then 201return "" 202end 203if vim . tbl_isempty ( result ) then 204return "" 205end 206local term =vim . fn . getreg ( "/" ) 207local display 208if result . incomplete == 1 then 209-- timed out 210display ="[?/??]" 211elseif result . incomplete == 2 then 212-- max count exceeded 213if result . total > result . maxcount and result . current > result . maxcount then 214display =string . format ( "[>%d/>%d]" , result . current , result . total ) 215elseif result . total > result . maxcount then 216display =string . format ( "[%d/>%d]" , result . current , result . total ) 217end 218end 219display =display or string . format ( "[%d/%d]" , result . current , result . total ) 220 221return { 222{ string . format ( "/%s" , term ), "IncSearch" } , 223{ " " } , 224{ display , "MsgSeparator" } , 225} 226end 227 228---@param mode string 229---@return string 230---@return string 231function utils . get_modehl_name ( mode ) 232return "@lylla." .. mode , string . format ( "@lylla.%s.rev" , mode ) 233end 234 235---@return string 236---@return string 237function utils . get_modehl () 238local mode =vim . api . nvim_get_mode (). mode 239local modename ="normal" 240 241if string . match ( mode , "^[vVs]" ) then 242modename ="visual" 243elseif string . match ( mode , "^c" ) then 244modename ="command" 245elseif string . match ( mode , "^[it]" ) then 246modename ="insert" 247elseif string . match ( mode , "^[rR]" ) then 248modename ="replace" 249elseif string . match ( mode , "^%ao" ) then 250modename ="operator" 251end 252 253return utils . get_modehl_name ( modename ) 254end 255 256return utils