robin/lylla.nvim

a tiny, customizable statusline for neovim

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

robininit: allow disabling refreshc7b169c

main
3.3 KiB169 linesraw
1---@module 'lylla.config'
2
3---@class lylla.item
4---@field fn fun(): any
5---@field opts? { events?: string[] }
6
7---@alias lylla.item.tuple {[1]: string, [2]?: string}
8
9---@class lylla.config
10---@field refresh_rate integer|boolean
11---@field hls table<'normal'|'visual'|'command'|'insert'|'replace'|'operator', vim.api.keyset.highlight>
12---@field modules (lylla.item|lylla.item.tuple|string)[]
13---@field winbar any[]
14---@field tabline (fun(): (lylla.item|lylla.item.tuple|string)[])|vim.NIL
15
16local M, H = {}, {}
17
18---@type lylla.config
19---@eval return MiniDoc.afterlines_to_code(MiniDoc.current.eval_section)
20---@text # Default ~
21M.default = {
22  refresh_rate = 300,
23  hls = {},
24  modules = {
25    "meow",
26    "%<%f %h%w%m%r",
27    "%=",
28    {
29      fn = function()
30        if vim.o.showcmdloc == "statusline" then
31          return "%-10.S"
32        end
33        return ""
34      end,
35    },
36    { " " },
37    {
38      fn = function()
39        if not vim.b.keymap_name then
40          return ""
41        end
42        return "<" .. vim.b.keymap_name .. ">"
43      end,
44    },
45    { " " },
46    {
47      fn = function()
48        if vim.bo.busy > 0 then
49          return "◐ "
50        end
51        return ""
52      end,
53    },
54    { " " },
55    {
56      fn = function()
57        if not package.loaded["vim.diagnostic"] then
58          return ""
59        end
60        return vim.diagnostic.status()
61      end,
62      opts = {
63        events = { "DiagnosticChanged" },
64      },
65    },
66    { " " },
67    {
68      fn = function()
69        if not vim.o.ruler then
70          return ""
71        end
72        if vim.o.rulerformat == "" then
73          return "%-14.(%l,%c%V%) %P"
74        end
75        return vim.o.rulerformat
76      end,
77    },
78  },
79  winbar = {},
80  tabline = vim.NIL,
81}
82
83---@type lylla.config
84---@diagnostic disable-next-line: missing-fields
85M.config = {}
86
87---@return lylla.config
88function M.get()
89  return H.merge(M.default, M.config)
90end
91
92---@param cfg lylla.config
93---@return lylla.config
94function M.override(cfg)
95  return H.merge(M.default, cfg)
96end
97
98---@param cfg lylla.config
99function M.set(cfg)
100  M.config = cfg
101end
102
103-- helpers ====================================================================
104
105---@param tdefault lylla.config
106---@param toverride lylla.config
107---@return lylla.config
108function H.merge(tdefault, toverride)
109  return H.tmerge(tdefault, toverride)
110end
111
112---@private
113---@generic T: table|any[]
114---@param tdefault T
115---@param toverride T
116---@return T
117H.tmerge = function(tdefault, toverride)
118  if toverride == nil then
119    return tdefault
120  end
121
122  -- do not merge lists
123  if H.islist(tdefault) then
124    return toverride
125  end
126  if vim.tbl_isempty(tdefault) then
127    return toverride
128  end
129
130  return vim.iter(pairs(tdefault)):fold({}, function(tnew, k, v)
131    if toverride[k] == nil then
132      tnew[k] = v
133      return tnew
134    end
135    if type(v) ~= type(toverride[k]) then
136      tnew[k] = toverride[k]
137      return tnew
138    end
139    if type(v) == "table" then
140      tnew[k] = H.tmerge(v, toverride[k])
141      return tnew
142    end
143
144    tnew[k] = toverride[k]
145    return tnew
146  end)
147end
148
149---@param t table
150---@return boolean
151H.islist = function(t)
152  if type(t) ~= "table" then
153    return false
154  end
155
156  for k, _ in ipairs(t) do
157    return type(k) == "number"
158  end
159
160  -- non-numeric keys
161  for _, _ in pairs(t) do
162    return false
163  end
164
165  -- empty table
166  return true
167end
168
169return M, H