diff options
| author | Thomas Vanbesien <tvanbesi@proton.me> | 2026-06-24 17:03:10 +0200 |
|---|---|---|
| committer | Thomas Vanbesien <tvanbesi@proton.me> | 2026-06-24 17:03:10 +0200 |
| commit | 9e6c8209aa7ebe01c258c5a82177f116f22e55ed (patch) | |
| tree | d167a1e406efdef5c3b94cb76642a8201e0421a2 /plugin | |
| parent | 42233850cef2e1b30dc6524e056f604c3b5d021b (diff) | |
| download | nvim-config-9e6c8209aa7ebe01c258c5a82177f116f22e55ed.tar.gz nvim-config-9e6c8209aa7ebe01c258c5a82177f116f22e55ed.zip | |
refactor(nvim): split GetTabLine into multiple functions
Diffstat (limited to 'plugin')
| -rw-r--r-- | plugin/50-highlight.lua | 60 |
1 files changed, 35 insertions, 25 deletions
diff --git a/plugin/50-highlight.lua b/plugin/50-highlight.lua index a0624ba..448eb6e 100644 --- a/plugin/50-highlight.lua +++ b/plugin/50-highlight.lua @@ -62,12 +62,41 @@ local function create_tabpage_title_hl() ) end +-- Builds the window-count segment of a tabpage label, e.g. ` (2)[+] ` (no styling). +-- `is_modified` appends the `[+]` marker when any window in the tab has pending changes. +local function make_tab_info_label(win_count, is_modified) + local tab_win_count = "(" .. (win_count >= 10 and ">9" or tostring(win_count)) .. ")" + local tab_win_count_suffix = is_modified and "[+] " or " " + return " " .. tab_win_count .. tab_win_count_suffix +end + +-- Builds the title segment of a tabpage label, padded or truncated to `width` (no styling). +-- Uses the tab's `tabname` if set, else the active buffer's file name, else `[No Name]`, prefixed +-- with `*` for the current tab and `-` for the last-accessed tab. +local function make_tab_label(id, is_current, width) + local is_last_accessed = vim.api.nvim_tabpage_get_number(id) == vim.fn.tabpagenr("#") + local tab_label_prefix = (is_current and "*" or "") .. (is_last_accessed and "-" or "") + local tab_label = vim.t[id].tabname + if tab_label == nil or tab_label == "" then + local win = vim.api.nvim_tabpage_get_win(id) + local bufname = vim.api.nvim_buf_get_name(vim.api.nvim_win_get_buf(win)) + tab_label = vim.fn.fnamemodify(bufname, ":t:r") + end + if tab_label == "" then + tab_label = "[No Name]" + end + tab_label = tab_label_prefix .. tab_label + local display_width = vim.fn.strdisplaywidth(tab_label) + return (display_width > width) + and (vim.fn.strcharpart(tab_label, 0, width - 1) .. "…") + or (tab_label .. string.rep(" ", width - display_width)) +end + -- Returns a string compatible with the 'tabline' option, used to set the tabline content and style. -- Declared as a global function to be available as `v:lua.GetTabLine` in the Vimscript engine for -- the 'tabline' option. See `:help 'statusline'`. --- Each tabpage title is set to show the number of windows, a `[+]` if any of them has pending --- changes, and the tabpage title (current tabpage is emphasized). function GetTabLine() + local max_tab_label_width = 30 local s = "" for _, id in pairs(vim.api.nvim_list_tabpages()) do local is_current = id == vim.api.nvim_get_current_tabpage() @@ -83,30 +112,11 @@ function GetTabLine() -- Define start of tabpage label (marks where clickage region begins) s = s .. "%" .. vim.api.nvim_tabpage_get_number(id) .. "T" - local tab_win_count = "(" .. (#wins >= 10 and ">9" or tostring(#wins)) .. ")" - local tab_win_count_suffix = is_modified and "[+] " or " " - local tab_info_label = " " .. tab_win_count .. tab_win_count_suffix - local tab_info_label_hl = (is_current and "%#TabLineSelWinCount#" or "%#TabLineWinCount#") - s = s .. tab_info_label_hl .. tab_info_label - - local max_tab_label_width = 30 + local tab_info_label = make_tab_info_label(#wins, is_modified) local tab_label_width = max_tab_label_width - vim.fn.strdisplaywidth(tab_info_label) - local is_last_accessed = vim.api.nvim_tabpage_get_number(id) == vim.fn.tabpagenr("#") - local tab_label_prefix = (is_current and "*" or "") .. (is_last_accessed and "-" or "") - local tab_label = vim.t[id].tabname - if tab_label == nil or tab_label == "" then - local win = vim.api.nvim_tabpage_get_win(id) - local bufname = vim.api.nvim_buf_get_name(vim.api.nvim_win_get_buf(win)) - tab_label = vim.fn.fnamemodify(bufname, ":t:r") - end - if tab_label == "" then - tab_label = "[No Name]" - end - tab_label = tab_label_prefix .. tab_label - local display_width = vim.fn.strdisplaywidth(tab_label) - tab_label = (display_width > tab_label_width) - and (vim.fn.strcharpart(tab_label, 0, tab_label_width - 1) .. "…") - or (tab_label .. string.rep(" ", tab_label_width - display_width)) + local tab_label = make_tab_label(id, is_current, tab_label_width) + + s = s .. (is_current and "%#TabLineSelWinCount#" or "%#TabLineWinCount#") .. tab_info_label s = s .. (is_current and "%#TabLineSel#" or "%#TabLine#") .. tab_label end return s .. "%#TabLineFill#" |
