feat(previewer): add option for ls --short (#2486)

Co-authored-by: Github Actions <actions@github>
This commit is contained in:
James Trew
2023-05-16 02:02:09 -04:00
committed by GitHub
parent ebf93395e7
commit 40c31fdde9
3 changed files with 48 additions and 12 deletions

View File

@@ -523,6 +523,10 @@ telescope.setup({opts}) *telescope.setup()*
- hide_on_startup: Hide previewer when picker starts. Previewer can be toggled
with actions.layout.toggle_preview.
Default: false
- ls_short: Determines whether to use the `--short` flag for the `ls`
command when previewing directories. Otherwise will result
to using `--long`.
Default: false
*telescope.defaults.vimgrep_arguments*

View File

@@ -621,6 +621,10 @@ append(
- hide_on_startup: Hide previewer when picker starts. Previewer can be toggled
with actions.layout.toggle_preview.
Default: false
- ls_short: Determines whether to use the `--short` flag for the `ls`
command when previewing directories. Otherwise will result
to using `--long`.
Default: false
]]
)

View File

@@ -95,7 +95,7 @@ color_hash[6] = function(line)
return color_hash[line:sub(1, 1)]
end
local colorize_ls = function(bufnr, data, sections)
local colorize_ls_long = function(bufnr, data, sections)
local windows_add = Path.path.sep == "\\" and 2 or 0
for lnum, line in ipairs(data) do
local section = sections[lnum]
@@ -117,6 +117,44 @@ local colorize_ls = function(bufnr, data, sections)
end
end
local handle_directory_preview = function(filepath, bufnr, opts)
opts.preview.ls_short = vim.F.if_nil(opts.preview.ls_short, false)
local set_colorize_lines
if opts.preview.ls_short then
set_colorize_lines = function(data, sections)
local PATH_SECTION = Path.path.sep == "\\" and 4 or 6
local paths = {}
for i, line in ipairs(data) do
local section = sections[i][PATH_SECTION]
local path = line:sub(section.start_index, section.end_index)
table.insert(paths, path)
end
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, paths)
for i, path in ipairs(paths) do
local hl = color_hash[6](data[i])
vim.api.nvim_buf_add_highlight(bufnr, ns_previewer, hl, i - 1, 0, #path)
end
end
else
set_colorize_lines = function(data, sections)
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, data)
colorize_ls_long(bufnr, data, sections)
end
end
pscan.ls_async(filepath, {
hidden = true,
group_directories_first = true,
on_exit = vim.schedule_wrap(function(data, sections)
set_colorize_lines(data, sections)
if opts.callback then
opts.callback(bufnr)
end
end),
})
end
local search_cb_jump = function(self, bufnr, query)
if not query then
return
@@ -177,17 +215,7 @@ previewers.file_maker = function(filepath, bufnr, opts)
return
end
if stat.type == "directory" then
pscan.ls_async(filepath, {
hidden = true,
group_directories_first = true,
on_exit = vim.schedule_wrap(function(data, sections)
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, data)
colorize_ls(bufnr, data, sections)
if opts.callback then
opts.callback(bufnr)
end
end),
})
handle_directory_preview(filepath, bufnr, opts)
else
if opts.preview.check_mime_type == true and has_file and opts.ft == "" then
-- avoid SIGABRT in buffer previewer happening with utils.get_os_command_output