shadmansaleh fac96d71cc [Breaking] Refactor: switch to %{%expr%} from %!expr
Huge change to internal mechanics.
- Now %{%expr%} blocks are used for evaluating statusline instead of
  %!expr . Pros for this is statusline is evaluated on current win and
  buf context instead of active win & bufs context.
- Now all components branch & diff(These two are cached) including users
  function components updates on inactive status.
  - now components update status and function components receive an
    argument (is_focused) when called. It indicates whether it's running
    for active or inactive statusline.
- Now lualine no longer aggrasively takes over 'statusline' option.
  instead it sets the global version of statusline option. So it's
  possible to unset it to hide lualine . Or set buffer local version
  of that option to have different statusline then lualine on thay
  buffer
- Switch vim.o to vim.go or vim.opt.
- BugFix autcommands being set everytime an instence of diff or branch
  component is created
- Added new utils functions define_autocmd & is_focused
- Remove utils function lualine_eval
- Removed hacky require cache modification from component.lua
2021-08-08 22:50:17 +06:00

83 lines
3.3 KiB
Lua

-- Copyright (c) 2020-2021 hoob3rt
-- MIT license, see LICENSE for more details.
local M = {}
local utils = require('lualine.utils.utils')
local highlight = require('lualine.highlight')
-- Returns formated string for a section
function M.draw_section(section, section_name, is_focused)
local highlight_name = highlight.format_highlight(is_focused,
'lualine_' .. section_name)
local status = {}
for _, component in pairs(section) do
-- load components into status table
if type(component) ~= 'table' or
(type(component) == 'table' and not component.component_no) then
return '' -- unknown element in section. section posibly not yet loaded
end
table.insert(status, component:draw(highlight_name, is_focused))
end
-- Flags required for knowing when to remove component separator
local strip_next_component = false
local last_component_found = false
local first_component_no = #section
-- Check through components to see when component separator need to be removed
for component_no = #section, 1, -1 do
if #status[component_no] > 0 then first_component_no = component_no end
-- Remove component separator with highlight for last component
if not last_component_found and #status[component_no] > 0 then
last_component_found = true
status[component_no] = section[component_no]:strip_separator()
if section_name < 'c' then
if type(section[first_component_no].options.separator) ~= 'table' and
section[1].options.section_separators[1] ~= '' then
status[component_no] = string.format('%s%%S{%s}',
status[component_no], section[1]
.options.section_separators[1])
end
end
end
-- Remove component separator when color option is used in next component
if strip_next_component then
strip_next_component = false
status[component_no] = section[component_no]:strip_separator()
end
-- Remove component separator when color option is used to color background
if (type(section[component_no].options.color) == 'table' and
section[component_no].options.color.bg) or
type(section[component_no].options.color) == 'string' then
strip_next_component = true
status[component_no] = section[component_no]:strip_separator()
end
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])
end
-- Remove empty strings from status
status = utils.list_shrink(status)
local status_str = table.concat(status)
if #status_str == 0 then return ''
elseif status_str:find('%%#.*#') == 1 then
-- Don't prepend with old highlight when the component changes it imidiately
return left_sparator_string .. status_str
else
return left_sparator_string .. highlight_name .. status_str
end
end
return M