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 length option for shorten_path #886

Merged
merged 5 commits into from
Jul 16, 2021
Merged
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
9 changes: 9 additions & 0 deletions doc/telescope.txt
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,15 @@ telescope.setup({opts}) *telescope.setup()*
- "shorten" only display the first character of each directory in
the path

You can also specify the number of characters of each directory name
to keep by setting `path_display.shorten = num`.
e.g. for a path like
`alpha/beta/gamma/delta.txt`
setting `path_display.shorten = 1` will give a path like:
`a/b/g/delta.txt`
Similarly, `path_display.shorten = 2` will give a path like:
`al/be/ga/delta.txt`

path_display can also be set to 'hidden' string to hide file names

path_display can also be set to a function for custom formatting of
Expand Down
10 changes: 10 additions & 0 deletions lua/telescope/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,15 @@ local telescope_defaults = {
- "shorten" only display the first character of each directory in
the path

You can also specify the number of characters of each directory name
to keep by setting `path_display.shorten = num`.
e.g. for a path like
`alpha/beta/gamma/delta.txt`
setting `path_display.shorten = 1` will give a path like:
`a/b/g/delta.txt`
Similarly, `path_display.shorten = 2` will give a path like:
`al/be/ga/delta.txt`

path_display can also be set to 'hidden' string to hide file names

path_display can also be set to a function for custom formatting of
Expand All @@ -202,6 +211,7 @@ local telescope_defaults = {
Default: {}]]
},


borderchars = { { "─", "│", "─", "│", "╭", "╮", "╯", "╰" } },

get_status_text = {
Expand Down
19 changes: 11 additions & 8 deletions lua/telescope/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ local has_devicons, devicons = pcall(require, 'nvim-web-devicons')
local Path = require('plenary.path')
local Job = require('plenary.job')

local log = require('telescope.log')

local utils = {}

utils.get_separator = function()
Expand Down Expand Up @@ -251,8 +253,9 @@ utils.diagnostics_to_tbl = function(opts)
return items
end

utils.path_shorten = function(file)
return Path:new(file):shorten()
utils.path_shorten = function(filename,len)
log.warn("`utils.path_shorten` is deprecated. Use `require('plenary.path').shorten`.")
return Path:new(filename):shorten(len)
end

utils.path_tail = (function()
Expand All @@ -268,7 +271,7 @@ utils.is_path_hidden = function(opts, path_display)
path_display = path_display or utils.get_default(opts.path_display, require('telescope.config').values.path_display)

return path_display == nil or path_display == "hidden" or
type(path_display) ~= "table" or vim.tbl_contains(path_display, "hidden")
type(path_display) ~= "table" or vim.tbl_contains(path_display, "hidden") or path_display.hidden
end

utils.transform_path = function(opts, path)
Expand All @@ -284,10 +287,10 @@ utils.transform_path = function(opts, path)
return ''
end

if vim.tbl_contains(path_display, "tail") then
if vim.tbl_contains(path_display, "tail") or path_display.tail then
transformed_path = utils.path_tail(transformed_path)
else
if not vim.tbl_contains(path_display, "absolute") then
if not vim.tbl_contains(path_display, "absolute") or path_display.absolute == false then
local cwd
if opts.cwd then
cwd = opts.cwd
Expand All @@ -300,8 +303,8 @@ utils.transform_path = function(opts, path)
transformed_path = Path:new(transformed_path):make_relative(cwd)
end

if vim.tbl_contains(path_display, "shorten") then
transformed_path = Path:new(transformed_path):shorten()
if vim.tbl_contains(path_display, "shorten") or path_display["shorten"] ~= nil then
transformed_path = Path:new(transformed_path):shorten(path_display["shorten"])
end
end

Expand Down Expand Up @@ -439,7 +442,7 @@ utils.transform_devicons = (function()
end

local icon, icon_highlight = devicons.get_icon(filename, string.match(filename, '%a+$'), { default = true })
local icon_display = (icon or ' ') .. ' ' .. display
local icon_display = (icon or ' ') .. ' ' .. (display or '')

if conf.color_devicons then
return icon_display, icon_highlight
Expand Down