Skip to content

Commit a543ea5

Browse files
authored
feat: add support for mini.icons (#439)
1 parent b5a1abf commit a543ea5

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ https://user-images.githubusercontent.com/506791/209727111-6b4a11f4-634a-4efa-94
2020
## Requirements
2121

2222
- Neovim 0.8+
23-
- (optional) [nvim-web-devicons](https://github.com/nvim-tree/nvim-web-devicons) for file icons
23+
- Icon provider plugin (optional)
24+
- [mini.icons](https://github.com/echasnovski/mini.nvim/blob/main/readmes/mini-icons.md) for file and folder icons
25+
- [nvim-web-devicons](https://github.com/nvim-tree/nvim-web-devicons) for file icons
2426

2527
## Installation
2628

@@ -34,7 +36,8 @@ oil.nvim supports all the usual plugin managers
3436
'stevearc/oil.nvim',
3537
opts = {},
3638
-- Optional dependencies
37-
dependencies = { "nvim-tree/nvim-web-devicons" },
39+
dependencies = { "echasnovski/mini.icons" },
40+
-- dependencies = { "nvim-tree/nvim-web-devicons" }, -- use if prefer nvim-web-devicons
3841
}
3942
```
4043

lua/oil/columns.lua

+5-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
local config = require("oil.config")
22
local constants = require("oil.constants")
33
local util = require("oil.util")
4-
local has_devicons, devicons = pcall(require, "nvim-web-devicons")
54
local M = {}
65

76
local FIELD_NAME = constants.FIELD_NAME
@@ -202,7 +201,8 @@ M.perform_change_action = function(adapter, action, callback)
202201
column.perform_action(action, callback)
203202
end
204203

205-
if has_devicons then
204+
local icon_provider = util.get_icon_provider()
205+
if icon_provider then
206206
M.register("icon", {
207207
render = function(entry, conf)
208208
local field_type = entry[FIELD_TYPE]
@@ -216,17 +216,10 @@ if has_devicons then
216216
field_type = meta.link_stat.type
217217
end
218218
end
219-
local icon, hl
220-
if field_type == "directory" then
221-
icon = conf and conf.directory or ""
222-
hl = "OilDirIcon"
223-
else
224-
if meta and meta.display_name then
225-
name = meta.display_name
226-
end
227-
icon, hl = devicons.get_icon(name)
228-
icon = icon or (conf and conf.default_file or "")
219+
if meta and meta.display_name then
220+
name = meta.display_name
229221
end
222+
local icon, hl = icon_provider(field_type, name, conf)
230223
if not conf or conf.add_padding ~= false then
231224
icon = icon .. " "
232225
end

lua/oil/util.lua

+28
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ local FIELD_NAME = constants.FIELD_NAME
88
local FIELD_TYPE = constants.FIELD_TYPE
99
local FIELD_META = constants.FIELD_META
1010

11+
---@alias oil.IconProvider fun(type: string, name: string, conf: table?): (icon: string, hl: string)
12+
1113
---@param url string
1214
---@return nil|string
1315
---@return nil|string
@@ -858,4 +860,30 @@ M.get_edit_path = function(bufnr, entry, callback)
858860
end
859861
end
860862

863+
--- Check for an icon provider and return a common icon provider API
864+
---@return (oil.IconProvider)?
865+
M.get_icon_provider = function()
866+
-- prefer mini.icons
867+
local has_mini_icons, mini_icons = pcall(require, "mini.icons")
868+
if has_mini_icons then
869+
return function(type, name)
870+
return mini_icons.get(type == "directory" and "directory" or "file", name)
871+
end
872+
end
873+
874+
-- fallback to `nvim-web-devicons`
875+
local has_devicons, devicons = pcall(require, "nvim-web-devicons")
876+
if has_devicons then
877+
return function(type, name, conf)
878+
if type == "directory" then
879+
return conf and conf.directory or "", "OilDirIcon"
880+
else
881+
local icon, hl = devicons.get_icon(name)
882+
icon = icon or (conf and conf.default_file or "")
883+
return icon, hl
884+
end
885+
end
886+
end
887+
end
888+
861889
return M

0 commit comments

Comments
 (0)