diff options
| author | TJ DeVries <devries.timothyj@gmail.com> | 2020-09-28 17:03:59 -0400 |
|---|---|---|
| committer | TJ DeVries <devries.timothyj@gmail.com> | 2020-09-28 17:03:59 -0400 |
| commit | 3592b1f8b0ca207462bf945af52026e592afda30 (patch) | |
| tree | 9e522af7e92425e5050a47a8367043a0af844b56 /lua/telescope/path.lua | |
| parent | 079773ebee5f18f55c72856049c1a332dabe28e2 (diff) | |
fix: 126
Diffstat (limited to 'lua/telescope/path.lua')
| -rw-r--r-- | lua/telescope/path.lua | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/lua/telescope/path.lua b/lua/telescope/path.lua new file mode 100644 index 0000000..8fdbc71 --- /dev/null +++ b/lua/telescope/path.lua @@ -0,0 +1,60 @@ +local path = {} + +-- TODO: Can we use vim.loop for this? +path.separator = package.config:sub(1, 1) +path.home = vim.fn.expand("~") + + +path.make_relative = function(filepath, cwd) + if not cwd or not filepath then return filepath end + + if filepath:sub(1, #cwd) == cwd then + local offset = 0 + -- if cwd does ends in the os separator, we need to take it off + if cwd:sub(#cwd, #cwd) ~= path.separator then + offset = 1 + end + + filepath = filepath:sub(#cwd + 1 + offset, #filepath) + end + + 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)() + +path.normalize = function(filepath, cwd) + filepath = path.make_relative(filepath, cwd) + + -- Substitute home directory w/ "~" + filepath = filepath:gsub("^" .. path.home, '~', 1) + + -- Remove double path separators, it's annoying + filepath = filepath:gsub(path.separator .. path.separator, path.separator) + + return filepath +end + +return path |
