From a085df9d983266903b6a1f9d07640485c306404e Mon Sep 17 00:00:00 2001 From: Kat Date: Wed, 27 Jul 2022 21:35:40 -0400 Subject: [PATCH 1/5] feat: Add devicon framework support --- lua/dirbuf/config.lua | 8 ++++++++ lua/dirbuf/devicons/init.lua | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 lua/dirbuf/devicons/init.lua diff --git a/lua/dirbuf/config.lua b/lua/dirbuf/config.lua index 9fc5432..44f34cf 100644 --- a/lua/dirbuf/config.lua +++ b/lua/dirbuf/config.lua @@ -51,6 +51,14 @@ local CONFIG_SPEC = { end end, }, + devicons = { + default = false, + check = function(val) + if type(val) ~= "boolean" then + return "must be boolean, received " .. type(val) + end + end, + }, } local user_config = {} diff --git a/lua/dirbuf/devicons/init.lua b/lua/dirbuf/devicons/init.lua new file mode 100644 index 0000000..f0855aa --- /dev/null +++ b/lua/dirbuf/devicons/init.lua @@ -0,0 +1,24 @@ +--[[ +Support for nvim-web-devicons +--]] + +local devicons = require('nvim-web-devicons') + +local M = {} + +function M.has_devicons() + return assert(devicons.has_loaded(), "Devicons not found for dirbuf.nvim") +end + +function M.get_icon(fname, ftype) + 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 From b023311897c6a10bdb5670d863303b8c08d054a4 Mon Sep 17 00:00:00 2001 From: Kat Date: Wed, 27 Jul 2022 21:36:30 -0400 Subject: [PATCH 2/5] feat: Ignore devicons in parsing, show devicons in dirbuf --- lua/dirbuf/buffer.lua | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/lua/dirbuf/buffer.lua b/lua/dirbuf/buffer.lua index f7768bb..067a29a 100644 --- a/lua/dirbuf/buffer.lua +++ b/lua/dirbuf/buffer.lua @@ -1,4 +1,6 @@ local fs = require("dirbuf.fs") +local devicons = require("dirbuf.devicons.init") +local config = require("dirbuf.config") local M = {} @@ -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 @@ -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) From 0dbd957c2e80fc607725e7dab07338d98ef60a6d Mon Sep 17 00:00:00 2001 From: Kat Date: Wed, 27 Jul 2022 21:54:52 -0400 Subject: [PATCH 3/5] fix: Be more explicit about devicon requirements --- lua/dirbuf/config.lua | 3 +++ lua/dirbuf/devicons/init.lua | 5 +++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lua/dirbuf/config.lua b/lua/dirbuf/config.lua index 44f34cf..f287c38 100644 --- a/lua/dirbuf/config.lua +++ b/lua/dirbuf/config.lua @@ -54,6 +54,9 @@ local CONFIG_SPEC = { 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 diff --git a/lua/dirbuf/devicons/init.lua b/lua/dirbuf/devicons/init.lua index f0855aa..ede1401 100644 --- a/lua/dirbuf/devicons/init.lua +++ b/lua/dirbuf/devicons/init.lua @@ -2,15 +2,16 @@ Support for nvim-web-devicons --]] -local devicons = require('nvim-web-devicons') local M = {} function M.has_devicons() - return assert(devicons.has_loaded(), "Devicons not found for dirbuf.nvim") + local ok, has = 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}) From 2b00d18664e3415d105d6aa95af27db55718628c Mon Sep 17 00:00:00 2001 From: Kat Date: Wed, 27 Jul 2022 22:04:41 -0400 Subject: [PATCH 4/5] fix: Remove unused variable --- lua/dirbuf/devicons/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/dirbuf/devicons/init.lua b/lua/dirbuf/devicons/init.lua index ede1401..245440a 100644 --- a/lua/dirbuf/devicons/init.lua +++ b/lua/dirbuf/devicons/init.lua @@ -6,7 +6,7 @@ Support for nvim-web-devicons local M = {} function M.has_devicons() - local ok, has = pcall(require, "nvim-web-devicons") + local ok, _ = pcall(require, "nvim-web-devicons") return ok end From 9626c636f57f8da4eb0b7af08bf0c115eb5d1f45 Mon Sep 17 00:00:00 2001 From: Kat Date: Wed, 27 Jul 2022 22:15:09 -0400 Subject: [PATCH 5/5] docs: Add information about devicon support --- README.md | 16 ++++++++++++++++ doc/dirbuf.txt | 4 ++++ 2 files changed, 20 insertions(+) diff --git a/README.md b/README.md index ee3ce64..2a013fe 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/doc/dirbuf.txt b/doc/dirbuf.txt index b1ac6f5..3a42d47 100644 --- a/doc/dirbuf.txt +++ b/doc/dirbuf.txt @@ -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*