diff --git a/README.md b/README.md index 16cb03e..7a4963f 100644 --- a/README.md +++ b/README.md @@ -156,7 +156,38 @@ vim.keymap.set("n", "cd", t.extensions.zoxide.list) vim.cmd.tcd(selection.path) end, }, - } + }, + + show_score = true, + -- See `:help telescope.defaults.path_display` + path_display = function(opts, path) + local transformed_path = vim.trim(path) + -- Replace home with ~ + local home = (vim.uv or vim.loop).os_homedir() + transformed_path = home and transformed_path:gsub("^" .. vim.pesc(home), "~") or transformed_path + -- Truncate + local calc_result_length = function(truncate_len) + local status = get_status(vim.api.nvim_get_current_buf()) + -- Compatibility with telescope.nvim 0.1.4 + local results_win = vim.tbl_get(status, "layout", "results", "winid") or status.results_win + local len = vim.api.nvim_win_get_width(results_win) - status.picker.selection_caret:len() - 2 + return type(truncate_len) == "number" and len - truncate_len or len + end + local truncate_len = nil + if opts.__length == nil then + opts.__length = calc_result_length(truncate_len) + end + if opts.__prefix == nil then + opts.__prefix = 0 + end + transformed_path = truncate(transformed_path, opts.__length - opts.__prefix, nil, -1) + -- Dim parent directories + local tail = utils.path_tail(path) + local path_style = { + { { 0, #transformed_path - #tail }, "Comment" }, + } + return transformed_path, path_style + end, } ``` diff --git a/lua/telescope/_extensions/zoxide/config.lua b/lua/telescope/_extensions/zoxide/config.lua index 03900a6..7d34b5a 100644 --- a/lua/telescope/_extensions/zoxide/config.lua +++ b/lua/telescope/_extensions/zoxide/config.lua @@ -2,6 +2,9 @@ local builtin = require("telescope.builtin") local utils = require("telescope.utils") local z_utils = require("telescope._extensions.zoxide.utils") +local truncate = require("plenary.strings").truncate +local get_status = require("telescope.state").get_status + local config = {} local default_config = { @@ -32,7 +35,38 @@ local default_config = { vim.cmd.tcd(selection.path) end, }, - } + }, + + show_score = true, + -- See `:help telescope.defaults.path_display` + path_display = function(opts, path) + local transformed_path = vim.trim(path) + -- Replace home with ~ + local home = (vim.uv or vim.loop).os_homedir() + transformed_path = home and transformed_path:gsub("^" .. vim.pesc(home), "~") or transformed_path + -- Truncate + local calc_result_length = function(truncate_len) + local status = get_status(vim.api.nvim_get_current_buf()) + -- Compatibility with telescope.nvim 0.1.4 + local results_win = vim.tbl_get(status, "layout", "results", "winid") or status.results_win + local len = vim.api.nvim_win_get_width(results_win) - status.picker.selection_caret:len() - 2 + return type(truncate_len) == "number" and len - truncate_len or len + end + local truncate_len = nil + if opts.__length == nil then + opts.__length = calc_result_length(truncate_len) + end + if opts.__prefix == nil then + opts.__prefix = 0 + end + transformed_path = truncate(transformed_path, opts.__length - opts.__prefix, nil, -1) + -- Dim parent directories + local tail = utils.path_tail(path) + local path_style = { + { { 0, #transformed_path - #tail }, "Comment" }, + } + return transformed_path, path_style + end, } local current_config = default_config diff --git a/lua/telescope/_extensions/zoxide/list.lua b/lua/telescope/_extensions/zoxide/list.lua index 9e90918..4e0fe30 100644 --- a/lua/telescope/_extensions/zoxide/list.lua +++ b/lua/telescope/_extensions/zoxide/list.lua @@ -3,7 +3,9 @@ local action_state = require('telescope.actions.state') local finders = require('telescope.finders') local pickers = require('telescope.pickers') local sorters = require('telescope.sorters') +local entry_display = require('telescope.pickers.entry_display') local utils = require('telescope.utils') +local z_config = require("telescope._extensions.zoxide.config") local map_both = function(map, keys, func) map("i", keys, func) @@ -59,18 +61,52 @@ local fuzzy_with_z_score_bias = function(opts) } end -local entry_maker = function(item) - local trimmed = string.gsub(item, '^%s*(.-)%s*$', '%1') - local item_path = string.gsub(trimmed, '^[^%s]* (.*)$', '%1') - local score = tonumber(string.gsub(trimmed, '^([^%s]*) .*$', '%1'), 10) - - return { - value = item_path, - ordinal = item_path, - display = item_path, - z_score = score, - path = item_path - } +local entry_maker = function(opts) + opts = opts or {} + + local show_score = z_config.get_config().show_score + local score_width = show_score and 7 or 0 + + local display_items = { { remaining = true } } + if show_score then + table.insert(display_items, 1, { width = score_width, right_justify = true }) + end + + local displayer = entry_display.create({ + separator = " ", + items = display_items, + }) + + local make_display = function(entry) + opts.__prefix = score_width + local display_path, path_style = utils.transform_path(opts, entry.path) + local display_columns = { + { + display_path, + function() + return path_style + end, + }, + } + if show_score then + table.insert(display_columns, 1, { ("%.1f"):format(entry.z_score), "TelescopeResultsNumber" }) + end + return displayer(display_columns) + end + + return function(item) + local trimmed = string.gsub(item, '^%s*(.-)%s*$', '%1') + local item_path = string.gsub(trimmed, '^[^%s]* (.*)$', '%1') + local score = tonumber(string.gsub(trimmed, '^([^%s]*) .*$', '%1'), 10) + + return { + value = item_path, + ordinal = item_path, + display = make_display, + z_score = score, + path = item_path + } + end end local create_mapping = function(prompt_bufnr, mapping_config) @@ -94,13 +130,13 @@ end return function(opts) opts = opts or {} - local z_config = require("telescope._extensions.zoxide.config") local cmd = z_config.get_config().list_command local shell_arg = "-c" if vim.o.shell == "cmd.exe" then shell_arg = "/c" end opts.cmd = vim.F.if_nil(opts.cmd, {vim.o.shell, shell_arg, cmd}) + opts.path_display = vim.F.if_nil(opts.path_display, z_config.get_config().path_display) pickers.new(opts, { prompt_title = z_config.get_config().prompt_title, @@ -108,7 +144,7 @@ return function(opts) finder = finders.new_table { results = utils.get_os_command_output(opts.cmd), - entry_maker = entry_maker + entry_maker = entry_maker(opts) }, sorter = fuzzy_with_z_score_bias(opts), attach_mappings = function(prompt_bufnr, map)