robin/lylla.nvim

a tiny, customizable statusline for neovim

git clone https://git.robinwobin.dev/robin/lylla.nvim

robinrefactor: move timer to global scope56cb261

main
2.4 KiB102 linesraw
1if vim.g.loaded_lylla then
2  return
3end
4
5vim.g.loaded_lylla = true
6
7local lzrq = function(modname)
8  return vim.defaulttable(function(k)
9    return require(modname)[k]
10  end)
11end
12
13local config = lzrq("lylla.config")
14local utils = lzrq("lylla.utils")
15
16-- highlights =================================================================
17
18---@type table<'normal'|'visual'|'command'|'insert'|'replace'|'operator', vim.api.keyset.highlight>
19local default_hls = {
20  normal = { link = "@property" },
21  visual = { link = "@constant" },
22  command = { link = "@function" },
23  insert = { link = "@variable" },
24  replace = { link = "@type" },
25  operator = { link = "NonText" },
26}
27
28local function set_hl(name, hl)
29  hl.default = true
30  vim.api.nvim_set_hl(0, name, hl)
31end
32
33local function inithls()
34  vim.iter(pairs(default_hls)):each(function(mode, defaulthl)
35    local hl = config.get().hls[mode]
36    local name, revname = utils.get_modehl_name(mode)
37    if hl then
38      set_hl(name, hl)
39    elseif vim.tbl_isempty(vim.api.nvim_get_hl(0, { name = name })) then
40      set_hl(name, defaulthl)
41    end
42    set_hl(revname, utils.reverse_hl(name))
43  end)
44end
45
46-- init =======================================================================
47
48local group = vim.api.nvim_create_augroup("@lylla", { clear = true })
49
50vim.api.nvim_create_autocmd({ "WinNew", "WinEnter" }, {
51  group = group,
52  callback = function()
53    require("lylla.statusline").try_new()
54  end,
55})
56
57vim.api.nvim_create_autocmd("WinClosed", {
58  group = group,
59  callback = function(ev)
60    ---@cast ev +{match: integer}
61    local stl = require("lylla.statusline").wins[ev.match]
62    if stl then
63      stl:close()
64    end
65  end,
66})
67
68vim.api.nvim_create_autocmd({ "UIEnter", "ColorScheme" }, {
69  group = group,
70  callback = function()
71    inithls()
72
73    require("lylla").refresh(true)
74  end,
75})
76
77local function init()
78  require("lylla").init()
79
80  local wins = vim.api.nvim_list_wins()
81  for _, win in ipairs(wins) do
82    if vim.api.nvim_win_is_valid(win) then
83      require("lylla.statusline").try_new(win)
84    end
85  end
86
87  if config.get().tabline ~= vim.NIL then
88    ---@diagnostic disable-next-line: undefined-field
89    require("lylla.tabline").init()
90  end
91end
92
93if vim.v.vim_did_enter > 0 then
94  init()
95else
96  vim.api.nvim_create_autocmd("VimEnter", {
97    group = vim.api.nvim_create_augroup("@lylla.init", { clear = true }),
98    callback = function()
99      init()
100    end,
101  })
102end