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-04-11 08:20:41 +00:00
|
|
|
local utils = require('lualine.utils.utils')
|
2021-05-07 00:54:49 +00:00
|
|
|
local highlight = require('lualine.highlight')
|
2021-03-18 00:57:59 +00:00
|
|
|
-- Returns formated string for a section
|
2021-05-07 00:54:49 +00:00
|
|
|
function M.draw_section(section, section_name, is_focused)
|
|
|
|
local highlight_name = highlight.format_highlight(is_focused,
|
2021-05-07 15:39:28 +00:00
|
|
|
'lualine_' .. section_name)
|
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-05-09 21:11:18 +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-04-11 08:20:41 +00:00
|
|
|
table.insert(status, component:draw(highlight_name))
|
2021-03-18 00:57:59 +00:00
|
|
|
end
|
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-05-07 15:39:28 +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
|
|
|
|
if type(section[first_component_no].options.separator) ~= 'table' and
|
2021-05-07 15:39:28 +00:00
|
|
|
section[1].options.section_separators[1] ~= '' then
|
|
|
|
status[component_no] = string.format('%s%%S{%s}',
|
|
|
|
status[component_no], section[1]
|
|
|
|
.options.section_separators[1])
|
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-04-23 11:41:41 +00:00
|
|
|
if (type(section[component_no].options.color) == 'table' and
|
|
|
|
section[component_no].options.color.bg) or
|
2021-05-09 21:11:18 +00:00
|
|
|
type(section[component_no].options.color) == 'string' 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
|
|
|
|
|
|
|
if (section[component_no].strip_previous_separator == true) then
|
|
|
|
strip_next_component = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local left_sparator_string = ''
|
|
|
|
if section_name > 'x' and section[first_component_no] and
|
|
|
|
type(section[first_component_no].options.separator) ~= 'table' and
|
|
|
|
section[1].options.section_separators[2] ~= '' then
|
|
|
|
left_sparator_string = string.format('%%s{%s}',
|
|
|
|
section[first_component_no].options.ls_separator or
|
|
|
|
section[1].options.section_separators[2])
|
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-05-02 04:36:51 +00:00
|
|
|
if status_str:find('%%#.*#') == 1 or #status_str == 0 then
|
2021-04-11 08:20:41 +00:00
|
|
|
-- Don't prepend with old highlight when the component changes it imidiately
|
|
|
|
return status_str
|
|
|
|
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
|