Skip to content

Commit

Permalink
Add 'path_prompt_prefix' option (#252)
Browse files Browse the repository at this point in the history
* Add 'path_prompt_prefix' option, defaulting to false, which if enabled
will update the prompt with the relative path from the CWD.

* Rename path_prompt_prefix to prompt_path so it fits in docs

* Changes to util:
- When absoute path for home folder is in path, substitute it with `~`
- Prefix relative path with `./` so that is not added/removed when
  navigating.

* Update docs

* Add field documentation to picker.lua instead of docs

* Linting
  • Loading branch information
CKolkey committed Mar 22, 2023
1 parent e591301 commit 1e75d58
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ require("telescope").setup {
-- browse_folders
hide_parent_dir = false,
collapse_dirs = false,
prompt_path = false,
quiet = false,
dir_icon = "",
dir_icon_hl = "Default",
Expand Down
20 changes: 16 additions & 4 deletions lua/telescope/_extensions/file_browser/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,10 @@ fb_actions.goto_parent_dir = function(prompt_bufnr, bypass)

finder.path = parent_dir
fb_utils.redraw_border_title(current_picker)
current_picker:refresh(finder, { reset_prompt = true, multi = current_picker._multi })
current_picker:refresh(
finder,
{ new_prefix = fb_utils.relative_path_prefix(finder), reset_prompt = true, multi = current_picker._multi }
)
end

--- Goto working directory of nvim in |telescope-file-browser.picker.file_browser|.
Expand All @@ -555,7 +558,10 @@ fb_actions.goto_cwd = function(prompt_bufnr)
finder.path = vim.loop.cwd()

fb_utils.redraw_border_title(current_picker)
current_picker:refresh(finder, { reset_prompt = true, multi = current_picker._multi })
current_picker:refresh(
finder,
{ new_prefix = fb_utils.relative_path_prefix(finder), reset_prompt = true, multi = current_picker._multi }
)
end

--- Change working directory of nvim to the selected file/folder in |telescope-file-browser.picker.file_browser|.
Expand All @@ -569,7 +575,10 @@ fb_actions.change_cwd = function(prompt_bufnr)
vim.cmd("cd " .. finder.path)

fb_utils.redraw_border_title(current_picker)
current_picker:refresh(finder, { reset_prompt = true, multi = current_picker._multi })
current_picker:refresh(
finder,
{ new_prefix = fb_utils.relative_path_prefix(finder), reset_prompt = true, multi = current_picker._multi }
)
fb_utils.notify(
"action.change_cwd",
{ msg = "Set the current working directory!", level = "INFO", quiet = finder.quiet }
Expand All @@ -584,7 +593,10 @@ fb_actions.goto_home_dir = function(prompt_bufnr)
finder.path = vim.loop.os_homedir()

fb_utils.redraw_border_title(current_picker)
current_picker:refresh(finder, { reset_prompt = true, multi = current_picker._multi })
current_picker:refresh(
finder,
{ new_prefix = fb_utils.relative_path_prefix(finder), reset_prompt = true, multi = current_picker._multi }
)
end

--- Toggle between file and folder browser for |telescope-file-browser.picker.file_browser|.
Expand Down
5 changes: 4 additions & 1 deletion lua/telescope/_extensions/file_browser/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ _TelescopeFileBrowserConfig = {
finder.files = true
finder.path = path
fb_utils.redraw_border_title(current_picker)
current_picker:refresh(finder, { reset_prompt = true, multi = current_picker._multi })
current_picker:refresh(
finder,
{ new_prefix = fb_utils.relative_path_prefix(finder), reset_prompt = true, multi = current_picker._multi }
)
end)
return true
end,
Expand Down
1 change: 1 addition & 0 deletions lua/telescope/_extensions/file_browser/finders.lua
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ fb_finders.finder = function(opts)
end,
prompt_title = opts.custom_prompt_title,
results_title = opts.custom_results_title,
prompt_path = opts.prompt_path,
use_fd = vim.F.if_nil(opts.use_fd, true),
}, {
__call = function(self, ...)
Expand Down
3 changes: 3 additions & 0 deletions lua/telescope/_extensions/file_browser/picker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ local fb_picker = {}
---@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)
---@field prompt_path boolean: Show the current relative path from cwd as the prompt prefix. (default: false)
fb_picker.file_browser = function(opts)
opts = opts or {}

Expand All @@ -91,6 +92,7 @@ fb_picker.file_browser = function(opts)
opts.custom_results_title = opts.results_title ~= nil
opts.use_fd = vim.F.if_nil(opts.use_fd, true)
opts.git_status = vim.F.if_nil(opts.git_status, vim.fn.executable "git" == 1)
opts.prompt_path = vim.F.if_nil(opts.prompt_path, false)

local select_buffer = opts.select_buffer and opts.files
-- handle case that current buffer is a hidden file
Expand All @@ -115,6 +117,7 @@ fb_picker.file_browser = function(opts)
.new(opts, {
prompt_title = opts.files and "File Browser" or "Folder Browser",
results_title = Path:new(opts.path):make_relative(cwd) .. os_sep,
prompt_prefix = fb_utils.relative_path_prefix(opts.finder),
previewer = conf.file_previewer(opts),
sorter = conf.file_sorter(opts),
})
Expand Down
14 changes: 14 additions & 0 deletions lua/telescope/_extensions/file_browser/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,20 @@ fb_utils.redraw_border_title = function(current_picker)
end
end

fb_utils.relative_path_prefix = function(finder)
local prefix
if finder.prompt_path then
local path, _ = Path:new(finder.path):make_relative(finder.cwd):gsub(vim.fn.expand "~", "~")
if path:match "^%w" then
prefix = "./" .. path .. os_sep
else
prefix = path .. os_sep
end
end

return prefix
end

fb_utils.group_by_type = function(tbl)
table.sort(tbl, function(x, y)
local x_stat = vim.loop.fs_stat(x)
Expand Down

0 comments on commit 1e75d58

Please sign in to comment.