8000 mini.nvim/lua/mini/tabline.lua at main · rafamadriz/mini.nvim · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
494 lines (413 loc) · 16.1 KB

tabline.lua

File metadata and controls

494 lines (413 loc) · 16.1 KB
return vim.tbl_flatten(vim.tbl_filter(function(x) return #x > 1 end, label_tab_ids))
end
-- Fit tabline to maximum displayed width -------------------------------------
H.fit_width = function()
H.update_center_buf_id()
-- Compute label width data
local center_offset = 1
local tot_width = 0
for _, tab in pairs(H.tabs) do
-- Use `nvim_strwidth()` and not `:len()` to respect multibyte characters
tab.label_width = vim.api.nvim_strwidth(tab.label)
tab.chars_on_left = tot_width
tot_width = tot_width + tab.label_width
if tab.buf_id == H.center_buf_id then
-- Make right end of 'center tab' to be always displayed in center in
-- case of truncation
center_offset = tot_width
end
end
local display_interval = H.compute_display_interval(center_offset, tot_width)
H.truncate_tabs_display(display_interval)
end
H.update_center_buf_id = function()
local cur_buf = vim.api.nvim_get_current_buf()
if H.is_buffer_in_minitabline(cur_buf) then H.center_buf_id = cur_buf end
end
H.compute_display_interval = function(center_offset, tabline_width)
-- left - first character to be displayed (starts with 1)
-- right - last character to be displayed
-- Conditions to be satisfied:
-- 1) right - left + 1 = math.min(tot_width, tabline_width)
-- 2) 1 <= left <= tabline_width; 1 <= right <= tabline_width
local tot_width = vim.o.columns - vim.api.nvim_strwidth(H.tabpage_section)
-- Usage of `math.floor` is crucial to avoid non-integer values which might
-- affect total width of output tabline string.
-- Using `floor` instead of `ceil` has effect when `tot_width` is odd:
-- - `floor` makes "true center" to be between second to last and last label
-- character (usually non-space and space).
-- - `ceil` - between last character of center label and first character of
-- next label (both whitespaces).
local right = math.min(tabline_width, math.floor(center_offset + 0.5 * tot_width))
local left = math.max(1, right - tot_width + 1)
right = left + math.min(tot_width, tabline_width) - 1
return { left, right }
end
H.truncate_tabs_display = function(display_interval)
local display_left, display_right = display_interval[1], display_interval[2]
local tabs = {}
for _, tab in ipairs(H.tabs) do
local tab_left = tab.chars_on_left + 1
local tab_right = tab.chars_on_left + tab.label_width
if (display_left <= tab_right) and (tab_left <= display_right) then
-- Process tab that should be displayed (even partially)
local n_trunc_left = math.max(0, display_left - tab_left)
local n_trunc_right = math.max(0, tab_right - display_right)
-- Take desired amount of characters starting from `n_trunc_left`
tab.label = vim.fn.strcharpart(tab.label, n_trunc_left, tab.label_width - n_trunc_right)
table.insert(tabs, tab)
end
end
H.tabs = tabs
end
-- Concatenate tabs into single tablien string --------------------------------
H.concat_tabs = function()
-- NOTE: it is assumed that all padding is incorporated into labels
local t = {}
for _, tab in ipairs(H.tabs) do
-- Escape '%' in labels
table.insert(t, ('%s%s%s'):format(tab.hl, tab.tabfunc, tab.label:gsub('%%', '%%%%')))
end
-- Usage of `%X` makes filled space to the right 'non-clickable'
local res = ('%s%%X%%#MiniTablineFill#'):format(table.concat(t, ''))
-- Add tabpage section
local position = H.get_config().tabpage_section
if H.tabpage_section ~= '' then
if position == 'left' then res = ('%%#MiniTablineTabpagesection#%s%s'):format(H.tabpage_section, res) end
if position == 'right' then
-- Use `%=` to make it stick to right hand side
res = ('%s%%=%%#MiniTablineTabpagesection#%s'):format(res, H.tabpage_section)
end
end
return res
end
return MiniTabline
0