Skip to content

Commit

Permalink
feat: add shorten_len option for path shortening
Browse files Browse the repository at this point in the history
- adds option to configure the length of shortened parts of filenames
- only affects paths when "shorten" is in `path_display`
- reimplemented based on changes in nvim-telescope#473 and nvim-telescope#839
  • Loading branch information
l-kershaw committed Jul 16, 2021
1 parent 1386979 commit 6e21e95
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 27 deletions.
15 changes: 15 additions & 0 deletions lua/telescope/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,21 @@ local telescope_defaults = {
Default: {}]]
},

shorten_len = { 1, [[
Determines the length of the shortened portions of a filepath.
e.g. for a path like
`alpha/beta/gamma/delta.txt`
setting `shorten_len = 1` will give a path like:
`a/b/g/delta.txt`
when "shorten" is present in `path_display`.
Similarly, `shorten_len = 2` will give a path like:
`al/be/ga/delta.txt`
when "shorten" is present in `path_display`.
Default: 1]]
},

borderchars = { { "", "", "", "", "", "", "", "" } },

get_status_text = {
Expand Down
29 changes: 6 additions & 23 deletions lua/telescope/path.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,12 @@ path.make_relative = function(filepath, cwd)
return filepath
end

path.shorten = (function()
if jit then
local ffi = require('ffi')
ffi.cdef [[
typedef unsigned char char_u;
char_u *shorten_dir(char_u *str);
]]

return function(filepath)
if not filepath then
return filepath
end

local c_str = ffi.new("char[?]", #filepath + 1)
ffi.copy(c_str, filepath)
return ffi.string(ffi.C.shorten_dir(c_str))
end
else
return function(filepath)
return filepath
end
end
end)()
-- In most cases it is better to use `utils.path_shorten`
-- as it handles cases for `len` being things other than
-- a positive integer.
path.shorten = function(filepath,len)
return require'plenary.path'.new(filepath):shorten(len)
end

path.normalize = function(filepath, cwd)
filepath = path.make_relative(filepath, cwd)
Expand Down
15 changes: 11 additions & 4 deletions lua/telescope/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,14 @@ 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)
if len == nil or len == true then
return Path:new(filename):shorten(1)
elseif tonumber(len) > 0 then
return Path:new(filename):shorten(tonumber(len))
else
error('Invalid value for `len`. Acceptable values are `nil`, `true` and positive integers.')
end
end

utils.path_tail = (function()
Expand All @@ -273,6 +279,7 @@ end

utils.transform_path = function(opts, path)
local path_display = utils.get_default(opts.path_display, require('telescope.config').values.path_display)
local shorten_len = utils.get_default(opts.shorten_len, require('telescope.config').values.shorten_len)

local transformed_path = path

Expand Down Expand Up @@ -301,7 +308,7 @@ utils.transform_path = function(opts, path)
end

if vim.tbl_contains(path_display, "shorten") then
transformed_path = Path:new(transformed_path):shorten()
transformed_path = utils.path_shorten(transformed_path, shorten_len)
end
end

Expand Down Expand Up @@ -439,7 +446,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

0 comments on commit 6e21e95

Please sign in to comment.