diff --git a/doc/telescope.txt b/doc/telescope.txt index 81a7006..5e95c49 100644 --- a/doc/telescope.txt +++ b/doc/telescope.txt @@ -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* diff --git a/lua/telescope/config.lua b/lua/telescope/config.lua index 1c573ba..1e1f866 100644 --- a/lua/telescope/config.lua +++ b/lua/telescope/config.lua @@ -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 ]] ) diff --git a/lua/telescope/previewers/buffer_previewer.lua b/lua/telescope/previewers/buffer_previewer.lua index 8a077f5..cb0e629 100644 --- a/lua/telescope/previewers/buffer_previewer.lua +++ b/lua/telescope/previewers/buffer_previewer.lua @@ -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