Skip to content

Commit

Permalink
feat: added dir_icon{_color} to file entry maker (#983)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibhagwan committed Jan 4, 2024
1 parent f4f3671 commit eb54a36
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 32 deletions.
4 changes: 2 additions & 2 deletions lua/fzf-lua/complete.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ local function find_toplevel_cwd(maybe_cwd, postfix, orig_cwd)
if vim.fn.isdirectory(vim.fn.expand(maybe_cwd)) == 1 then
local disp_cwd, cwd = maybe_cwd, vim.fn.expand(maybe_cwd)
-- returned cwd must be full path
if cwd:sub(1, 1) == "." and cwd:sub(2, 2) == path.separator() then
if cwd:sub(1, 1) == "." and cwd:sub(2, 2) == path.SEPARATOR then
cwd = vim.loop.cwd() .. (#cwd > 1 and cwd:sub(2) or "")
-- inject "./" only if original path started with it
-- otherwise ignore the "." retval from fnamemodify
Expand Down Expand Up @@ -61,7 +61,7 @@ local set_cmp_opts_path = function(opts)
opts.prompt = "."
end
if not path.ends_with_separator(opts.prompt) then
opts.prompt = opts.prompt .. path.separator()
opts.prompt = opts.prompt .. path.SEPARATOR
end
-- completion function rebuilds the line with the full path
opts.complete = function(selected, o, l, _)
Expand Down
2 changes: 1 addition & 1 deletion lua/fzf-lua/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ M.set_header = function(opts, hdr_tbl)
opts.prompt = path.shorten(opts.prompt, tonumber(opts.cwd_prompt_shorten_val) or 1)
end
if not path.ends_with_separator(opts.prompt) then
opts.prompt = opts.prompt .. path.separator()
opts.prompt = opts.prompt .. path.SEPARATOR
end
end
if opts.no_header or opts.headers == false then
Expand Down
10 changes: 8 additions & 2 deletions lua/fzf-lua/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -899,8 +899,11 @@ M.defaults.dap = {
}

M.defaults.complete_path = {
cmd = nil, -- default: auto detect fd|rg|find
actions = { ["default"] = actions.complete },
cmd = nil, -- default: auto detect fd|rg|find
file_icons = false,
git_icons = false,
color_icons = true,
actions = { ["default"] = actions.complete },
}

M.defaults.complete_file = {
Expand All @@ -921,6 +924,9 @@ M.defaults.file_icon_padding = ""

M.defaults.file_icon_colors = {}

M.defaults.dir_icon = ""
M.defaults.dir_icon_color = "#519aba"

M.defaults.__HLS = {
normal = "FzfLuaNormal",
border = "FzfLuaBorder",
Expand Down
25 changes: 17 additions & 8 deletions lua/fzf-lua/make_entry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ pcall(load_devicons)
if not config then
local _config = { globals = { git = {}, files = {}, grep = {} } }
_config.globals.git.icons = load_config_section("globals.git.icons", "table") or {}
_config.globals.dir_icon = load_config_section("globals.dir_icon", "string")
_config.globals.dir_icon_color = load_config_section("globals.dir_icon_color", "string")
_config.globals.file_icon_colors = load_config_section("globals.file_icon_colors", "table") or {}
_config.globals.file_icon_padding = load_config_section("globals.file_icon_padding", "string")
_config.globals.files.git_status_cmd = load_config_section("globals.files.git_status_cmd", "table")
Expand Down Expand Up @@ -432,14 +434,21 @@ M.file = function(x, opts)
ret[#ret + 1] = utils.nbsp
end
if opts.file_icons then
local filename = path.tail(origpath)
local ext = path.extension(filename)
icon, hl = M.get_devicon(filename, ext)
if opts.color_icons then
-- extra workaround for issue #119 (or similars)
-- use default if we can't find the highlight ansi
local fn = utils.ansi_codes[hl] or utils.ansi_codes["dark_grey"]
icon = fn(icon)
if path.ends_with_separator(origpath) then
icon = config.globals.dir_icon
if opts.color_icons then
icon = utils.ansi_from_rgb(config.globals.dir_icon_color, icon)
end
else
local filename = path.tail(origpath)
local ext = path.extension(filename)
icon, hl = M.get_devicon(filename, ext)
if opts.color_icons then
-- extra workaround for issue #119 (or similars)
-- use default if we can't find the highlight ansi
local fn = utils.ansi_codes[hl] or utils.ansi_codes["dark_grey"]
icon = fn(icon)
end
end
ret[#ret + 1] = icon
ret[#ret + 1] = utils.nbsp
Expand Down
38 changes: 19 additions & 19 deletions lua/fzf-lua/path.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ local string_byte = string.byte

local M = {}

M.SEPARATOR = "/"

M.separator = function()
return "/"
return M.SEPARATOR
end

M.dot_byte = string_byte(".")
M.separator_byte = string_byte(M.separator())
M.separator_byte = string_byte(M.SEPARATOR)

M.starts_with_separator = function(path)
return string_byte(path, 1) == M.separator_byte
-- return path:find("^"..M.separator()) == 1
end

M.ends_with_separator = function(path)
Expand All @@ -24,18 +25,17 @@ M.starts_with_cwd = function(path)
return #path > 1
and string_byte(path, 1) == M.dot_byte
and string_byte(path, 2) == M.separator_byte
-- return path:match("^."..M.separator()) ~= nil
-- return path:match("^."..M.SEPARATOR) ~= nil
end

M.strip_cwd_prefix = function(path)
return #path > 2 and path:sub(3)
end

function M.tail(path)
local os_sep = string_byte(M.separator())

for i = #path, 1, -1 do
if string_byte(path, i) == os_sep then
local end_idx = M.ends_with_separator(path) and (#path - 1) or #path
for i = end_idx, 1, -1 do
if string_byte(path, i) == M.separator_byte then
return path:sub(i + 1)
end
end
Expand All @@ -59,20 +59,20 @@ end

function M.join(paths)
-- gsub to remove double separator
return table.concat(paths, M.separator()):gsub(
M.separator() .. M.separator(), M.separator())
local ret = table.concat(paths, M.SEPARATOR):gsub(M.SEPARATOR .. M.SEPARATOR, M.SEPARATOR)
return ret
end

function M.split(path)
return path:gmatch("[^" .. M.separator() .. "]+" .. M.separator() .. "?")
return path:gmatch("[^" .. M.SEPARATOR .. "]+" .. M.SEPARATOR .. "?")
end

---Get the basename of the given path.
---@param path string
---@return string
function M.basename(path)
path = M.remove_trailing(path)
local i = path:match("^.*()" .. M.separator())
local i = path:match("^.*()" .. M.SEPARATOR)
if not i then return path end
return path:sub(i + 1, #path)
end
Expand All @@ -84,7 +84,7 @@ end
---@return string|nil
function M.parent(path, remove_trailing)
path = " " .. M.remove_trailing(path)
local i = path:match("^.+()" .. M.separator())
local i = path:match("^.+()" .. M.SEPARATOR)
if not i then return nil end
path = path:sub(2, i)
if remove_trailing then
Expand All @@ -108,15 +108,15 @@ function M.is_relative(path, relative_to)
end

function M.add_trailing(path)
if path:sub(-1) == M.separator() then
if path:sub(-1) == M.SEPARATOR then
return path
end

return path .. M.separator()
return path .. M.SEPARATOR
end

function M.remove_trailing(path)
local p, _ = path:gsub(M.separator() .. "$", "")
local p, _ = path:gsub(M.SEPARATOR .. "$", "")
return p
end

Expand Down Expand Up @@ -152,7 +152,7 @@ function M.HOME_to_tilde(path)
end

function M.shorten(path, max_len)
local sep = M.separator()
local sep = M.SEPARATOR
local parts = {}
local start_idx = 1
max_len = max_len and tonumber(max_len) > 0 and max_len or 1
Expand All @@ -172,9 +172,9 @@ end
function M.lengthen(path)
-- we use 'glob_escape' to escape \{} (#548)
path = utils.glob_escape(path)
return vim.fn.glob(path:gsub(M.separator(), "%*" .. M.separator())
return vim.fn.glob(path:gsub(M.SEPARATOR, "%*" .. M.SEPARATOR)
-- remove the starting '*/' if any
:gsub("^%*" .. M.separator(), M.separator())):match("[^\n]+")
:gsub("^%*" .. M.SEPARATOR, M.SEPARATOR)):match("[^\n]+")
or string.format("<glob expand failed for '%s'>", path)
end

Expand Down
8 changes: 8 additions & 0 deletions lua/fzf-lua/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,14 @@ function M.hexcol_from_hl(hlgroup, what)
return hexcol
end

function M.ansi_from_rgb(rgb, s)
local r, g, b = hex2rgb(rgb)
if r and g and b then
return string.format("[38;2;%d;%d;%dm%s%s", r, g, b, s, M.ansi_escseq.clear)
end
return s
end

function M.ansi_from_hl(hl, s)
if not hl or #hl == 0 or vim.fn.hlexists(hl) ~= 1 then
return s, nil
Expand Down

0 comments on commit eb54a36

Please sign in to comment.