From 030819d0f2a5c64d6b6701704fdc1d10254eb60a Mon Sep 17 00:00:00 2001 From: Turiiya <34311583+ttytm@users.noreply.github.com> Date: Sun, 24 Mar 2024 14:15:05 +0100 Subject: [PATCH] utils: rewrite `trunc_path` to simplify, fix and increase robustness --- lua/nxvim/utils.lua | 50 ++++++++------------------------------------- 1 file changed, 9 insertions(+), 41 deletions(-) diff --git a/lua/nxvim/utils.lua b/lua/nxvim/utils.lua index 9e76650..534bdcd 100644 --- a/lua/nxvim/utils.lua +++ b/lua/nxvim/utils.lua @@ -25,55 +25,23 @@ function M.truc_path(input_path) if string.match(input_path, home_path) then input_path = input_path:gsub(home_path, "~") end local config = { - max_dirs = 2, - shorten = true, + max_dirs = 3, prefix = "…", -- "󰘍" "" trunc_symbol = "…", } - local file_sep = package.config:sub(1, 1) - local file = vim.split(input_path, file_sep) - local path = "" + local path = vim.split(input_path, "/") + local res = path[1] - for dirs, _ in ipairs(file) do - if dirs <= config.max_dirs then - if config.max_dirs ~= 1 and dirs == 1 then - -- first level files (directly in project root) - if file[#file - config.max_dirs] == nil then - path = path .. file[#file - (#file - 1)] - else - path = path .. file[#file - config.max_dirs] - end - else - -- tip for debugging: use somthing like *test* inside "/" - if file[#file - dirs] == nil then - path = path .. "/" .. file[#file - (#file - dirs)] - else - -- middle position - path = path - .. "/" - .. ( - ( - config.shorten - and string.sub(file[#file - (config.max_dirs - (dirs - 1))], 1, 2) - .. config.trunc_symbol - ) or file[#file - (config.max_dirs - (dirs - 1))] - ) - end - end - else - -- actual filename - if file[#file - dirs] == nil then - path = path .. "/" .. file[#file] - break - else - path = config.prefix .. path .. "/" .. file[#file] - break - end + for i, _ in ipairs(path) do + if i + 1 <= config.max_dirs and i + 1 < #path then + res = res .. "/" .. ((string.sub(path[i + 1], 1, 2) .. config.trunc_symbol) or path[i + 1]) end end - return path + res = res .. "/" .. path[#path] + + return res end -- Auto window height - use for qf lists