Skip to content

Commit

Permalink
feat(entry_maker): file-descriptor mode column (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
CKolkey committed Mar 24, 2023
1 parent 24389d8 commit ff558dd
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ require("telescope").setup {
quiet = false,
dir_icon = "",
dir_icon_hl = "Default",
display_stat = { date = true, size = true },
display_stat = { date = true, size = true, mode = true },
hijack_netrw = false,
use_fd = true,
git_status = true,
Expand Down
83 changes: 70 additions & 13 deletions lua/telescope/_extensions/file_browser/make_entry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,53 @@ local DATE = {
hl = "TelescopePreviewDate",
}

local stat_enum = { size = SIZE, date = DATE }
local function mode_perm(bit)
if bit == "0" then
return "---"
elseif bit == "1" then
return "--x"
elseif bit == "2" then
return "-w-"
elseif bit == "3" then
return "-wx"
elseif bit == "4" then
return "r--"
elseif bit == "5" then
return "r-x"
elseif bit == "6" then
return "rw-"
elseif bit == "7" then
return "rwx"
end
end

local function mode_type(type)
if type == "directory" then
return "d"
elseif type == "link" then
return "l"
else
return "-"
end
end

local MODE = {
width = 11,
right_justify = true,
display = function(entry)
local owner, group, other = string.format("%3o", entry.stat.mode):match "(.)(.)(.)$"
return table.concat {
mode_type(entry.lstat.type),
mode_perm(owner),
mode_perm(group),
mode_perm(other),
entry.stat.flags ~= 0 and "@" or " ",
}
end,
hl = "TelescopePreviewWrite",
}

local stat_enum = { size = SIZE, date = DATE, mode = MODE }

local get_fb_prompt = function()
local prompt_bufnr = vim.tbl_filter(function(b)
Expand Down Expand Up @@ -196,22 +242,26 @@ local make_entry = function(opts)
end

local file_width = vim.F.if_nil(opts.file_width, math.max(15, total_file_width))
-- TODO maybe this can be dealth with more cleanly
-- TODO maybe this can be dealt with more cleanly
if #path_display > file_width then
path_display = strings.truncate(path_display, file_width, nil, -1)
end
path_display = is_dir and { path_display, "TelescopePreviewDirectory" } or path_display
table.insert(display_array, entry.stat and path_display or { path_display, "WarningMsg" })
table.insert(widths, { width = file_width })
if opts.display_stat then
for _, v in pairs(opts.display_stat) do
-- stat may be false meaning file not found / unavailable, e.g. broken symlink
if entry.stat then

-- stat may be false meaning file not found / unavailable, e.g. broken symlink
if entry.stat and opts.display_stat then
for _, stat in ipairs { "mode", "size", "date" } do
local v = opts.display_stat[stat]

if v then
table.insert(widths, { width = v.width, right_justify = v.right_justify })
table.insert(display_array, { v.display(entry), v.hl })
end
end
end

-- original prompt bufnr becomes invalid with `:Telescope resume`
if not vim.api.nvim_buf_is_valid(prompt_bufnr) then
prompt_bufnr = get_fb_prompt()
Expand Down Expand Up @@ -259,16 +309,23 @@ local make_entry = function(opts)
end
return t.value
end

if k == "stat" then
local stat = vim.loop.fs_stat(t.value)
t.stat = vim.F.if_nil(stat, false)
t.stat = vim.F.if_nil(vim.loop.fs_stat(t.value), false)
if not t.stat then
local lstat = vim.F.if_nil(vim.loop.fs_lstat(t.value), false)
if not lstat then
log.warn("Unable to get stat for " .. t.value)
end
return t.lstat
end
return t.stat
end

if k == "lstat" then
local lstat = vim.F.if_nil(vim.loop.fs_lstat(t.value), false)
if not lstat then
log.warn("Unable to get stat for " .. t.value)
else
t.lstat = lstat
end
return stat
return t.lstat
end

return rawget(t, rawget({ value = 1 }, k))
Expand Down
4 changes: 2 additions & 2 deletions lua/telescope/_extensions/file_browser/picker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ local fb_picker = {}
---@field quiet boolean: surpress any notification from file_brower actions (default: false)
---@field dir_icon string: change the icon for a directory (default: )
---@field dir_icon_hl string: change the highlight group of dir icon (default: "Default")
---@field display_stat boolean|table: ordered stat; see above notes, (default: `{ date = true, size = true }`)
---@field display_stat boolean|table: ordered stat; see above notes, (default: `{ date = true, size = true, mode = true }`)
---@field hijack_netrw boolean: use telescope file browser when opening directory paths; must be set on `setup` (default: false)
---@field use_fd boolean: use `fd` if available over `plenary.scandir` (default: true)
---@field git_status boolean: show the git status of files (default: true if `git` executable can be found)
Expand All @@ -87,7 +87,7 @@ fb_picker.file_browser = function(opts)
opts.quiet = vim.F.if_nil(opts.quiet, false)
opts.hide_parent_dir = vim.F.if_nil(opts.hide_parent_dir, false)
opts.select_buffer = vim.F.if_nil(opts.select_buffer, false)
opts.display_stat = vim.F.if_nil(opts.display_stat, { date = true, size = true })
opts.display_stat = vim.F.if_nil(opts.display_stat, { mode = true, date = true, size = true })
opts.custom_prompt_title = opts.prompt_title ~= nil
opts.custom_results_title = opts.results_title ~= nil
opts.use_fd = vim.F.if_nil(opts.use_fd, true)
Expand Down

0 comments on commit ff558dd

Please sign in to comment.