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
3.7 KiB160 linesraw
1local utils = require("lylla.utils")
2
3---@class (partial) lylla.proto
4---@field wins table<integer, lylla.proto>
5---@field win integer
6---@field modules any[]
7---@field winbar any[]
8local statusline = {}
9
10statusline.wins = {}
11
12---@class (partial) lylla.proto
13---@field new fun(win: integer): lylla.proto
14function statusline.new(win)
15  if statusline.wins[win] then
16    statusline.wins[win]:close()
17  end
18  local stl = setmetatable({
19    win = win,
20    modules = vim.deepcopy(require("lylla.config").get().modules, true),
21    winbar = vim.deepcopy(require("lylla.config").get().winbar, true),
22  }, { __index = statusline })
23  statusline.wins[win] = stl
24  return stl
25end
26
27---@class (partial) lylla.proto
28---@field try_new fun(win: integer?): lylla.proto
29function statusline.try_new(win)
30  win = win or vim.api.nvim_get_current_win()
31
32  if statusline.wins[win] then
33    return statusline.wins[win]
34  end
35  return statusline.new(win)
36end
37
38---@class (partial) lylla.proto
39---@field close fun(self)
40function statusline:close()
41  statusline.wins[self.win] = nil
42end
43
44---@param v any
45---@return string?
46local function display(v)
47  if type(v) == "table" then
48    return utils.fold(v)
49  end
50  if type(v) == "function" then
51    local ok, result = pcall(v)
52    if not ok then
53      return
54    end
55
56    return display(result)
57  end
58
59  return tostring(v)
60end
61
62---@class (partial) lylla.proto
63---@field fold fun(ev?: vim.api.keyset.create_autocmd.callback_args|true, modules: any[]): string
64function statusline.fold(ev, modules)
65  if type(modules) ~= "table" or modules == nil then
66    return ""
67  end
68
69  local lst = vim
70    .iter(ipairs(modules))
71    :map(function(_, module)
72      if type(module) ~= "table" then
73        return display(module)
74      end
75
76      if not module.fn or type(module.fn) ~= "function" then
77        return display(module)
78      end
79
80      if
81        not (type(ev) == "boolean" and ev == true)
82        and module.opts
83        and module.opts.events
84      then
85        -- refresh from timer
86        if not ev and module.prev then
87          return module.prev
88        end
89        -- refresh from non-match event
90        if
91          ev
92          and not vim.tbl_contains(module.opts.events, ev.event)
93          and module.prev
94        then
95          return module.prev
96        end
97      end
98
99      module.prev = display(module.fn)
100
101      return module.prev
102    end)
103    :totable()
104  return table.concat(lst)
105end
106
107---@class (partial) lylla.proto
108---@field get fun(self, ev?: vim.api.keyset.create_autocmd.callback_args): string
109function statusline:get(ev)
110  return self.fold(ev, self.modules)
111end
112
113---@class (partial) lylla.proto
114---@field getwinbar fun(self, ev?: vim.api.keyset.create_autocmd.callback_args): string
115function statusline:getwinbar(ev)
116  return self.fold(ev, self.winbar)
117end
118
119---@class (partial) lylla.proto
120---@field setwinbar fun(self, ev?: vim.api.keyset.create_autocmd.callback_args)
121function statusline:setwinbar(ev)
122  local buf = vim.api.nvim_win_get_buf(self.win)
123  if vim.bo[buf].buftype ~= "" and vim.bo[buf].buftype ~= "nowrite" then
124    return
125  end
126
127  local ok, result = pcall(vim.api.nvim_win_call, self.win, function()
128    return self:getwinbar(ev)
129  end)
130  assert(
131    ok,
132    string.format(
133      "error occured while trying to evaluate winbar:\n\t%s",
134      result
135    )
136  )
137  ---@cast result string
138
139  vim.wo[self.win].winbar = result
140end
141
142---@class (partial) lylla.proto
143---@field set fun(self, ev?: vim.api.keyset.create_autocmd.callback_args)
144function statusline:set(ev)
145  local ok, result = pcall(vim.api.nvim_win_call, self.win, function()
146    return self:get(ev)
147  end)
148  assert(
149    ok,
150    string.format(
151      "error occured while trying to evaluate statusline:\n\t%s",
152      result
153    )
154  )
155  ---@cast result string
156
157  vim.wo[self.win].statusline = result
158end
159
160return statusline