Skip to content

Commit c2cd437

Browse files
committed
feat(utils): move icon provider to a utility function with caching
1 parent f2df6f9 commit c2cd437

File tree

3 files changed

+49
-50
lines changed

3 files changed

+49
-50
lines changed

lua/astroui/status/hl.lua

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,7 @@ function M.mode_bg() return config.modes[vim.fn.mode()][2] end
3838
---@return table # the highlight group for the current filetype foreground
3939
-- @usage local heirline_component = { provider = require("astroui.status").provider.fileicon(), hl = require("astroui.status").hl.filetype_color },
4040
function M.filetype_color(self)
41-
local color
42-
local bufnr = self and self.bufnr or 0
43-
local bufname = vim.fn.fnamemodify(vim.api.nvim_buf_get_name(bufnr), ":t")
44-
local filetype = vim.bo[bufnr].filetype
45-
46-
local _, mini_icons = pcall(require, "mini.icons")
47-
if _G.MiniIcons then -- mini.icons
48-
local _, hl, is_default = mini_icons.get("file", bufname)
49-
if is_default then
50-
_, hl, is_default = mini_icons.get("filetype", filetype)
51-
end
52-
color = require("astroui").get_hlgroup(hl).fg
53-
if type(color) == "number" then color = string.format("#%06x", color) end
54-
else -- nvim-web-devicons
55-
local devicons_avail, devicons = pcall(require, "nvim-web-devicons")
56-
if devicons_avail then
57-
_, color = devicons.get_icon_color(bufname)
58-
if not color then
59-
_, color = devicons.get_icon_color_by_filetype(filetype)
60-
end
61-
end
62-
end
63-
41+
local _, color = require("astroui.status.utils").icon_provider(self and self.bufnr or 0)
6442
return { fg = color }
6543
end
6644

lua/astroui/status/provider.lua

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -421,31 +421,7 @@ end
421421
-- @usage local heirline_component = { provider = require("astroui.status").provider.file_icon() }
422422
-- @see astroui.status.utils.stylize
423423
function M.file_icon(opts)
424-
return function(self)
425-
local ft_icon
426-
local bufnr = self and self.bufnr or 0
427-
local bufname = vim.fn.fnamemodify(vim.api.nvim_buf_get_name(bufnr), ":t")
428-
local filetype = vim.bo[bufnr].filetype
429-
430-
local _, mini_icons = pcall(require, "mini.icons")
431-
if _G.MiniIcons then -- mini.icons
432-
local is_default
433-
ft_icon, _, is_default = mini_icons.get("file", bufname)
434-
if is_default then
435-
ft_icon, _, is_default = mini_icons.get("filetype", filetype)
436-
end
437-
else -- nvim-web-devicons
438-
local devicons_avail, devicons = pcall(require, "nvim-web-devicons")
439-
if devicons_avail then
440-
ft_icon = devicons.get_icon(bufname)
441-
if not ft_icon then
442-
ft_icon = devicons.get_icon_by_filetype(filetype, { default = vim.bo[bufnr].buftype == "" })
443-
end
444-
end
445-
end
446-
447-
return status_utils.stylize(ft_icon, opts)
448-
end
424+
return function(self) return status_utils.stylize(status_utils.icon_provider(self and self.bufnr or 0), opts) end
449425
end
450426

451427
--- A provider function for showing the current git branch

lua/astroui/status/utils.lua

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ local M = {}
1212
local astro = require "astrocore"
1313
local ui = require "astroui"
1414
local config = assert(ui.config.status)
15-
local get_icon = ui.get_icon
1615
local extend_tbl = astro.extend_tbl
1716

1817
--- Convert a component parameter table to a table that can be used with the component builder
@@ -76,7 +75,7 @@ function M.stylize(str, opts)
7675
escape = true,
7776
icon = { kind = "NONE", padding = { left = 0, right = 0 } },
7877
}, opts)
79-
local icon = M.pad_string(get_icon(opts.icon.kind), opts.icon.padding)
78+
local icon = M.pad_string(ui.get_icon(opts.icon.kind), opts.icon.padding)
8079
return str
8180
and (str ~= "" or opts.show_empty)
8281
and opts.separator.left .. M.pad_string(icon .. (opts.escape and escape(str) or str), opts.padding) .. opts.separator.right
@@ -138,6 +137,52 @@ function M.surround(separator, color, component, condition, update)
138137
return surrounded
139138
end
140139

140+
---@type false|fun(bufname: string, filetype: string, buftype: string): string?,string?
141+
local cached_icon_provider
142+
--- Resolve the icon and color information for a given buffer
143+
---@param bufnr integer the buffer number to resolve the icon and color of
144+
---@return string? icon the icon string
145+
---@return string? color the hex color of the icon
146+
function M.icon_provider(bufnr)
147+
if not bufnr then bufnr = 0 end
148+
local bufname = vim.fn.fnamemodify(vim.api.nvim_buf_get_name(bufnr), ":t")
149+
local filetype = vim.bo[bufnr].filetype
150+
local buftype = vim.bo[bufnr].buftype
151+
if cached_icon_provider then return cached_icon_provider(bufname, filetype, buftype) end
152+
if cached_icon_provider == false then return end
153+
154+
local _, mini_icons = pcall(require, "mini.icons")
155+
-- mini.icons
156+
if _G.MiniIcons then
157+
cached_icon_provider = function(_bufname, _filetype)
158+
local icon, hl, is_default = mini_icons.get("file", _bufname)
159+
if is_default then
160+
icon, hl, is_default = mini_icons.get("filetype", _filetype)
161+
end
162+
local color = require("astroui").get_hlgroup(hl).fg
163+
if type(color) == "number" then color = string.format("#%06x", color) end
164+
return icon, color
165+
end
166+
return cached_icon_provider(bufname, filetype, bufname)
167+
end
168+
169+
-- nvim-web-devicons
170+
local devicons_avail, devicons = pcall(require, "nvim-web-devicons")
171+
if devicons_avail then
172+
cached_icon_provider = function(_bufname, _filetype, _buftype)
173+
local icon, color = devicons.get_icon_color(_bufname)
174+
if not color then
175+
icon, color = devicons.get_icon_color_by_filetype(_filetype, { default = _buftype == "" })
176+
end
177+
return icon, color
178+
end
179+
return cached_icon_provider(bufname, filetype, buftype)
180+
end
181+
182+
-- fallback to no icon provider
183+
cached_icon_provider = false
184+
end
185+
141186
--- Encode a position to a single value that can be decoded later
142187
---@param line integer line number of position
143188
---@param col integer column number of position

0 commit comments

Comments
 (0)