feat: add support for mini.icons (#439)
This commit is contained in:
parent
b5a1abfde0
commit
a543ea598e
3 changed files with 38 additions and 14 deletions
|
|
@ -20,7 +20,9 @@ https://user-images.githubusercontent.com/506791/209727111-6b4a11f4-634a-4efa-94
|
||||||
## Requirements
|
## Requirements
|
||||||
|
|
||||||
- Neovim 0.8+
|
- Neovim 0.8+
|
||||||
- (optional) [nvim-web-devicons](https://github.com/nvim-tree/nvim-web-devicons) for file icons
|
- Icon provider plugin (optional)
|
||||||
|
- [mini.icons](https://github.com/echasnovski/mini.nvim/blob/main/readmes/mini-icons.md) for file and folder icons
|
||||||
|
- [nvim-web-devicons](https://github.com/nvim-tree/nvim-web-devicons) for file icons
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
|
|
@ -34,7 +36,8 @@ oil.nvim supports all the usual plugin managers
|
||||||
'stevearc/oil.nvim',
|
'stevearc/oil.nvim',
|
||||||
opts = {},
|
opts = {},
|
||||||
-- Optional dependencies
|
-- Optional dependencies
|
||||||
dependencies = { "nvim-tree/nvim-web-devicons" },
|
dependencies = { "echasnovski/mini.icons" },
|
||||||
|
-- dependencies = { "nvim-tree/nvim-web-devicons" }, -- use if prefer nvim-web-devicons
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
local config = require("oil.config")
|
local config = require("oil.config")
|
||||||
local constants = require("oil.constants")
|
local constants = require("oil.constants")
|
||||||
local util = require("oil.util")
|
local util = require("oil.util")
|
||||||
local has_devicons, devicons = pcall(require, "nvim-web-devicons")
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
local FIELD_NAME = constants.FIELD_NAME
|
local FIELD_NAME = constants.FIELD_NAME
|
||||||
|
|
@ -202,7 +201,8 @@ M.perform_change_action = function(adapter, action, callback)
|
||||||
column.perform_action(action, callback)
|
column.perform_action(action, callback)
|
||||||
end
|
end
|
||||||
|
|
||||||
if has_devicons then
|
local icon_provider = util.get_icon_provider()
|
||||||
|
if icon_provider then
|
||||||
M.register("icon", {
|
M.register("icon", {
|
||||||
render = function(entry, conf)
|
render = function(entry, conf)
|
||||||
local field_type = entry[FIELD_TYPE]
|
local field_type = entry[FIELD_TYPE]
|
||||||
|
|
@ -216,17 +216,10 @@ if has_devicons then
|
||||||
field_type = meta.link_stat.type
|
field_type = meta.link_stat.type
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
local icon, hl
|
if meta and meta.display_name then
|
||||||
if field_type == "directory" then
|
name = meta.display_name
|
||||||
icon = conf and conf.directory or ""
|
|
||||||
hl = "OilDirIcon"
|
|
||||||
else
|
|
||||||
if meta and meta.display_name then
|
|
||||||
name = meta.display_name
|
|
||||||
end
|
|
||||||
icon, hl = devicons.get_icon(name)
|
|
||||||
icon = icon or (conf and conf.default_file or "")
|
|
||||||
end
|
end
|
||||||
|
local icon, hl = icon_provider(field_type, name, conf)
|
||||||
if not conf or conf.add_padding ~= false then
|
if not conf or conf.add_padding ~= false then
|
||||||
icon = icon .. " "
|
icon = icon .. " "
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,8 @@ local FIELD_NAME = constants.FIELD_NAME
|
||||||
local FIELD_TYPE = constants.FIELD_TYPE
|
local FIELD_TYPE = constants.FIELD_TYPE
|
||||||
local FIELD_META = constants.FIELD_META
|
local FIELD_META = constants.FIELD_META
|
||||||
|
|
||||||
|
---@alias oil.IconProvider fun(type: string, name: string, conf: table?): (icon: string, hl: string)
|
||||||
|
|
||||||
---@param url string
|
---@param url string
|
||||||
---@return nil|string
|
---@return nil|string
|
||||||
---@return nil|string
|
---@return nil|string
|
||||||
|
|
@ -858,4 +860,30 @@ M.get_edit_path = function(bufnr, entry, callback)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- Check for an icon provider and return a common icon provider API
|
||||||
|
---@return (oil.IconProvider)?
|
||||||
|
M.get_icon_provider = function()
|
||||||
|
-- prefer mini.icons
|
||||||
|
local has_mini_icons, mini_icons = pcall(require, "mini.icons")
|
||||||
|
if has_mini_icons then
|
||||||
|
return function(type, name)
|
||||||
|
return mini_icons.get(type == "directory" and "directory" or "file", name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- fallback to `nvim-web-devicons`
|
||||||
|
local has_devicons, devicons = pcall(require, "nvim-web-devicons")
|
||||||
|
if has_devicons then
|
||||||
|
return function(type, name, conf)
|
||||||
|
if type == "directory" then
|
||||||
|
return conf and conf.directory or "", "OilDirIcon"
|
||||||
|
else
|
||||||
|
local icon, hl = devicons.get_icon(name)
|
||||||
|
icon = icon or (conf and conf.default_file or "")
|
||||||
|
return icon, hl
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue