Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add path_display support with a default value #24

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,36 @@ vim.keymap.set("n", "<leader>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())
local len = vim.api.nvim_win_get_width(status.layout.results.winid) - 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,
}
```

Expand Down
34 changes: 33 additions & 1 deletion lua/telescope/_extensions/zoxide/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -32,7 +35,36 @@ 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())
local len = vim.api.nvim_win_get_width(status.layout.results.winid) - 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
Expand Down
64 changes: 50 additions & 14 deletions lua/telescope/_extensions/zoxide/list.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -94,21 +130,21 @@ 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,
previewer = require("telescope.previewers.buffer_previewer").cat.new(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)
Expand Down