From 85b4b088243eaca134b643a1a31d2ce9d11366cc Mon Sep 17 00:00:00 2001 From: David JULIEN Date: Wed, 22 Feb 2023 19:34:24 +0100 Subject: [PATCH] [nvim] feat: replace path in statusline by "/.../" when useful --- .config/nvim/lua/statusline.lua | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/.config/nvim/lua/statusline.lua b/.config/nvim/lua/statusline.lua index 7bd8386..4abecb5 100644 --- a/.config/nvim/lua/statusline.lua +++ b/.config/nvim/lua/statusline.lua @@ -107,7 +107,13 @@ local function shorten_path(path, max_len) end local segments = vim.split(path, sep) - for idx = 1, #segments - 1 do + local start = 1 + if segments[1] == "~" then + start = start + 1 + end + + + for idx = start, #segments - 1 do if len <= max_len then break end @@ -122,6 +128,13 @@ local function shorten_path(path, max_len) len = len - (#segment - #shortened) end + if (len > max_len and #segments > 3) then + while (len > max_len and #segments > 3) do + table.remove(segments, 2) + end + table.insert(segments,2, "...") + end + return table.concat(segments, sep) end @@ -174,7 +187,7 @@ local function statusline_focused() local left = table.concat { gen_section(accent_color, { get_mode_display_name(mg) }), - gen_section("%#Middle#", { shorten_path(file, winwidth / 3) }), + gen_section("%#Middle#", { shorten_path(file, winwidth / 2) }), gen_section("%#Bottom#", { "%m", "%r" }), gen_section( "%#Alert#", @@ -223,7 +236,7 @@ local function statusline_not_focused() local winwidth = vim.fn.winwidth(0) return table.concat { gen_section("%#StatuslineNF#", { - shorten_path(file, winwidth / 3), + shorten_path(file, winwidth / 2), "%m" }), "%=",