@@ -12,7 +12,6 @@ local M = {}
1212local astro = require " astrocore"
1313local ui = require " astroui"
1414local config = assert (ui .config .status )
15- local get_icon = ui .get_icon
1615local 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
139138end
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