[nvim] feat: shorten path in statusline if needed

This commit is contained in:
David JULIEN 2023-02-12 22:30:25 +01:00
parent 475ed9bb31
commit 786d119e5c
1 changed files with 145 additions and 120 deletions

View File

@ -98,6 +98,29 @@ local function get_mode_color(m)
return color[m]
end
-- from https://github.com/nvim-lualine/lualine.nvim/blob/master/lua/lualine/components/filename.lua
local function shorten_path(path, max_len)
local sep = '/'
local len = #path
if len <= max_len then
return path
end
local segments = vim.split(path, sep)
for idx = 1, #segments - 1 do
if len <= max_len then
break
end
local segment = segments[idx]
local shortened = segment:sub(1, vim.startswith(segment, '.') and 2 or 1)
segments[idx] = shortened
len = len - (#segment - #shortened)
end
return table.concat(segments, sep)
end
-- from https://github.com/nvim-lua/lsp-status.nvim/blob/master/lua/lsp-status/diagnostics.lua
local function get_lsp_diagnostics()
local result = {}
@ -138,14 +161,16 @@ end
end
local function statusline_focused()
local file = vim.fn.expand("%:p:~")
local diagnostics = get_lsp_diagnostics()
local mode = vim.fn.mode()
local mg = get_mode(mode)
local accent_color = get_mode_color(mg)
local winwidth = vim.fn.winwidth(0)
local left = table.concat {
gen_section(accent_color, { get_mode_display_name(mg) }),
gen_section("%#Middle#", {"%f"}),
gen_section("%#Middle#", { shorten_path(file, winwidth / 3) }),
gen_section("%#Bottom#", { "%m", "%r" }),
gen_section(
"%#Alert#",