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* 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) diff --git a/lua/dirbuf/config.lua b/lua/dirbuf/config.lua index 9fc5432..f287c38 100644 --- a/lua/dirbuf/config.lua +++ b/lua/dirbuf/config.lua @@ -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 = {} diff --git a/lua/dirbuf/devicons/init.lua b/lua/dirbuf/devicons/init.lua new file mode 100644 index 0000000..245440a --- /dev/null +++ b/lua/dirbuf/devicons/init.lua @@ -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