2021-10-10 18:04:55 +00:00
|
|
|
local Buffer = 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 buffer from opts
|
2021-10-12 14:04:47 +00:00
|
|
|
---@param opts table
|
2021-10-10 18:04:55 +00:00
|
|
|
function Buffer:init(opts)
|
|
|
|
assert(opts.bufnr, 'Cannot create Buffer without bufnr')
|
|
|
|
self.bufnr = opts.bufnr
|
2022-04-13 05:01:42 +00:00
|
|
|
self.buf_index = opts.buf_index
|
2021-10-10 18:04:55 +00:00
|
|
|
self.options = opts.options
|
|
|
|
self.highlights = opts.highlights
|
|
|
|
self:get_props()
|
|
|
|
end
|
|
|
|
|
2022-03-26 13:31:40 +00:00
|
|
|
function Buffer:is_current()
|
|
|
|
return vim.api.nvim_get_current_buf() == self.bufnr
|
|
|
|
end
|
|
|
|
|
2022-05-20 00:48:18 +00:00
|
|
|
function Buffer:is_alternate()
|
|
|
|
return vim.fn.bufnr('#') == self.bufnr and not self:is_current()
|
|
|
|
end
|
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
---setup icons, modified status for buffer
|
2021-10-10 18:04:55 +00:00
|
|
|
function Buffer:get_props()
|
2022-02-15 17:17:05 +00:00
|
|
|
self.file = modules.utils.stl_escape(vim.api.nvim_buf_get_name(self.bufnr))
|
2021-10-10 18:04:55 +00:00
|
|
|
self.buftype = vim.api.nvim_buf_get_option(self.bufnr, 'buftype')
|
|
|
|
self.filetype = vim.api.nvim_buf_get_option(self.bufnr, 'filetype')
|
|
|
|
local modified = self.options.show_modified_status and vim.api.nvim_buf_get_option(self.bufnr, 'modified')
|
2022-05-20 00:48:18 +00:00
|
|
|
self.modified_icon = modified and self.options.symbols.modified or ''
|
|
|
|
self.alternate_file_icon = self:is_alternate() and self.options.symbols.alternate_file or ''
|
2021-10-10 18:04:55 +00:00
|
|
|
self.icon = ''
|
|
|
|
if self.options.icons_enabled then
|
|
|
|
local dev
|
|
|
|
local status, _ = pcall(require, 'nvim-web-devicons')
|
|
|
|
if not status then
|
|
|
|
dev, _ = '', ''
|
|
|
|
elseif self.filetype == 'TelescopePrompt' then
|
2022-01-02 11:38:39 +00:00
|
|
|
dev, _ = require('nvim-web-devicons').get_icon('telescope')
|
2021-10-10 18:04:55 +00:00
|
|
|
elseif self.filetype == 'fugitive' then
|
2022-01-02 11:38:39 +00:00
|
|
|
dev, _ = require('nvim-web-devicons').get_icon('git')
|
2021-10-10 18:04:55 +00:00
|
|
|
elseif self.filetype == 'vimwiki' then
|
2022-01-02 11:38:39 +00:00
|
|
|
dev, _ = require('nvim-web-devicons').get_icon('markdown')
|
2021-10-10 18:04:55 +00:00
|
|
|
elseif self.buftype == 'terminal' then
|
2022-01-02 11:38:39 +00:00
|
|
|
dev, _ = require('nvim-web-devicons').get_icon('zsh')
|
2021-10-10 18:04:55 +00:00
|
|
|
elseif vim.fn.isdirectory(self.file) == 1 then
|
2022-05-20 00:48:18 +00:00
|
|
|
dev, _ = self.options.symbols.directory, nil
|
2021-10-10 18:04:55 +00:00
|
|
|
else
|
|
|
|
dev, _ = require('nvim-web-devicons').get_icon(self.file, vim.fn.expand('#' .. self.bufnr .. ':e'))
|
|
|
|
end
|
|
|
|
if dev then
|
|
|
|
self.icon = dev .. ' '
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-26 13:31:40 +00:00
|
|
|
---returns line configured for handling mouse click
|
|
|
|
---@param name string
|
|
|
|
---@return string
|
|
|
|
function Buffer:configure_mouse_click(name)
|
|
|
|
return string.format('%%%s@LualineSwitchBuffer@%s%%T', self.bufnr, name)
|
|
|
|
end
|
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
---returns rendered buffer
|
|
|
|
---@return string
|
2021-10-10 18:04:55 +00:00
|
|
|
function Buffer:render()
|
2021-11-18 02:12:33 +00:00
|
|
|
local name = self:name()
|
|
|
|
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
|
2021-11-13 13:44:35 +00:00
|
|
|
|
2022-05-30 14:25:05 +00:00
|
|
|
if self.ellipse then -- show ellipsis
|
2021-10-10 18:04:55 +00:00
|
|
|
name = '...'
|
|
|
|
else
|
2022-03-26 13:31:40 +00:00
|
|
|
name = self:apply_mode(name)
|
2021-10-10 18:04:55 +00:00
|
|
|
end
|
2021-11-18 02:12:33 +00:00
|
|
|
name = Buffer.apply_padding(name, self.options.padding)
|
2021-10-10 18:04:55 +00:00
|
|
|
self.len = vim.fn.strchars(name)
|
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
-- setup for mouse clicks
|
2022-03-26 13:31:40 +00:00
|
|
|
local line = self:configure_mouse_click(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 buffer
|
|
|
|
---@return string
|
2021-10-10 18:04:55 +00:00
|
|
|
function Buffer:separator_before()
|
|
|
|
if self.current or self.aftercurrent then
|
|
|
|
return '%S{' .. self.options.section_separators.left .. '}'
|
|
|
|
else
|
|
|
|
return self.options.component_separators.left
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
---apply separator after current buffer
|
|
|
|
---@return string
|
2021-10-10 18:04:55 +00:00
|
|
|
function Buffer:separator_after()
|
|
|
|
if self.current or self.beforecurrent then
|
|
|
|
return '%s{' .. self.options.section_separators.right .. '}'
|
|
|
|
else
|
|
|
|
return self.options.component_separators.right
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
---returns name of current buffer after filtering special buffers
|
|
|
|
---@return string
|
2021-10-10 18:04:55 +00:00
|
|
|
function Buffer:name()
|
|
|
|
if self.options.filetype_names[self.filetype] then
|
|
|
|
return self.options.filetype_names[self.filetype]
|
|
|
|
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+)')
|
|
|
|
return match ~= nil and match or vim.fn.fnamemodify(vim.env.SHELL, ':t')
|
2022-03-26 13:31:40 +00:00
|
|
|
elseif self.buftype == 'quickfix' then
|
|
|
|
local is_loclist = 0 ~= vim.fn.getloclist(0, { filewinid = 1 }).filewinid
|
|
|
|
return is_loclist and 'Location list' or 'Quickfix List'
|
2021-10-10 18:04:55 +00:00
|
|
|
elseif vim.fn.isdirectory(self.file) == 1 then
|
|
|
|
return vim.fn.fnamemodify(self.file, ':p:.')
|
|
|
|
elseif self.file == '' then
|
|
|
|
return '[No Name]'
|
|
|
|
end
|
2022-04-13 04:59:53 +00:00
|
|
|
|
|
|
|
local name
|
|
|
|
if self.options.show_filename_only then
|
|
|
|
name = vim.fn.fnamemodify(self.file, ':t')
|
|
|
|
else
|
|
|
|
name = vim.fn.pathshorten(vim.fn.fnamemodify(self.file, ':p:.'))
|
|
|
|
end
|
|
|
|
if self.options.hide_filename_extension then
|
|
|
|
name = vim.fn.fnamemodify(name, ':r')
|
|
|
|
end
|
|
|
|
return name
|
2021-10-10 18:04:55 +00:00
|
|
|
end
|
|
|
|
|
2021-11-18 02:12:33 +00:00
|
|
|
---adds spaces to left and right
|
|
|
|
function Buffer.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
|
|
|
|
|
2022-03-26 13:31:40 +00:00
|
|
|
function Buffer:apply_mode(name)
|
|
|
|
if self.options.mode == 0 then
|
2022-05-20 00:48:18 +00:00
|
|
|
return string.format('%s%s%s%s', self.alternate_file_icon, self.icon, name, self.modified_icon)
|
2022-03-26 13:31:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if self.options.mode == 1 then
|
2022-05-20 00:48:18 +00:00
|
|
|
return string.format('%s%s %s%s', self.alternate_file_icon, self.buf_index or '', self.icon, self.modified_icon)
|
|
|
|
end
|
|
|
|
|
|
|
|
if self.options.mode == 2 then
|
2022-05-20 00:48:48 +00:00
|
|
|
return string.format(
|
|
|
|
'%s%s %s%s%s',
|
|
|
|
self.alternate_file_icon,
|
|
|
|
self.buf_index or '',
|
|
|
|
self.icon,
|
|
|
|
name,
|
|
|
|
self.modified_icon
|
|
|
|
)
|
2022-03-26 13:31:40 +00:00
|
|
|
end
|
|
|
|
|
2022-05-20 00:48:18 +00:00
|
|
|
if self.options.mode == 3 then
|
|
|
|
return string.format('%s%s %s%s', self.alternate_file_icon, self.bufnr or '', self.icon, self.modified_icon)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- if self.options.mode == 4 then
|
|
|
|
return string.format('%s%s %s%s%s', self.alternate_file_icon, self.bufnr or '', self.icon, name, self.modified_icon)
|
2022-03-26 13:31:40 +00:00
|
|
|
end
|
|
|
|
|
2021-10-10 18:04:55 +00:00
|
|
|
return Buffer
|