Skip to content
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

Add nvim-web-devicon support #44

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,28 @@ require("dirbuf").setup {
show_hidden = true,
sort_order = "default",
write_cmd = "DirbufSync",
devicons = false,
}
```

Read the [documentation](/doc/dirbuf.txt) for more information (`:help
dirbuf-options`).

### Devicons
Devicon support requires [nvim-web-devicons](https://github.com/kyazdani42/nvim-web-devicons).
If not already installed, require it with your package manager if supported:

```lua
use {"elihunter173/dirbuf.nvim", requires = "kyazdani42/nvim-web-devicons"}
```

Otherwise make sure it is loaded before dirbuf:

```vim
Plug "kyazdani42/nvim-web-devicons"
Plug "elihunter173/dirbuf.nvim"
```

## Development

A [Justfile][just] is provided to test and lint the project.
Expand Down
4 changes: 4 additions & 0 deletions doc/dirbuf.txt
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ OPTIONS *dirbuf-options*
Disables `:write` in directory buffers, forcing users to
explicitly invoke `:DirbufSync`.

|devicons| (default: `false`)
Enable support for devicons through:
`https://github.com/kyazdani42/nvim-web-devicons`. Set to true to enable.

==============================================================================
FAQ *dirbuf-faq*

Expand Down
22 changes: 21 additions & 1 deletion lua/dirbuf/buffer.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
local fs = require("dirbuf.fs")
local devicons = require("dirbuf.devicons.init")
local config = require("dirbuf.config")

local M = {}

Expand Down Expand Up @@ -159,6 +161,20 @@ function M.parse_line(line)
chars = line:gmatch(".")
end

-- We need to make sure to never parse the devicon and the proceeding space
-- Any icon will not be a character
-- chars() will iterate until the file name at this point
-- Ignore all bytes and the first space
if config.get("devicons") then
if chars() ~= nil then
for char in chars do
if char == " " then
break
end
end
end
end

local err, fname, ftype = parse_fname(chars)
if err ~= nil then
return err
Expand All @@ -178,7 +194,11 @@ function M.display_fs_entry(fs_entry)
for escape_char, unescaped in pairs(ESCAPE_CHARS) do
escaped = escaped:gsub(unescaped, "\\" .. escape_char)
end
return escaped .. ftype_to_suffix(fs_entry.ftype)
if config.get("devicons") then
return devicons.get_icon(fs_entry.fname, fs_entry.ftype) .. " " .. escaped .. ftype_to_suffix(fs_entry.ftype)
else
return escaped .. ftype_to_suffix(fs_entry.ftype)
end
end

function M.write_fs_entries(fs_entries, track_fname)
Expand Down
11 changes: 11 additions & 0 deletions lua/dirbuf/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ local CONFIG_SPEC = {
end
end,
},
devicons = {
default = false,
check = function(val)
if require('dirbuf.devicons').has_devicons() ~= true and val == true then
return "nvim-web-devicons not installed"
end
if type(val) ~= "boolean" then
return "must be boolean, received " .. type(val)
end
end,
},
}

local user_config = {}
Expand Down
25 changes: 25 additions & 0 deletions lua/dirbuf/devicons/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--[[
Support for nvim-web-devicons
--]]


local M = {}

function M.has_devicons()
local ok, _ = pcall(require, "nvim-web-devicons")
return ok
end

function M.get_icon(fname, ftype)
local devicons = require('nvim-web-devicons')
if ftype == "file" then
local ext = vim.fn.fnamemodify(fname, ":e")
return devicons.get_icon(fname, ext, {default = true})
elseif ftype == "directory" then
return ""
else
return devicons.get_icon(fname, "", {default = true})
end
end

return M