2021-03-18 00:57:59 +00:00
|
|
|
-- Copyright (c) 2020-2021 hoob3rt
|
|
|
|
-- MIT license, see LICENSE for more details.
|
2021-04-07 00:34:36 +00:00
|
|
|
local M = {}
|
2021-09-03 18:28:20 +00:00
|
|
|
local require = require('lualine_require').require
|
2022-01-02 11:38:39 +00:00
|
|
|
local utils = require('lualine.utils.utils')
|
|
|
|
local highlight = require('lualine.highlight')
|
2021-10-12 14:04:47 +00:00
|
|
|
|
|
|
|
---runs draw function on components in section
|
|
|
|
---handles separator edge cases :/
|
|
|
|
---also handles default transitional separators at section boundaries
|
|
|
|
---(why? I don't know)
|
|
|
|
---@param section table list of components
|
|
|
|
---@param section_name string used for getting proper hl
|
|
|
|
---@param is_focused boolean
|
|
|
|
---@return string formated string for a section
|
|
|
|
--TODO Clean this up this does lots of messy stuff.
|
2021-05-07 00:54:49 +00:00
|
|
|
function M.draw_section(section, section_name, is_focused)
|
2022-03-02 13:37:08 +00:00
|
|
|
local highlight_name = highlight.format_highlight(section_name, is_focused)
|
2021-05-07 00:54:49 +00:00
|
|
|
|
2021-03-18 00:57:59 +00:00
|
|
|
local status = {}
|
|
|
|
for _, component in pairs(section) do
|
2021-04-11 08:20:41 +00:00
|
|
|
-- load components into status table
|
2021-09-03 18:28:20 +00:00
|
|
|
if type(component) ~= 'table' or (type(component) == 'table' and not component.component_no) then
|
2021-05-06 11:05:55 +00:00
|
|
|
return '' -- unknown element in section. section posibly not yet loaded
|
|
|
|
end
|
2021-08-08 16:03:58 +00:00
|
|
|
table.insert(status, component:draw(highlight_name, is_focused))
|
2021-03-18 00:57:59 +00:00
|
|
|
end
|
2021-04-11 08:20:41 +00:00
|
|
|
|
2022-03-02 13:37:08 +00:00
|
|
|
local section_color = utils.extract_highlight_colors(string.match(highlight_name, '%%#(.*)#'))
|
|
|
|
|
2021-04-11 08:20:41 +00:00
|
|
|
-- Flags required for knowing when to remove component separator
|
2021-05-07 00:54:49 +00:00
|
|
|
local strip_next_component = false
|
2021-04-11 08:20:41 +00:00
|
|
|
local last_component_found = false
|
2021-05-07 00:54:49 +00:00
|
|
|
local first_component_no = #section
|
2021-04-11 08:20:41 +00:00
|
|
|
|
|
|
|
-- Check through components to see when component separator need to be removed
|
|
|
|
for component_no = #section, 1, -1 do
|
2021-09-03 18:28:20 +00:00
|
|
|
if #status[component_no] > 0 then
|
|
|
|
first_component_no = component_no
|
|
|
|
end
|
2021-04-11 08:20:41 +00:00
|
|
|
-- Remove component separator with highlight for last component
|
|
|
|
if not last_component_found and #status[component_no] > 0 then
|
|
|
|
last_component_found = true
|
2021-05-07 00:54:49 +00:00
|
|
|
status[component_no] = section[component_no]:strip_separator()
|
|
|
|
if section_name < 'c' then
|
2021-09-03 18:28:20 +00:00
|
|
|
if
|
|
|
|
type(section[first_component_no].options.separator) ~= 'table'
|
2021-09-14 15:14:23 +00:00
|
|
|
and (section[1].options.section_separators.left ~= nil and section[1].options.section_separators.left ~= '')
|
2021-09-03 18:28:20 +00:00
|
|
|
then
|
|
|
|
status[component_no] = string.format(
|
|
|
|
'%s%%S{%s}',
|
|
|
|
status[component_no],
|
2021-09-14 15:14:23 +00:00
|
|
|
section[1].options.section_separators.left
|
2021-09-03 18:28:20 +00:00
|
|
|
)
|
2021-05-07 00:54:49 +00:00
|
|
|
end
|
|
|
|
end
|
2021-04-11 08:20:41 +00:00
|
|
|
end
|
|
|
|
-- Remove component separator when color option is used in next component
|
2021-05-07 00:54:49 +00:00
|
|
|
if strip_next_component then
|
|
|
|
strip_next_component = false
|
2021-04-11 08:20:41 +00:00
|
|
|
status[component_no] = section[component_no]:strip_separator()
|
2021-03-18 00:57:59 +00:00
|
|
|
end
|
2021-04-11 08:20:41 +00:00
|
|
|
-- Remove component separator when color option is used to color background
|
2021-09-03 18:28:20 +00:00
|
|
|
if
|
2022-03-02 13:37:08 +00:00
|
|
|
(
|
|
|
|
type(section[component_no].options.color) == 'table'
|
|
|
|
and section[component_no].options.color.bg
|
|
|
|
and section[component_no].options.color.bg ~= section_color.bg
|
|
|
|
)
|
2021-09-03 18:28:20 +00:00
|
|
|
or type(section[component_no].options.color) == 'string'
|
2022-03-02 13:37:08 +00:00
|
|
|
or (
|
|
|
|
type(section[component_no].options.color) == 'function'
|
|
|
|
and section[component_no].color_fn_cache
|
|
|
|
and section[component_no].color_fn_cache.bg
|
|
|
|
and section[component_no].color_fn_cache.bg ~= section_color.bg
|
|
|
|
)
|
2021-09-03 18:28:20 +00:00
|
|
|
then
|
2021-05-07 00:54:49 +00:00
|
|
|
strip_next_component = true
|
2021-04-11 08:20:41 +00:00
|
|
|
status[component_no] = section[component_no]:strip_separator()
|
|
|
|
end
|
2021-05-07 00:54:49 +00:00
|
|
|
|
2021-09-03 18:28:20 +00:00
|
|
|
if section[component_no].strip_previous_separator == true then
|
2021-05-07 00:54:49 +00:00
|
|
|
strip_next_component = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local left_sparator_string = ''
|
2021-09-03 18:28:20 +00:00
|
|
|
if
|
|
|
|
section_name > 'x'
|
|
|
|
and section[first_component_no]
|
|
|
|
and type(section[first_component_no].options.separator) ~= 'table'
|
2021-09-14 15:14:23 +00:00
|
|
|
and (section[1].options.section_separators.right ~= nil and section[1].options.section_separators.right ~= '')
|
2021-09-03 18:28:20 +00:00
|
|
|
then
|
|
|
|
left_sparator_string = string.format(
|
|
|
|
'%%s{%s}',
|
2021-09-14 15:14:23 +00:00
|
|
|
section[first_component_no].options.ls_separator or section[1].options.section_separators.right
|
2021-09-03 18:28:20 +00:00
|
|
|
)
|
2021-04-11 08:20:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Remove empty strings from status
|
|
|
|
status = utils.list_shrink(status)
|
|
|
|
local status_str = table.concat(status)
|
2021-08-05 15:44:22 +00:00
|
|
|
|
2021-09-03 18:28:20 +00:00
|
|
|
if #status_str == 0 then
|
|
|
|
return ''
|
|
|
|
end
|
2021-08-26 11:26:13 +00:00
|
|
|
|
|
|
|
local needs_hl
|
|
|
|
|
2022-01-02 11:38:39 +00:00
|
|
|
local find_start_trans_sep_start, find_start_trans_sep_end = status_str:find('^%%s{.-}')
|
2021-08-26 11:26:13 +00:00
|
|
|
if find_start_trans_sep_start then
|
|
|
|
-- the section doesn't need to be prepended with default hl when sections
|
|
|
|
-- first component has trasitionals sep
|
|
|
|
needs_hl = status_str:find('^%%#', find_start_trans_sep_end + 1)
|
2021-09-03 18:28:20 +00:00
|
|
|
else
|
2022-01-02 11:38:39 +00:00
|
|
|
needs_hl = status_str:find('^%%#')
|
2021-09-03 18:28:20 +00:00
|
|
|
end
|
2021-08-26 11:26:13 +00:00
|
|
|
|
|
|
|
if needs_hl then
|
2021-04-11 08:20:41 +00:00
|
|
|
-- Don't prepend with old highlight when the component changes it imidiately
|
2021-08-05 15:44:22 +00:00
|
|
|
return left_sparator_string .. status_str
|
2021-04-11 08:20:41 +00:00
|
|
|
else
|
2021-05-07 00:54:49 +00:00
|
|
|
return left_sparator_string .. highlight_name .. status_str
|
2021-03-18 00:57:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return M
|