refactor: moved section utils to new file

This commit is contained in:
hoob3rt 2021-03-18 01:57:59 +01:00
parent 745cdde237
commit bfc9c744d7
3 changed files with 56 additions and 51 deletions

View File

@ -1,7 +1,7 @@
-- Copyright (c) 2020-2021 hoob3rt -- Copyright (c) 2020-2021 hoob3rt
-- MIT license, see LICENSE for more details. -- MIT license, see LICENSE for more details.
local utils_component = require('lualine.utils.component')
local utils = require('lualine.utils.utils') local utils = require('lualine.utils.utils')
local utils_section = require('lualine.utils.section')
local highlight = require('lualine.highlight') local highlight = require('lualine.highlight')
local config = require('lualine.config') local config = require('lualine.config')
@ -194,7 +194,7 @@ local function statusline(sections, is_focused)
local section_highlight = highlight.format_highlight(is_focused, local section_highlight = highlight.format_highlight(is_focused,
'lualine_' .. 'lualine_' ..
section_name) section_name)
local section_data = utils_component.draw_section( local section_data = utils_section.draw_section(
sections['lualine_' .. section_name], sections['lualine_' .. section_name],
section_highlight) section_highlight)
if #section_data > 0 then if #section_data > 0 then

View File

@ -5,7 +5,7 @@ local M = {}
local highlight = require 'lualine.highlight' local highlight = require 'lualine.highlight'
-- set upper or lower case -- set upper or lower case
local function apply_case(status, options) function M.apply_case(status, options)
-- Donn't work on components that emit vim statusline escaped chars -- Donn't work on components that emit vim statusline escaped chars
if status:find('%%') and not status:find('%%%%') then return status end if status:find('%%') and not status:find('%%%%') then return status end
if options.upper == true then if options.upper == true then
@ -17,7 +17,7 @@ local function apply_case(status, options)
end end
-- Adds spaces to left and right of a component -- Adds spaces to left and right of a component
local function apply_padding(status, options) function M.apply_padding(status, options)
local l_padding = (options.left_padding or options.padding or 1) local l_padding = (options.left_padding or options.padding or 1)
local r_padding = (options.right_padding or options.padding or 1) local r_padding = (options.right_padding or options.padding or 1)
if l_padding then if l_padding then
@ -36,7 +36,7 @@ local function apply_padding(status, options)
end end
-- Applies custom highlights for component -- Applies custom highlights for component
local function apply_highlights(status, options, default_hl) function M.apply_highlights(status, options, default_hl)
if options.color_highlight then if options.color_highlight then
status = highlight.component_format_highlight(options.color_highlight) .. status = highlight.component_format_highlight(options.color_highlight) ..
status status
@ -45,7 +45,7 @@ local function apply_highlights(status, options, default_hl)
end end
-- Apply icon in front of component -- Apply icon in front of component
local function apply_icon(status, options) function M.apply_icon(status, options)
if options.icons_enabled and options.icon then if options.icons_enabled and options.icon then
status = options.icon .. ' ' .. status status = options.icon .. ' ' .. status
end end
@ -54,7 +54,7 @@ end
-- Apply separator at end of component only when -- Apply separator at end of component only when
-- custom highlights haven't affected background -- custom highlights haven't affected background
local function apply_spearator(status, options) function M.apply_spearator(status, options)
local separator local separator
if options.separator and #options.separator > 0 then if options.separator and #options.separator > 0 then
separator = options.separator separator = options.separator
@ -71,7 +71,7 @@ local function apply_spearator(status, options)
return status return status
end end
local function strip_separator(status, options) function M.strip_separator(status, options)
if options.separator_applied then if options.separator_applied then
status = status:sub(1, #status - #options.separator_applied) status = status:sub(1, #status - #options.separator_applied)
options.separator_applied = nil options.separator_applied = nil
@ -79,47 +79,4 @@ local function strip_separator(status, options)
return status return status
end end
-- Returns formated string for a section
function M.draw_section(section, highlight_name)
local status = {}
local drawn_components = {}
for _, component in pairs(section) do
local localstatus = component[1]()
if #localstatus > 0 then
local custom_highlight_at_begining =
localstatus:find('%%#.*#') == 1 or component.color ~= nil
-- Apply modifier functions for options
if component.format then localstatus = component.format(localstatus) end
localstatus = apply_icon(localstatus, component)
localstatus = apply_case(localstatus, component)
localstatus = apply_padding(localstatus, component)
localstatus = apply_highlights(localstatus, component, highlight_name)
localstatus = apply_spearator(localstatus, component)
if custom_highlight_at_begining or (#drawn_components > 0 and
not drawn_components[#drawn_components].separator_applied) then
-- Don't prepend with old highlight when the component changes it imidiately
-- Or when it was already applied with separator
table.insert(status, localstatus)
else
table.insert(status, highlight_name .. localstatus)
end
table.insert(drawn_components, component)
end
end
-- Draw nothing when all the components were empty
if #status == 0 then return '' end
-- Remove separators sorounding custom highlighted component
for i = 1, #status do
if (drawn_components[i].color and drawn_components[i].color.bg) or
drawn_components[i].custom_highlight then
status[i] = strip_separator(status[i], drawn_components[i])
if i > 1 then
status[i - 1] = strip_separator(status[i - 1], drawn_components[i - 1])
end
end
end
status[#status] = strip_separator(status[#status], drawn_components[#status])
return table.concat(status)
end
return M return M

View File

@ -0,0 +1,48 @@
-- Copyright (c) 2020-2021 hoob3rt
-- MIT license, see LICENSE for more details.
local utils_component = require('lualine.utils.component')
local M ={}
-- Returns formated string for a section
function M.draw_section(section, highlight_name)
local status = {}
local drawn_components = {}
for _, component in pairs(section) do
local localstatus = component[1]()
if #localstatus > 0 then
local custom_highlight_at_begining =
localstatus:find('%%#.*#') == 1 or component.color ~= nil
-- Apply modifier functions for options
if component.format then localstatus = component.format(localstatus) end
localstatus = utils_component.apply_icon(localstatus, component)
localstatus = utils_component.apply_case(localstatus, component)
localstatus = utils_component.apply_padding(localstatus, component)
localstatus = utils_component.apply_highlights(localstatus, component, highlight_name)
localstatus = utils_component.apply_spearator(localstatus, component)
if custom_highlight_at_begining or (#drawn_components > 0 and
not drawn_components[#drawn_components].separator_applied) then
-- Don't prepend with old highlight when the component changes it imidiately
-- Or when it was already applied with separator
table.insert(status, localstatus)
else
table.insert(status, highlight_name .. localstatus)
end
table.insert(drawn_components, component)
end
end
-- Draw nothing when all the components were empty
if #status == 0 then return '' end
-- Remove separators sorounding custom highlighted component
for i = 1, #status do
if (drawn_components[i].color and drawn_components[i].color.bg) or
drawn_components[i].custom_highlight then
status[i] = utils_component.strip_separator(status[i], drawn_components[i])
if i > 1 then
status[i - 1] = utils_component.strip_separator(status[i - 1], drawn_components[i - 1])
end
end
end
status[#status] = utils_component.strip_separator(status[#status], drawn_components[#status])
return table.concat(status)
end
return M