2021-05-07 00:54:49 +00:00
|
|
|
-- Copyright (c) 2020-2021 shadmansaleh
|
|
|
|
-- MIT license, see LICENSE for more details.
|
2022-07-20 13:28:49 +00:00
|
|
|
local lualine_require = require('lualine_require')
|
|
|
|
local require = lualine_require.require
|
2021-09-25 17:15:42 +00:00
|
|
|
local M = require('lualine.utils.class'):extend()
|
2022-07-20 13:28:49 +00:00
|
|
|
local modules = lualine_require.lazy_require {
|
|
|
|
highlight = 'lualine.highlight',
|
|
|
|
utils_notices = 'lualine.utils.notices',
|
|
|
|
fn_store = 'lualine.utils.fn_store',
|
|
|
|
}
|
2021-04-11 08:20:41 +00:00
|
|
|
|
|
|
|
-- Used to provide a unique id for each component
|
|
|
|
local component_no = 1
|
2022-07-20 13:28:49 +00:00
|
|
|
function M._reset_components()
|
2022-07-06 14:36:07 +00:00
|
|
|
component_no = 1
|
|
|
|
end
|
2021-04-11 08:20:41 +00:00
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
-- variable to store component output for manipulation
|
|
|
|
M.status = ''
|
|
|
|
|
2021-09-25 17:15:42 +00:00
|
|
|
function M:__tostring()
|
|
|
|
local str = 'Component: ' .. self.options.component_name
|
|
|
|
if self.debug then
|
|
|
|
str = str .. '\n---------------------\n' .. vim.inspect(self)
|
|
|
|
end
|
|
|
|
return str
|
|
|
|
end
|
|
|
|
|
2021-11-25 06:57:00 +00:00
|
|
|
M.__is_lualine_component = true
|
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
---initialize new component
|
|
|
|
---@param options table options for component
|
2021-09-25 17:15:42 +00:00
|
|
|
function M:init(options)
|
|
|
|
self.options = options or {}
|
|
|
|
component_no = component_no + 1
|
2021-10-13 01:17:24 +00:00
|
|
|
if not self.options.component_name then
|
2021-09-25 17:15:42 +00:00
|
|
|
self.options.component_name = tostring(component_no)
|
|
|
|
end
|
|
|
|
self.component_no = component_no
|
|
|
|
self:set_separator()
|
|
|
|
self:create_option_highlights()
|
2022-07-20 13:28:49 +00:00
|
|
|
self:set_on_click()
|
2021-09-25 17:15:42 +00:00
|
|
|
end
|
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
---sets the default separator for component based on whether the component
|
2022-05-30 14:25:05 +00:00
|
|
|
---is in left sections or right sections when separator option is omitted.
|
2021-09-25 17:15:42 +00:00
|
|
|
function M:set_separator()
|
|
|
|
if self.options.separator == nil then
|
|
|
|
if self.options.component_separators then
|
2022-03-02 13:37:08 +00:00
|
|
|
if self.options.self.section < 'x' then
|
2021-09-25 17:15:42 +00:00
|
|
|
self.options.separator = self.options.component_separators.left
|
2021-04-11 08:20:41 +00:00
|
|
|
else
|
2021-09-25 17:15:42 +00:00
|
|
|
self.options.separator = self.options.component_separators.right
|
2021-04-11 08:20:41 +00:00
|
|
|
end
|
|
|
|
end
|
2021-09-25 17:15:42 +00:00
|
|
|
end
|
|
|
|
end
|
2021-04-11 08:20:41 +00:00
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
---creates hl group from color option
|
2021-09-25 17:15:42 +00:00
|
|
|
function M:create_option_highlights()
|
|
|
|
-- set custom highlights
|
|
|
|
if self.options.color then
|
2022-04-14 18:30:47 +00:00
|
|
|
self.options.color_highlight = self:create_hl(self.options.color)
|
2021-09-25 17:15:42 +00:00
|
|
|
end
|
2022-03-02 14:02:58 +00:00
|
|
|
-- setup icon highlight
|
|
|
|
if type(self.options.icon) == 'table' and self.options.icon.color then
|
2022-04-14 18:30:47 +00:00
|
|
|
self.options.icon_color_highlight = self:create_hl(self.options.icon.color)
|
2022-03-02 14:02:58 +00:00
|
|
|
end
|
2021-09-25 17:15:42 +00:00
|
|
|
end
|
2021-04-11 08:20:41 +00:00
|
|
|
|
2022-07-20 13:28:49 +00:00
|
|
|
---Setup on click function so they can be added during drawing.
|
|
|
|
function M:set_on_click()
|
|
|
|
if self.options.on_click ~= nil then
|
|
|
|
if vim.fn.has('nvim-0.8') == 0 then
|
|
|
|
modules.utils_notices.add_notice(
|
2022-07-20 13:29:21 +00:00
|
|
|
'### Options.on_click\nSorry `on_click` can only be used in neovim 0.8 or higher.\n'
|
|
|
|
)
|
2022-07-20 13:28:49 +00:00
|
|
|
self.options.on_click = nil
|
|
|
|
return
|
|
|
|
end
|
|
|
|
self.on_click_id = modules.fn_store.register_fn(self.component_no, self.options.on_click)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
---adds spaces to left and right of a component
|
2021-09-25 17:15:42 +00:00
|
|
|
function M:apply_padding()
|
|
|
|
local padding = self.options.padding
|
|
|
|
local l_padding, r_padding
|
|
|
|
if padding == nil then
|
|
|
|
padding = 1
|
|
|
|
end
|
|
|
|
if type(padding) == 'number' then
|
|
|
|
l_padding, r_padding = padding, padding
|
|
|
|
elseif type(padding) == 'table' then
|
|
|
|
l_padding, r_padding = padding.left, padding.right
|
|
|
|
end
|
|
|
|
if l_padding then
|
2022-01-02 11:38:39 +00:00
|
|
|
if self.status:find('%%#.*#') == 1 then
|
2022-05-30 14:25:05 +00:00
|
|
|
-- When component has changed the highlight at beginning
|
2021-09-25 17:15:42 +00:00
|
|
|
-- we will add the padding after the highlight
|
|
|
|
local pre_highlight = vim.fn.matchlist(self.status, [[\(%#.\{-\}#\)]])[2]
|
|
|
|
self.status = pre_highlight .. string.rep(' ', l_padding) .. self.status:sub(#pre_highlight + 1, #self.status)
|
|
|
|
else
|
|
|
|
self.status = string.rep(' ', l_padding) .. self.status
|
2021-04-11 08:20:41 +00:00
|
|
|
end
|
2021-09-25 17:15:42 +00:00
|
|
|
end
|
|
|
|
if r_padding then
|
|
|
|
self.status = self.status .. string.rep(' ', r_padding)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
---applies custom highlights for component
|
2021-09-25 17:15:42 +00:00
|
|
|
function M:apply_highlights(default_highlight)
|
|
|
|
if self.options.color_highlight then
|
2022-03-02 13:37:08 +00:00
|
|
|
local hl_fmt
|
2022-04-14 18:30:47 +00:00
|
|
|
hl_fmt, M.color_fn_cache = self:format_hl(self.options.color_highlight)
|
2022-03-02 13:37:08 +00:00
|
|
|
self.status = hl_fmt .. self.status
|
2021-09-25 17:15:42 +00:00
|
|
|
end
|
2022-01-02 11:38:39 +00:00
|
|
|
if type(self.options.separator) ~= 'table' and self.status:find('%%#') then
|
2021-09-25 17:15:42 +00:00
|
|
|
-- Apply default highlight only when we aren't applying trans sep and
|
2022-05-30 14:25:05 +00:00
|
|
|
-- the component has changed it's hl. Since we won't be applying
|
|
|
|
-- regular sep in those cases so ending with default hl isn't necessary
|
2021-09-25 17:15:42 +00:00
|
|
|
self.status = self.status .. default_highlight
|
2022-05-30 14:25:05 +00:00
|
|
|
-- Also put it in applied sep so when sep get striped so does the hl
|
2021-09-25 17:15:42 +00:00
|
|
|
self.applied_separator = default_highlight
|
|
|
|
end
|
|
|
|
-- Prepend default hl when the component doesn't start with hl otherwise
|
|
|
|
-- color in previous component can cause side effect
|
2022-01-02 11:38:39 +00:00
|
|
|
if not self.status:find('^%%#') then
|
2021-09-25 17:15:42 +00:00
|
|
|
self.status = default_highlight .. self.status
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-30 14:25:05 +00:00
|
|
|
---apply icon to component (appends/prepends component with icon)
|
2021-09-25 17:15:42 +00:00
|
|
|
function M:apply_icon()
|
2022-03-02 14:02:58 +00:00
|
|
|
local icon = self.options.icon
|
|
|
|
if self.options.icons_enabled and icon then
|
|
|
|
if type(icon) == 'table' then
|
|
|
|
icon = icon[1]
|
|
|
|
end
|
2022-04-30 10:11:22 +00:00
|
|
|
if
|
|
|
|
self.options.icon_color_highlight
|
|
|
|
and type(self.options.icon) == 'table'
|
|
|
|
and self.options.icon.align == 'right'
|
|
|
|
then
|
2022-04-30 10:10:47 +00:00
|
|
|
self.status = table.concat {
|
|
|
|
self.status,
|
|
|
|
' ',
|
|
|
|
self:format_hl(self.options.icon_color_highlight),
|
|
|
|
icon,
|
|
|
|
self:get_default_hl(),
|
|
|
|
}
|
|
|
|
elseif self.options.icon_color_highlight then
|
2022-03-02 14:02:58 +00:00
|
|
|
self.status = table.concat {
|
2022-04-14 18:30:47 +00:00
|
|
|
self:format_hl(self.options.icon_color_highlight),
|
2022-03-02 14:02:58 +00:00
|
|
|
icon,
|
|
|
|
self:get_default_hl(),
|
|
|
|
' ',
|
|
|
|
self.status,
|
|
|
|
}
|
2022-04-30 10:10:47 +00:00
|
|
|
elseif type(self.options.icon) == 'table' and self.options.icon.align == 'right' then
|
|
|
|
self.status = table.concat({ self.status, icon }, ' ')
|
2022-03-02 14:02:58 +00:00
|
|
|
else
|
|
|
|
self.status = table.concat({ icon, self.status }, ' ')
|
|
|
|
end
|
2021-09-25 17:15:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
---apply separator at end of component only when
|
|
|
|
---custom highlights haven't affected background
|
2021-09-25 17:15:42 +00:00
|
|
|
function M:apply_separator()
|
|
|
|
local separator = self.options.separator
|
|
|
|
if type(separator) == 'table' then
|
|
|
|
if self.options.separator[2] == '' then
|
2022-03-02 13:37:08 +00:00
|
|
|
if self.options.self.section < 'x' then
|
2021-09-25 17:15:42 +00:00
|
|
|
separator = self.options.component_separators.left
|
2021-05-07 00:54:49 +00:00
|
|
|
else
|
2021-09-25 17:15:42 +00:00
|
|
|
separator = self.options.component_separators.right
|
2021-05-07 00:54:49 +00:00
|
|
|
end
|
2021-09-25 17:15:42 +00:00
|
|
|
else
|
2021-09-03 18:28:20 +00:00
|
|
|
return
|
|
|
|
end
|
2021-09-25 17:15:42 +00:00
|
|
|
end
|
|
|
|
if separator and #separator > 0 then
|
|
|
|
self.status = self.status .. separator
|
|
|
|
self.applied_separator = self.applied_separator .. separator
|
|
|
|
end
|
|
|
|
end
|
2021-04-11 08:20:41 +00:00
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
---apply transitional separator for the component
|
2021-09-25 17:15:42 +00:00
|
|
|
function M:apply_section_separators()
|
|
|
|
if type(self.options.separator) ~= 'table' then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if self.options.separator.left ~= nil and self.options.separator.left ~= '' then
|
|
|
|
self.status = string.format('%%s{%s}%s', self.options.separator.left, self.status)
|
|
|
|
self.strip_previous_separator = true
|
|
|
|
end
|
|
|
|
if self.options.separator.right ~= nil and self.options.separator.right ~= '' then
|
|
|
|
self.status = string.format('%s%%S{%s}', self.status, self.options.separator.right)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-20 13:28:49 +00:00
|
|
|
---Add on click funtion description to already drawn item
|
|
|
|
function M:apply_on_click()
|
|
|
|
if self.on_click_id then
|
|
|
|
self.status = self:format_fn(self.on_click_id, self.status)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
---remove separator from tail of this component.
|
|
|
|
---called by lualine.utils.sections.draw_section to manage unnecessary separators
|
2021-09-25 17:15:42 +00:00
|
|
|
function M:strip_separator()
|
|
|
|
if not self.applied_separator then
|
2021-08-26 11:26:13 +00:00
|
|
|
self.applied_separator = ''
|
2021-09-25 17:15:42 +00:00
|
|
|
end
|
|
|
|
self.status = self.status:sub(1, (#self.status - #self.applied_separator))
|
|
|
|
self.applied_separator = nil
|
|
|
|
return self.status
|
|
|
|
end
|
2021-08-08 16:03:58 +00:00
|
|
|
|
2022-03-02 13:37:08 +00:00
|
|
|
function M:get_default_hl()
|
|
|
|
if self.options.color_highlight then
|
2022-04-14 18:30:47 +00:00
|
|
|
return self:format_hl(self.options.color_highlight)
|
2022-03-02 13:37:08 +00:00
|
|
|
elseif self.default_hl then
|
|
|
|
return self.default_hl
|
|
|
|
else
|
2022-07-20 13:28:49 +00:00
|
|
|
return modules.highlight.format_highlight(self.options.self.section)
|
2022-03-02 13:37:08 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-14 18:30:47 +00:00
|
|
|
---create a lualine highlight for color
|
|
|
|
---@param color table|string|function defined color for hl
|
|
|
|
---@param hint string|nil hint for hl name
|
2022-05-30 14:25:05 +00:00
|
|
|
---@return table an identifier to later retrieve the hl for application
|
2022-04-14 18:30:47 +00:00
|
|
|
function M:create_hl(color, hint)
|
|
|
|
hint = hint and self.options.component_name .. '_' .. hint or self.options.component_name
|
2022-07-20 13:28:49 +00:00
|
|
|
return modules.highlight.create_component_highlight_group(color, hint, self.options, false)
|
2022-04-14 18:30:47 +00:00
|
|
|
end
|
|
|
|
|
2022-05-30 14:25:05 +00:00
|
|
|
---Get stl formatted hl group for hl_token
|
|
|
|
---@param hl_token table identifier received from create_hl or create_component_highlight_group
|
|
|
|
---@return string stl formatted hl group for hl_token
|
2022-04-14 18:30:47 +00:00
|
|
|
function M:format_hl(hl_token)
|
2022-07-20 13:28:49 +00:00
|
|
|
return modules.highlight.component_format_highlight(hl_token)
|
|
|
|
end
|
|
|
|
|
|
|
|
---Wrap str with click format for function of id
|
|
|
|
---@param id number
|
|
|
|
---@param str string
|
|
|
|
---@return string
|
|
|
|
function M:format_fn(id, str)
|
|
|
|
return string.format("%%%d@v:lua.require'lualine.utils.fn_store'.call_fn@%s%%T", id, str)
|
2022-04-14 18:30:47 +00:00
|
|
|
end
|
|
|
|
|
2021-09-25 17:15:42 +00:00
|
|
|
-- luacheck: push no unused args
|
2021-10-12 14:04:47 +00:00
|
|
|
---actual function that updates a component. Must be overwritten with component functionality
|
2021-09-25 17:15:42 +00:00
|
|
|
function M:update_status(is_focused) end
|
|
|
|
-- luacheck: pop
|
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
---driver code of the class
|
2022-04-14 18:30:47 +00:00
|
|
|
---@param default_highlight string default hl group of section where component resides
|
|
|
|
---@param is_focused boolean|number whether drawing for active or inactive statusline.
|
2022-05-30 14:25:05 +00:00
|
|
|
---@return string stl formatted rendering string for component
|
2021-09-25 17:15:42 +00:00
|
|
|
function M:draw(default_highlight, is_focused)
|
|
|
|
self.status = ''
|
|
|
|
self.applied_separator = ''
|
|
|
|
|
|
|
|
if self.options.cond ~= nil and self.options.cond() ~= true then
|
2021-04-11 08:20:41 +00:00
|
|
|
return self.status
|
2021-09-25 17:15:42 +00:00
|
|
|
end
|
2022-03-02 13:37:08 +00:00
|
|
|
self.default_hl = default_highlight
|
2021-09-25 17:15:42 +00:00
|
|
|
local status = self:update_status(is_focused)
|
|
|
|
if self.options.fmt then
|
|
|
|
status = self.options.fmt(status or '')
|
|
|
|
end
|
|
|
|
if type(status) == 'string' and #status > 0 then
|
|
|
|
self.status = status
|
|
|
|
self:apply_icon()
|
|
|
|
self:apply_padding()
|
2022-07-20 13:28:49 +00:00
|
|
|
self:apply_on_click()
|
2022-07-22 14:17:02 +00:00
|
|
|
self:apply_highlights(default_highlight)
|
2021-09-25 17:15:42 +00:00
|
|
|
self:apply_section_separators()
|
|
|
|
self:apply_separator()
|
|
|
|
end
|
|
|
|
return self.status
|
|
|
|
end
|
2021-04-11 08:20:41 +00:00
|
|
|
|
2021-09-25 17:15:42 +00:00
|
|
|
return M
|