robin/lylla.nvim

a tiny, customizable statusline for neovim

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

robininit: allow disabling refreshc7b169c

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