feat: add get_icon and get_icon_by_filetype API

Problem: plugins that don't use devicons have no way to get nonicons
glyphs for files. The only integration path is the devicons
monkey-patch via apply().

Solution: add get_icon(name, ext) and get_icon_by_filetype(ft) to
the public API in init.lua. Both use lazy require of the resolve
module and return nil on miss so callers decide fallback. Document
both functions in vimdoc and update the oil.nvim recipe.
This commit is contained in:
Barrett Ruth 2026-02-22 21:31:35 -05:00
parent b9a310dc37
commit 93a453e997
Signed by: barrett
GPG key ID: A6C96C9349D2FC81
2 changed files with 44 additions and 2 deletions

View file

@ -53,6 +53,33 @@ API *nonicons-api*
Parameters: ~
{name} `string` Icon name (e.g. `'lua'`, `'python'`, `'git-branch'`)
Returns: ~
`string?` The single-character nonicons glyph
*nonicons.get_icon()*
`require('nonicons').get_icon(name, ext)`
Returns the nonicons character for a file, resolved by filename and/or
extension. Returns `nil` if no match is found (caller decides fallback).
Resolution order: exact extension → exact filename → extracted extension.
Parameters: ~
{name} `string?` Filename (e.g. `'init.lua'`, `'Makefile'`)
{ext} `string?` File extension (e.g. `'lua'`, `'py'`)
Returns: ~
`string?` The single-character nonicons glyph
*nonicons.get_icon_by_filetype()*
`require('nonicons').get_icon_by_filetype(ft)`
Returns the nonicons character for a vim filetype. Returns `nil` if no
match is found.
Resolution order: direct mapping key → extension table → filetype table.
Parameters: ~
{ft} `string` Vim filetype (e.g. `'python'`, `'typescriptreact'`)
Returns: ~
`string?` The single-character nonicons glyph
@ -128,8 +155,9 @@ mason.nvim ~
oil.nvim ~
No configuration needed. oil.nvim reads the devicons extension/filename
tables directly, which nonicons.nvim mutates on load.
No configuration needed. oil.nvim detects nonicons.nvim and uses it as a
direct icon provider. If devicons is also loaded, the devicons override
still applies to other plugins.
fzf-lua ~
>lua

View file

@ -40,4 +40,18 @@ function M.apply()
end
end
function M.get_icon(name, ext)
local key = require('nonicons.resolve').resolve_name(name, ext)
if key then
return M.get(key)
end
end
function M.get_icon_by_filetype(ft)
local key = require('nonicons.resolve').resolve_filetype(ft)
if key then
return M.get(key)
end
end
return M