Skip to content

Commit

Permalink
feat(utils): move icon provider to a utility function with caching
Browse files Browse the repository at this point in the history
  • Loading branch information
mehalter committed Jul 8, 2024
1 parent f2df6f9 commit c2cd437
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 50 deletions.
24 changes: 1 addition & 23 deletions lua/astroui/status/hl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,7 @@ function M.mode_bg() return config.modes[vim.fn.mode()][2] end
---@return table # the highlight group for the current filetype foreground
-- @usage local heirline_component = { provider = require("astroui.status").provider.fileicon(), hl = require("astroui.status").hl.filetype_color },
function M.filetype_color(self)
local color
local bufnr = self and self.bufnr or 0
local bufname = vim.fn.fnamemodify(vim.api.nvim_buf_get_name(bufnr), ":t")
local filetype = vim.bo[bufnr].filetype

local _, mini_icons = pcall(require, "mini.icons")
if _G.MiniIcons then -- mini.icons
local _, hl, is_default = mini_icons.get("file", bufname)
if is_default then
_, hl, is_default = mini_icons.get("filetype", filetype)
end
color = require("astroui").get_hlgroup(hl).fg
if type(color) == "number" then color = string.format("#%06x", color) end
else -- nvim-web-devicons
local devicons_avail, devicons = pcall(require, "nvim-web-devicons")
if devicons_avail then
_, color = devicons.get_icon_color(bufname)
if not color then
_, color = devicons.get_icon_color_by_filetype(filetype)
end
end
end

local _, color = require("astroui.status.utils").icon_provider(self and self.bufnr or 0)
return { fg = color }
end

Expand Down
26 changes: 1 addition & 25 deletions lua/astroui/status/provider.lua
Original file line number Diff line number Diff line change
Expand Up @@ -421,31 +421,7 @@ end
-- @usage local heirline_component = { provider = require("astroui.status").provider.file_icon() }
-- @see astroui.status.utils.stylize
function M.file_icon(opts)
return function(self)
local ft_icon
local bufnr = self and self.bufnr or 0
local bufname = vim.fn.fnamemodify(vim.api.nvim_buf_get_name(bufnr), ":t")
local filetype = vim.bo[bufnr].filetype

local _, mini_icons = pcall(require, "mini.icons")
if _G.MiniIcons then -- mini.icons
local is_default
ft_icon, _, is_default = mini_icons.get("file", bufname)
if is_default then
ft_icon, _, is_default = mini_icons.get("filetype", filetype)
end
else -- nvim-web-devicons
local devicons_avail, devicons = pcall(require, "nvim-web-devicons")
if devicons_avail then
ft_icon = devicons.get_icon(bufname)
if not ft_icon then
ft_icon = devicons.get_icon_by_filetype(filetype, { default = vim.bo[bufnr].buftype == "" })
end
end
end

return status_utils.stylize(ft_icon, opts)
end
return function(self) return status_utils.stylize(status_utils.icon_provider(self and self.bufnr or 0), opts) end
end

--- A provider function for showing the current git branch
Expand Down
49 changes: 47 additions & 2 deletions lua/astroui/status/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ local M = {}
local astro = require "astrocore"
local ui = require "astroui"
local config = assert(ui.config.status)
local get_icon = ui.get_icon
local extend_tbl = astro.extend_tbl

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

---@type false|fun(bufname: string, filetype: string, buftype: string): string?,string?
local cached_icon_provider
--- Resolve the icon and color information for a given buffer
---@param bufnr integer the buffer number to resolve the icon and color of
---@return string? icon the icon string
---@return string? color the hex color of the icon
function M.icon_provider(bufnr)
if not bufnr then bufnr = 0 end
local bufname = vim.fn.fnamemodify(vim.api.nvim_buf_get_name(bufnr), ":t")
local filetype = vim.bo[bufnr].filetype
local buftype = vim.bo[bufnr].buftype
if cached_icon_provider then return cached_icon_provider(bufname, filetype, buftype) end
if cached_icon_provider == false then return end

local _, mini_icons = pcall(require, "mini.icons")
-- mini.icons
if _G.MiniIcons then
cached_icon_provider = function(_bufname, _filetype)
local icon, hl, is_default = mini_icons.get("file", _bufname)
if is_default then
icon, hl, is_default = mini_icons.get("filetype", _filetype)
end
local color = require("astroui").get_hlgroup(hl).fg
if type(color) == "number" then color = string.format("#%06x", color) end
return icon, color
end
return cached_icon_provider(bufname, filetype, bufname)
end

-- nvim-web-devicons
local devicons_avail, devicons = pcall(require, "nvim-web-devicons")
if devicons_avail then
cached_icon_provider = function(_bufname, _filetype, _buftype)
local icon, color = devicons.get_icon_color(_bufname)
if not color then
icon, color = devicons.get_icon_color_by_filetype(_filetype, { default = _buftype == "" })
end
return icon, color
end
return cached_icon_provider(bufname, filetype, buftype)
end

-- fallback to no icon provider
cached_icon_provider = false
end

--- Encode a position to a single value that can be decoded later
---@param line integer line number of position
---@param col integer column number of position
Expand Down

0 comments on commit c2cd437

Please sign in to comment.