robin/lylla.nvim

a tiny, customizable statusline for neovim

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

robinperf: do not use fold for string resultsd07bc2d

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