refactor statusline() function

This commit is contained in:
shadmansaleh 2021-05-07 21:39:28 +06:00
parent f480db58fb
commit d8a2a640e0
3 changed files with 97 additions and 73 deletions

View File

@ -1,6 +1,5 @@
-- Copyright (c) 2020-2021 shadmansaleh -- Copyright (c) 2020-2021 shadmansaleh
-- MIT license, see LICENSE for more details. -- MIT license, see LICENSE for more details.
local highlight = require 'lualine.highlight' local highlight = require 'lualine.highlight'
-- Used to provide a unique id for each component -- Used to provide a unique id for each component

View File

@ -7,8 +7,83 @@ local config_module = require('lualine.config')
local config = config_module.config local config = config_module.config
local function apply_transitional_separators(previous_section, current_section,
next_section)
local function fill_section_separator(prev, next, sep, reverse)
if #sep == 0 then return 0 end
local transitional_highlight = highlight.get_transitional_highlights(prev,
next,
reverse)
if transitional_highlight and #transitional_highlight > 0 then
return transitional_highlight .. sep
else
return ''
end
end
-- variable to track separators position
local sep_pos = 1
-- %s{sep} is marker for left separator and
-- %S{sep} is marker for right separator
-- Apply left separator
while sep_pos do
-- get what the separator char
local sep = current_section.data:match('%%s{(.-)}', sep_pos)
-- Get where separator starts from
sep_pos = current_section.data:find('%%s{.-}', sep_pos)
if not sep or not sep_pos then break end
-- part of section before separator . -1 since we don't want the %
local prev = current_section.data:sub(1, sep_pos - 1)
-- part of section after separator. 4 is length of "%s{}"
local nxt = current_section.data:sub(sep_pos + 4 + #sep)
-- prev might not exist when separator is the first element of section
-- use previous section as prev
if not prev or #prev == 0 or sep_pos == 1 then
prev = previous_section.data
end
if prev ~= previous_section.data then
-- Since the section isn't suppose to be highlighted with separators
-- separators highlight extract the last highlight and place it between
-- separator and section
local last_hl = prev:match('.*%#(.-)#.-')
current_section.data = prev ..
fill_section_separator(prev, nxt, sep, false) ..
'%#' .. last_hl .. '#' .. nxt
else
current_section.data = fill_section_separator(prev, nxt, sep, true) .. nxt
end
end
-- Reset pos for right separator
sep_pos = 1
-- Apply right separator
while sep_pos do
local sep = current_section.data:match('%%S{(.-)}', sep_pos)
sep_pos = current_section.data:find('%%S{.-}', sep_pos)
if not sep or not sep_pos then break end
local prev = current_section.data:sub(1, sep_pos - 1)
local nxt = current_section.data:sub(sep_pos + 4 + #sep)
if not nxt or #nxt == 0 or sep_pos == #current_section.data then
nxt = next_section.data
end
if nxt ~= next_section.data then
current_section.data = prev ..
fill_section_separator(prev, nxt, sep, false) ..
nxt
else
current_section.data = prev ..
fill_section_separator(prev, nxt, sep, false)
end
sep_pos = sep_pos + 4 + #sep
end
return current_section.data
end
local function statusline(sections, is_focused) local function statusline(sections, is_focused)
local function create_status_builder()
-- status_builder stores statusline without section_separators
-- The sequence sections should maintain -- The sequence sections should maintain
local section_sequence = {'a', 'b', 'c', 'x', 'y', 'z'} local section_sequence = {'a', 'b', 'c', 'x', 'y', 'z'}
local status_builder = {} local status_builder = {}
@ -19,28 +94,10 @@ local function statusline(sections, is_focused)
sections['lualine_' .. section_name], sections['lualine_' .. section_name],
section_name, is_focused) section_name, is_focused)
if #section_data > 0 then if #section_data > 0 then
table.insert(status_builder, table.insert(status_builder, {name = section_name, data = section_data})
{name = section_name, data = section_data})
end end
end end
end end
return status_builder
end
local function fill_section_separator(prev, next, sep, reverse)
if #sep == 0 then return 0 end
local transitional_highlight = highlight.get_transitional_highlights(
prev, next, reverse)
-- if not transitional_highlight or #transitional_highlight == 0 then
-- end
if transitional_highlight and #transitional_highlight > 0 then
return transitional_highlight..sep
else
return ''
end
end
-- status_builder stores statusline without section_separators
local status_builder = create_status_builder()
-- Actual statusline -- Actual statusline
local status = {} local status = {}
@ -58,41 +115,10 @@ local function statusline(sections, is_focused)
local current_section = status_builder[i] local current_section = status_builder[i]
local next_section = status_builder[i + 1] or {} local next_section = status_builder[i + 1] or {}
-- local splited_section = vim.split(current_section.data, '%%s{.-}') local section = apply_transitional_separators(previous_section,
-- if splited_section[1] == '' then table.remove(splited_section, 1) end current_section, next_section)
local count = 1
while count do table.insert(status, section)
local sep = current_section.data:match('%%s{(.-)}', count)
if not sep then break end
count = current_section.data:find('%%s{.-}', count)
if not count then break end
local prev = current_section.data:sub(1, count - 1)
local nxt = current_section.data:sub(count+4+#sep)
if not prev or #prev == 0 or count == 1 then prev = previous_section.data end
if prev ~= previous_section.data then
local last_hl = prev:match('.*%#(.-)#.-')
current_section.data = prev .. fill_section_separator(prev, nxt, sep, false)..'%#'..last_hl..'#'..nxt
else
current_section.data = fill_section_separator(prev, nxt, sep, true)..nxt
end
end
count = 1
while count do
local sep = current_section.data:match('%%S{(.-)}', count)
if not sep then break end
count = current_section.data:find('%%S{.-}', count)
if not count then break end
local prev = current_section.data:sub(1, count - 1)
local nxt = current_section.data:sub(count+4+#sep)
if not nxt or #nxt == 0 or count == #current_section.data then nxt = next_section.data end
if nxt ~= next_section.data then
current_section.data = prev .. fill_section_separator(prev, nxt, sep, false)..nxt
else
current_section.data = prev .. fill_section_separator(prev, nxt, sep, false)
end
count = count+4+#sep
end
table.insert(status, current_section.data)
end end
-- incase none of x,y,z was configured lets not fill whole statusline with a,b,c section -- incase none of x,y,z was configured lets not fill whole statusline with a,b,c section
if not half_passed then if not half_passed then

View File

@ -6,7 +6,7 @@ local highlight = require('lualine.highlight')
-- Returns formated string for a section -- Returns formated string for a section
function M.draw_section(section, section_name, is_focused) function M.draw_section(section, section_name, is_focused)
local highlight_name = highlight.format_highlight(is_focused, local highlight_name = highlight.format_highlight(is_focused,
'lualine_'..section_name) 'lualine_' .. section_name)
local status = {} local status = {}
for _, component in pairs(section) do for _, component in pairs(section) do
@ -25,9 +25,7 @@ function M.draw_section(section, section_name, is_focused)
-- Check through components to see when component separator need to be removed -- Check through components to see when component separator need to be removed
for component_no = #section, 1, -1 do for component_no = #section, 1, -1 do
if #status[component_no] > 0 then if #status[component_no] > 0 then first_component_no = component_no end
first_component_no = component_no
end
-- Remove component separator with highlight for last component -- Remove component separator with highlight for last component
if not last_component_found and #status[component_no] > 0 then if not last_component_found and #status[component_no] > 0 then
last_component_found = true last_component_found = true
@ -35,8 +33,9 @@ function M.draw_section(section, section_name, is_focused)
if section_name < 'c' then if section_name < 'c' then
if type(section[first_component_no].options.separator) ~= 'table' and if type(section[first_component_no].options.separator) ~= 'table' and
section[1].options.section_separators[1] ~= '' then section[1].options.section_separators[1] ~= '' then
status[component_no] = string.format('%s%%S{%s}',status[component_no], status[component_no] = string.format('%s%%S{%s}',
section[1].options.section_separators[1]) status[component_no], section[1]
.options.section_separators[1])
end end
end end
end end