Enhance: extract helper functions from apply_trans_sep()

This commit is contained in:
shadmansaleh 2021-08-27 00:09:02 +06:00
parent dd9e1b7bf0
commit d4f83afa7f
1 changed files with 33 additions and 28 deletions

View File

@ -11,13 +11,8 @@ local modules = require('lualine.utils.lazy_require'){
local config -- Stores cureently applied config local config -- Stores cureently applied config
local new_config = true -- Stores config that will be applied local new_config = true -- Stores config that will be applied
local function apply_transitional_separators(status) -- Helper for apply_transitional_separators()
local status_applied = {} -- Collects all the pieces for concatation local function find_next_hl(status, str_checked)
local last_hl -- Stores lash highligjt group that we found
local copied_pos = 1 -- Tracks how much we've copied over to status_applied
local str_checked = 1 -- Tracks where the searcher head is at
local function find_next_hl()
-- Gets the next valid hl group from str_checked -- Gets the next valid hl group from str_checked
local hl_pos_start, hl_pos_end = status:find('%%#.-#', str_checked) local hl_pos_start, hl_pos_end = status:find('%%#.-#', str_checked)
while true do while true do
@ -29,20 +24,28 @@ local function apply_transitional_separators(status)
hl_pos_start, hl_pos_end = next_start, next_end hl_pos_start, hl_pos_end = next_start, next_end
end end
return status:sub(hl_pos_start + 2, hl_pos_end - 1) return status:sub(hl_pos_start + 2, hl_pos_end - 1)
end end
local function fill_section_separator(sep, reverse) -- Helper for apply_transitional_separators()
local function fill_section_separator(status, str_checked, last_hl, sep, reverse)
-- Inserts transitional separator along with transitional highlight -- Inserts transitional separator along with transitional highlight
if last_hl and #last_hl == 0 then return end if last_hl and #last_hl == 0 then return end
local next_hl = find_next_hl() local next_hl = find_next_hl(status, str_checked)
if next_hl == nil then return end if next_hl == nil then return end
local transitional_highlight = reverse -- lua ternary assignment x ? y : z local transitional_highlight = reverse -- lua ternary assignment x ? y : z
and modules.highlight.get_transitional_highlights(last_hl, next_hl) and modules.highlight.get_transitional_highlights(last_hl, next_hl)
or modules.highlight.get_transitional_highlights(next_hl, last_hl) or modules.highlight.get_transitional_highlights(next_hl, last_hl)
if transitional_highlight then if transitional_highlight then
table.insert(status_applied, transitional_highlight .. sep) return transitional_highlight .. sep
end
end end
end
local function apply_transitional_separators(status)
local status_applied = {} -- Collects all the pieces for concatation
local last_hl -- Stores lash highligjt group that we found
local copied_pos = 1 -- Tracks how much we've copied over to status_applied
local str_checked = 1 -- Tracks where the searcher head is at
-- Process entire status replace the %s{sep} & %S{sep} placeholders -- Process entire status replace the %s{sep} & %S{sep} placeholders
-- with proper transitional separator. -- with proper transitional separator.
@ -61,7 +64,8 @@ local function apply_transitional_separators(status)
-- %s{sep} is marker for left separator and -- %s{sep} is marker for left separator and
local sep = status:match('^%%s{(.-)}', str_checked) local sep = status:match('^%%s{(.-)}', str_checked)
str_checked = str_checked + #sep + 4 -- 4 = len(%{}) str_checked = str_checked + #sep + 4 -- 4 = len(%{})
fill_section_separator(sep, false) local trans_sep = fill_section_separator(status, str_checked, last_hl, sep, false)
if trans_sep then table.insert(status_applied, trans_sep) end
copied_pos = str_checked copied_pos = str_checked
elseif next_char == 'S' then elseif next_char == 'S' then
-- %S{sep} is marker for right separator and -- %S{sep} is marker for right separator and
@ -72,7 +76,8 @@ local function apply_transitional_separators(status)
-- and in this exact order skip the left sep as we can't draw both. -- and in this exact order skip the left sep as we can't draw both.
str_checked = status:find('}', str_checked) + 1 str_checked = status:find('}', str_checked) + 1
end end
fill_section_separator(sep, true) local trans_sep = fill_section_separator(status, str_checked, last_hl, sep, true)
if trans_sep then table.insert(status_applied, trans_sep) end
copied_pos = str_checked copied_pos = str_checked
elseif next_char == '%' then elseif next_char == '%' then
str_checked = str_checked + 2 -- Skip the following % too str_checked = str_checked + 2 -- Skip the following % too