Skip to content

Commit

Permalink
Add length option for shorten_path
Browse files Browse the repository at this point in the history
  • Loading branch information
l-kershaw committed Jun 1, 2021
1 parent ec8cf12 commit be738ba
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 33 deletions.
16 changes: 8 additions & 8 deletions lua/telescope/make_entry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ do
local hl_group
local display = path.make_relative(entry.value, cwd)
if shorten_path then
display = utils.path_shorten(display)
display = utils.path_shorten(display,shorten_path)
end

display, hl_group = utils.transform_devicons(entry.value, display, disable_devicons)
Expand Down Expand Up @@ -196,7 +196,7 @@ do
display = function(entry)
local display_filename
if shorten_path then
display_filename = utils.path_shorten(entry.filename)
display_filename = utils.path_shorten(entry.filename,shorten_path)
else
display_filename = entry.filename
end
Expand Down Expand Up @@ -312,7 +312,7 @@ function make_entry.gen_from_quickfix(opts)
if opts.tail_path then
filename = utils.path_tail(filename)
elseif opts.shorten_path then
filename = utils.path_shorten(filename)
filename = utils.path_shorten(filename,opts.shorten_path)
end
end

Expand Down Expand Up @@ -391,7 +391,7 @@ function make_entry.gen_from_lsp_symbols(opts)
if opts.tail_path then
filename = utils.path_tail(filename)
elseif opts.shorten_path then
filename = utils.path_shorten(filename)
filename = utils.path_shorten(filename,opts.shorten_path)
end
end

Expand Down Expand Up @@ -469,7 +469,7 @@ function make_entry.gen_from_buffer(opts)
local make_display = function(entry)
local display_bufname
if opts.shorten_path then
display_bufname = path.shorten(entry.filename)
display_bufname = utils.path_shorten(entry.filename,opts.shorten_path)
else
display_bufname = entry.filename
end
Expand Down Expand Up @@ -875,7 +875,7 @@ function make_entry.gen_from_ctags(opts)
local filename
if not opts.hide_filename then
if opts.shorten_path then
filename = path.shorten(entry.filename)
filename = utils.path_shorten(entry.filename,opts.shorten_path)
else
filename = entry.filename
end
Expand Down Expand Up @@ -969,7 +969,7 @@ function make_entry.gen_from_lsp_diagnostics(opts)
if opts.tail_path then
filename = utils.path_tail(filename)
elseif opts.shorten_path then
filename = utils.path_shorten(filename)
filename = utils.path_shorten(filename,opts.shorten_path)
end
end

Expand Down Expand Up @@ -1173,7 +1173,7 @@ function make_entry.gen_from_jumplist(opts)
if opts.tail_path then
filename = utils.path_tail(filename)
elseif opts.shorten_path then
filename = utils.path_shorten(filename)
filename = utils.path_shorten(filename,opts.shorten_path)
end
end

Expand Down
29 changes: 6 additions & 23 deletions lua/telescope/path.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,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
14 changes: 12 additions & 2 deletions lua/telescope/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,17 @@ end

-- return result
-- end or nil)
utils.path_shorten = pathlib.shorten
utils.path_shorten = function(filename,len)
if len == nil then
return pathlib.shorten(filename)
elseif len == true then
return pathlib.shorten(filename,1)
elseif tonumber(len) > 0 then
return pathlib.shorten(filename,tonumber(len))
else
error('Invalid value for `len`. Acceptable values are `nil`, `true` and positive integers.')
end
end

utils.path_tail = (function()
local os_sep = utils.get_separator()
Expand Down Expand Up @@ -352,7 +362,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 be738ba

Please sign in to comment.