2021-10-10 18:04:55 +00:00
|
|
|
local Tab = require('lualine.utils.class'):extend()
|
|
|
|
|
2022-02-15 17:17:05 +00:00
|
|
|
local modules = require('lualine_require').lazy_require {
|
|
|
|
highlight = 'lualine.highlight',
|
|
|
|
utils = 'lualine.utils.utils',
|
|
|
|
}
|
|
|
|
|
2022-05-30 14:25:05 +00:00
|
|
|
---initialize a new tab from opts
|
2021-10-12 14:04:47 +00:00
|
|
|
---@param opts table
|
2021-10-10 18:04:55 +00:00
|
|
|
function Tab:init(opts)
|
|
|
|
assert(opts.tabnr, 'Cannot create Tab without tabnr')
|
|
|
|
self.tabnr = opts.tabnr
|
2022-02-07 16:13:42 +00:00
|
|
|
self.tabId = opts.tabId
|
2021-10-10 18:04:55 +00:00
|
|
|
self.options = opts.options
|
|
|
|
self.highlights = opts.highlights
|
2023-10-17 09:37:24 +00:00
|
|
|
self.modified_icon = ''
|
|
|
|
self:get_props()
|
|
|
|
end
|
|
|
|
|
|
|
|
function Tab:get_props()
|
|
|
|
local buflist = vim.fn.tabpagebuflist(self.tabnr)
|
|
|
|
local winnr = vim.fn.tabpagewinnr(self.tabnr)
|
|
|
|
local bufnr = buflist[winnr]
|
|
|
|
self.file = modules.utils.stl_escape(vim.api.nvim_buf_get_name(bufnr))
|
|
|
|
self.filetype = vim.api.nvim_buf_get_option(bufnr, 'filetype')
|
|
|
|
self.buftype = vim.api.nvim_buf_get_option(bufnr, 'buftype')
|
|
|
|
|
|
|
|
if self.options.show_modified_status then
|
|
|
|
for _, b in ipairs(buflist) do
|
|
|
|
if vim.api.nvim_buf_get_option(b, 'modified') then
|
|
|
|
self.modified_icon = self.options.symbols.modified or ''
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-10 18:04:55 +00:00
|
|
|
end
|
|
|
|
|
2022-05-30 14:25:05 +00:00
|
|
|
---returns name for tab. Tabs name is the name of buffer in last active window
|
2021-10-12 14:04:47 +00:00
|
|
|
--- of the tab.
|
|
|
|
---@return string
|
2021-10-10 18:04:55 +00:00
|
|
|
function Tab:label()
|
2022-03-05 13:41:08 +00:00
|
|
|
local ok, custom_tabname = pcall(vim.api.nvim_tabpage_get_var, self.tabId, 'tabname')
|
|
|
|
if not ok then
|
|
|
|
custom_tabname = nil
|
|
|
|
end
|
2022-02-07 15:26:25 +00:00
|
|
|
if custom_tabname and custom_tabname ~= '' then
|
2022-02-15 17:17:05 +00:00
|
|
|
return modules.utils.stl_escape(custom_tabname)
|
2022-02-07 15:26:25 +00:00
|
|
|
end
|
2023-10-17 09:37:24 +00:00
|
|
|
if self.filetype == 'fugitive' then
|
|
|
|
return 'fugitive: ' .. vim.fn.fnamemodify(self.file, ':h:h:t')
|
|
|
|
elseif self.buftype == 'help' then
|
|
|
|
return 'help:' .. vim.fn.fnamemodify(self.file, ':t:r')
|
|
|
|
elseif self.buftype == 'terminal' then
|
|
|
|
local match = string.match(vim.split(self.file, ' ')[1], 'term:.*:(%a+)')
|
2021-10-10 18:04:55 +00:00
|
|
|
return match ~= nil and match or vim.fn.fnamemodify(vim.env.SHELL, ':t')
|
2023-10-17 09:37:24 +00:00
|
|
|
elseif self.file == '' then
|
2021-10-10 18:04:55 +00:00
|
|
|
return '[No Name]'
|
|
|
|
end
|
2023-10-17 09:37:24 +00:00
|
|
|
if self.options.path == 1 then
|
|
|
|
return vim.fn.fnamemodify(self.file, ':~:.')
|
|
|
|
elseif self.options.path == 2 then
|
|
|
|
return vim.fn.fnamemodify(self.file, ':p')
|
|
|
|
elseif self.options.path == 3 then
|
|
|
|
return vim.fn.fnamemodify(self.file, ':p:~')
|
|
|
|
else
|
|
|
|
return vim.fn.fnamemodify(self.file, ':t')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
---shortens path by turning apple/orange -> a/orange
|
|
|
|
---@param path string
|
|
|
|
---@param sep string path separator
|
|
|
|
---@param max_len integer maximum length of the full filename string
|
|
|
|
---@return string
|
|
|
|
local function shorten_path(path, sep, max_len)
|
|
|
|
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)
|
2021-10-10 18:04:55 +00:00
|
|
|
end
|
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
---returns rendered tab
|
|
|
|
---@return string
|
2021-10-10 18:04:55 +00:00
|
|
|
function Tab:render()
|
2021-11-18 02:12:33 +00:00
|
|
|
local name = self:label()
|
2023-10-17 09:37:24 +00:00
|
|
|
if self.options.tab_max_length ~= 0 then
|
|
|
|
local path_separator = package.config:sub(1, 1)
|
|
|
|
name = shorten_path(name, path_separator, self.options.tab_max_length)
|
|
|
|
end
|
2021-11-18 02:12:33 +00:00
|
|
|
if self.options.fmt then
|
2022-12-22 13:14:34 +00:00
|
|
|
name = self.options.fmt(name or '', self)
|
2021-11-18 02:12:33 +00:00
|
|
|
end
|
2022-05-30 14:25:05 +00:00
|
|
|
if self.ellipse then -- show ellipsis
|
2021-10-10 18:04:55 +00:00
|
|
|
name = '...'
|
|
|
|
else
|
2021-10-12 14:04:47 +00:00
|
|
|
-- different formats for different modes
|
2021-10-10 18:04:55 +00:00
|
|
|
if self.options.mode == 0 then
|
2021-11-18 02:12:33 +00:00
|
|
|
name = tostring(self.tabnr)
|
2023-10-17 09:37:24 +00:00
|
|
|
if self.modified_icon ~= '' then
|
|
|
|
name = string.format('%s%s', name, self.modified_icon)
|
|
|
|
end
|
2021-10-10 18:04:55 +00:00
|
|
|
elseif self.options.mode == 1 then
|
2023-10-17 09:37:24 +00:00
|
|
|
if self.modified_icon ~= '' then
|
|
|
|
name = string.format('%s %s', self.modified_icon, name)
|
|
|
|
end
|
2021-10-10 18:04:55 +00:00
|
|
|
else
|
2023-10-17 09:37:24 +00:00
|
|
|
name = string.format('%s%s %s', tostring(self.tabnr), self.modified_icon, name)
|
2021-10-10 18:04:55 +00:00
|
|
|
end
|
|
|
|
end
|
2023-10-17 09:37:24 +00:00
|
|
|
|
2021-11-18 02:12:33 +00:00
|
|
|
name = Tab.apply_padding(name, self.options.padding)
|
2021-10-12 14:04:47 +00:00
|
|
|
self.len = vim.fn.strchars(name)
|
|
|
|
|
|
|
|
-- setup for mouse clicks
|
2021-10-10 18:04:55 +00:00
|
|
|
local line = string.format('%%%s@LualineSwitchTab@%s%%T', self.tabnr, name)
|
2021-10-12 14:04:47 +00:00
|
|
|
-- apply highlight
|
2022-02-15 17:17:05 +00:00
|
|
|
line = modules.highlight.component_format_highlight(self.highlights[(self.current and 'active' or 'inactive')])
|
|
|
|
.. line
|
2021-10-10 18:04:55 +00:00
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
-- apply separators
|
2022-03-02 13:37:08 +00:00
|
|
|
if self.options.self.section < 'x' and not self.first then
|
2021-10-10 18:04:55 +00:00
|
|
|
local sep_before = self:separator_before()
|
|
|
|
line = sep_before .. line
|
|
|
|
self.len = self.len + vim.fn.strchars(sep_before)
|
2022-03-02 13:37:08 +00:00
|
|
|
elseif self.options.self.section >= 'x' and not self.last then
|
2021-10-10 18:04:55 +00:00
|
|
|
local sep_after = self:separator_after()
|
|
|
|
line = line .. sep_after
|
|
|
|
self.len = self.len + vim.fn.strchars(sep_after)
|
|
|
|
end
|
|
|
|
return line
|
|
|
|
end
|
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
---apply separator before current tab
|
|
|
|
---@return string
|
2021-10-10 18:04:55 +00:00
|
|
|
function Tab:separator_before()
|
|
|
|
if self.current or self.aftercurrent then
|
2023-04-09 09:48:56 +00:00
|
|
|
return '%Z{' .. self.options.section_separators.left .. '}'
|
2021-10-10 18:04:55 +00:00
|
|
|
else
|
|
|
|
return self.options.component_separators.left
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
---apply separator after current tab
|
|
|
|
---@return string
|
2021-10-10 18:04:55 +00:00
|
|
|
function Tab:separator_after()
|
|
|
|
if self.current or self.beforecurrent then
|
2023-04-09 09:48:56 +00:00
|
|
|
return '%z{' .. self.options.section_separators.right .. '}'
|
2021-10-10 18:04:55 +00:00
|
|
|
else
|
|
|
|
return self.options.component_separators.right
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-18 02:12:33 +00:00
|
|
|
---adds spaces to left and right
|
|
|
|
function Tab.apply_padding(str, padding)
|
|
|
|
local l_padding, r_padding = 1, 1
|
|
|
|
if type(padding) == 'number' then
|
|
|
|
l_padding, r_padding = padding, padding
|
|
|
|
elseif type(padding) == 'table' then
|
|
|
|
l_padding, r_padding = padding.left or 0, padding.right or 0
|
|
|
|
end
|
|
|
|
return string.rep(' ', l_padding) .. str .. string.rep(' ', r_padding)
|
|
|
|
end
|
|
|
|
|
2021-10-10 18:04:55 +00:00
|
|
|
return Tab
|