[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

@ -12,7 +12,7 @@ local function gen_section(hl_string, items)
out = out .. " " .. item out = out .. " " .. item
end end
end end
if out ~="" then if out ~= "" then
return hl_string .. out .. " " return hl_string .. out .. " "
else else
return out return out
@ -22,56 +22,56 @@ end
-- sensible names for vim modes -- sensible names for vim modes
local function get_mode(m) local function get_mode(m)
local mode_group = { local mode_group = {
["n"] = "Normal", ["n"] = "Normal",
["no"] = "O-Pending", ["no"] = "O-Pending",
["nov"] = "O-Pending", ["nov"] = "O-Pending",
["noV"] = "O-Pending", ["noV"] = "O-Pending",
["no"] = "O-Pending", ["no"] = "O-Pending",
["niI"] = "Normal", ["niI"] = "Normal",
["niR"] = "Normal", ["niR"] = "Normal",
["niV"] = "Normal", ["niV"] = "Normal",
["v"] = "Visual", ["v"] = "Visual",
["V"] = "V-Line", ["V"] = "V-Line",
[""] = "V-Block", [""] = "V-Block",
["s"] = "Select", ["s"] = "Select",
["S"] = "S-Line", ["S"] = "S-Line",
[""] = "S-Block", [""] = "S-Block",
["i"] = "Insert", ["i"] = "Insert",
["ic"] = "Insert", ["ic"] = "Insert",
["ix"] = "Insert", ["ix"] = "Insert",
["R"] = "Replace", ["R"] = "Replace",
["Rc"] = "Replace", ["Rc"] = "Replace",
["Rv"] = "V-Replace", ["Rv"] = "V-Replace",
["Rx"] = "Replace", ["Rx"] = "Replace",
["c"] = "Command", ["c"] = "Command",
["cv"] = "Ex", ["cv"] = "Ex",
["ce"] = "Ex", ["ce"] = "Ex",
["r"] = "Prompt", ["r"] = "Prompt",
["rm"] = "Prompt", ["rm"] = "Prompt",
["r?"] = "Prompt", ["r?"] = "Prompt",
["!"] = "Shell", ["!"] = "Shell",
["t"] = "Terminal", ["t"] = "Terminal",
} }
return mode_group[m] or "None" return mode_group[m] or "None"
end end
local function get_mode_display_name(m) local function get_mode_display_name(m)
local mode = { local mode = {
["Normal"] = "[ NRM ]", ["Normal"] = "[ NRM ]",
["O-Pending"] = "[ OTR ]", ["O-Pending"] = "[ OTR ]",
["Visual"] = "[ VSL ]", ["Visual"] = "[ VSL ]",
["V-Line"] = "[ V·L ]", ["V-Line"] = "[ V·L ]",
["V-Block"] = "[ V·B ]", ["V-Block"] = "[ V·B ]",
["Select"] = "[ SEL ]", ["Select"] = "[ SEL ]",
["S-Line"] = "[ S·L ]", ["S-Line"] = "[ S·L ]",
["S-Block"] = "[ S·B ]", ["S-Block"] = "[ S·B ]",
["Insert"] = "[ INS ]", ["Insert"] = "[ INS ]",
["Replace"] = "[ RPL ]", ["Replace"] = "[ RPL ]",
["Command"] = "[ CMD ]", ["Command"] = "[ CMD ]",
["Prompt"] = "[ PMT ]", ["Prompt"] = "[ PMT ]",
["Shell"] = "[ SHL ]", ["Shell"] = "[ SHL ]",
["Terminal"] = "[ TRM ]", ["Terminal"] = "[ TRM ]",
["None"] = "[ --- ]" ["None"] = "[ --- ]"
} }
return mode[m] return mode[m]
end end
@ -79,25 +79,48 @@ end
-- get the highlight group for a mode group -- get the highlight group for a mode group
local function get_mode_color(m) local function get_mode_color(m)
local color = { local color = {
["Normal"] = "%#NormalMode#", ["Normal"] = "%#NormalMode#",
["O-Pending"] = "%#NormalMode#", ["O-Pending"] = "%#NormalMode#",
["Visual"] = "%#VisualMode#", ["Visual"] = "%#VisualMode#",
["V-Line"] = "%#VisualMode#", ["V-Line"] = "%#VisualMode#",
["V-Block"] = "%#VisualMode#", ["V-Block"] = "%#VisualMode#",
["Select"] = "%#NormalMode#", ["Select"] = "%#NormalMode#",
["S-Line"] = "%#NormalMode#", ["S-Line"] = "%#NormalMode#",
["S-Block"] = "%#NormalMode#", ["S-Block"] = "%#NormalMode#",
["Insert"] = "%#InsertMode#", ["Insert"] = "%#InsertMode#",
["Replace"] = "%#ReplaceMode#", ["Replace"] = "%#ReplaceMode#",
["Command"] = "%#CommandMode#", ["Command"] = "%#CommandMode#",
["Prompt"] = "%#NormalMode#", ["Prompt"] = "%#NormalMode#",
["Shell"] = "%#NormalMode#", ["Shell"] = "%#NormalMode#",
["Terminal"] = "%#NormalMode#", ["Terminal"] = "%#NormalMode#",
["None"] = "%#NormalMode#" ["None"] = "%#NormalMode#"
} }
return color[m] return color[m]
end 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 -- from https://github.com/nvim-lua/lsp-status.nvim/blob/master/lua/lsp-status/diagnostics.lua
local function get_lsp_diagnostics() local function get_lsp_diagnostics()
local result = {} local result = {}
@ -121,80 +144,82 @@ local function get_lsp_diagnostics()
end end
local function process_diagnostics(prefix, n, hl) local function process_diagnostics(prefix, n, hl)
if n > 0 then if n > 0 then
return hl .. prefix .. n return hl .. prefix .. n
else else
return "" return ""
end end
end end
local function spell_check() local function spell_check()
if vim.wo.spell then if vim.wo.spell then
local lang = vim.o.spelllang local lang = vim.o.spelllang
return "[SPELL=" .. lang .. "]" return "[SPELL=" .. lang .. "]"
else else
return "" return ""
end end
end end
local function statusline_focused() local function statusline_focused()
local diagnostics = get_lsp_diagnostics() local file = vim.fn.expand("%:p:~")
local mode = vim.fn.mode() local diagnostics = get_lsp_diagnostics()
local mg = get_mode(mode) local mode = vim.fn.mode()
local accent_color = get_mode_color(mg) local mg = get_mode(mode)
local accent_color = get_mode_color(mg)
local winwidth = vim.fn.winwidth(0)
local left = table.concat { local left = table.concat {
gen_section(accent_color, {get_mode_display_name(mg)}), 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("%#Bottom#", { "%m", "%r" }),
gen_section( gen_section(
"%#Alert#", "%#Alert#",
{ {
process_diagnostics( process_diagnostics(
globals.sign_error .. " ", globals.sign_error .. " ",
diagnostics.error, diagnostics.error,
"%#DiagnosticVirtualTextError#" "%#DiagnosticVirtualTextError#"
), ),
process_diagnostics( process_diagnostics(
globals.sign_warn .. " ", globals.sign_warn .. " ",
diagnostics.warn, diagnostics.warn,
"%#DiagnosticVirtualTextWarn#" "%#DiagnosticVirtualTextWarn#"
), ),
process_diagnostics( process_diagnostics(
globals.sign_info .. " ", globals.sign_info .. " ",
diagnostics.info, diagnostics.info,
"%#DiagnosticVirtualTextInfo#" "%#DiagnosticVirtualTextInfo#"
) )
} }
) )
} }
local right = table.concat { local right = table.concat {
gen_section( gen_section(
"%#Bottom#", "%#Bottom#",
{ {
spell_check(), spell_check(),
vim.bo.filetype vim.bo.filetype
} }
), ),
gen_section("%#Middle#", {"%03.p%%"}), gen_section("%#Middle#", { "%03.p%%" }),
gen_section("%#Top#", {"-%03.c-"}) gen_section("%#Top#", { "-%03.c-" })
} }
return table.concat { return table.concat {
left, left,
"%#Statusline#", "%#Statusline#",
"%=", "%=",
right right
} }
end end
local function statusline_not_focused() local function statusline_not_focused()
return table.concat { return table.concat {
gen_section("%#StatuslineNF#", {"%f", "%m"}), gen_section("%#StatuslineNF#", { "%f", "%m" }),
"%=", "%=",
gen_section("%#StatuslineNF#", {"%03.p%%"}), gen_section("%#StatuslineNF#", { "%03.p%%" }),
gen_section("%#StatuslineNF#", {"-%03.c-"}) gen_section("%#StatuslineNF#", { "-%03.c-" })
} }
end end