Skip to content

feat: add support for mini.icons #439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ https://user-images.githubusercontent.com/506791/209727111-6b4a11f4-634a-4efa-94
## Requirements

- 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

Expand All @@ -34,7 +36,8 @@ oil.nvim supports all the usual plugin managers
'stevearc/oil.nvim',
opts = {},
-- Optional dependencies
dependencies = { "nvim-tree/nvim-web-devicons" },
dependencies = { "echasnovski/mini.icons" },
-- dependencies = { "nvim-tree/nvim-web-devicons" }, -- use if prefer nvim-web-devicons
}
```

Expand Down
17 changes: 5 additions & 12 deletions lua/oil/columns.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
local config = require("oil.config")
local constants = require("oil.constants")
local util = require("oil.util")
local has_devicons, devicons = pcall(require, "nvim-web-devicons")
local M = {}

local FIELD_NAME = constants.FIELD_NAME
Expand Down Expand Up @@ -202,7 +201,8 @@ M.perform_change_action = function(adapter, action, callback)
column.perform_action(action, callback)
end

if has_devicons then
local icon_provider = util.get_icon_provider()
if icon_provider then
M.register("icon", {
render = function(entry, conf)
local field_type = entry[FIELD_TYPE]
Expand All @@ -216,17 +216,10 @@ if has_devicons then
field_type = meta.link_stat.type
end
end
local icon, hl
if field_type == "directory" then
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 "")
if meta and meta.display_name then
name = meta.display_name
end
local icon, hl = icon_provider(field_type, name, conf)
if not conf or conf.add_padding ~= false then
icon = icon .. " "
end
Expand Down
28 changes: 28 additions & 0 deletions lua/oil/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ local FIELD_NAME = constants.FIELD_NAME
local FIELD_TYPE = constants.FIELD_TYPE
local FIELD_META = constants.FIELD_META

---@alias oil.IconProvider fun(type: string, name: string, conf: table?): (icon: string, hl: string)

---@param url string
---@return nil|string
---@return nil|string
Expand Down Expand Up @@ -858,4 +860,30 @@ M.get_edit_path = function(bufnr, entry, callback)
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