Skip to content
This repository has been archived by the owner on Sep 20, 2023. It is now read-only.

Commit

Permalink
simplify code a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
famiu committed Sep 26, 2021
1 parent b25c672 commit 8aa26e9
Showing 1 changed file with 49 additions and 54 deletions.
103 changes: 49 additions & 54 deletions lua/feline/generator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -349,14 +349,17 @@ function M.generate_statusline(winid)

local sections = components_table[statusline_type]

-- Flatten sections to a single table containing all components. Parse the components and
-- store the parsed value alongside the original value. Also store the section number and
-- the component number of the component so that the components can later be placed in order
-- Also calculate the length of the statusline while doing all of that
local all_components = {}
-- Parse all components in the sections and also store the indices of every component so
-- both the sections and parsed_sections tables can be accessed later on after components
-- are sorted in order of priority
-- Also calculate statusline length while doing all of that
local parsed_sections = {}
local component_indices = {}
local statusline_length = 0

for i, section in ipairs(sections) do
parsed_sections[i] = {}

for j, component in ipairs(section) do
local ok, result = pcall(parse_component, component, winid)

Expand All @@ -367,49 +370,49 @@ function M.generate_statusline(winid)
j, i, statusline_type, result
))

result = ''
result = { str = '', len = 0 }
end

local component_info = {
value = component,
parsed_value = result,
section_index = i,
component_index = j
}

all_components[#all_components+1] = component_info
statusline_length = statusline_length + component_info.parsed_value.len
parsed_sections[i][j] = result
component_indices[#component_indices+1] = {i, j}
statusline_length = statusline_length + result.len
end
end

local win_width = api.nvim_win_get_width(winid)

-- If statusline length is larger than the window width, sort the components in ascending
-- order of priority and then truncate the components one by one using by their
-- short_provider or hiding them entirely until the statusline fits within the window
-- If statusline length is larger than the window width, sort the component indices
-- in ascending order of priority of the components they refer to
-- Then truncate the components one by one using by their short_provider or hiding them
-- entirely until the statusline fits within the window
if statusline_length > win_width then
table.sort(all_components, function(a, b)
return (a.value.priority or 0) < (b.value.priority or 0)
table.sort(component_indices, function(a, b)
-- Access the original component through the sections table using the indices
return (sections[a[1]][a[2]].priority or 0) < (sections[b[1]][b[2]].priority or 0)
end)

-- If short_provider exists, use it, else hide the component if the truncate_hide
-- component value is set to true
for _, component_info in ipairs(all_components) do
if component_info.value.short_provider then
-- Get new parsed value using the short provider and calculate the length
-- difference between the two values, and if it's greater than 0, use the new
-- value instead of the old one
local new_parsed_value = parse_component(component_info.value, winid, true)
local length_difference = component_info.parsed_value.len - new_parsed_value.len
for _, indices in ipairs(component_indices) do
-- Access the original and parsed values using the indices
local component = sections[indices[1]][indices[2]]
local parsed_component = parsed_sections[indices[1]][indices[2]]

-- If short_provider exists, use it, else hide the component if the truncate_hide
-- component value is set to true
if component.short_provider then
-- Get new parsed component value using the short_provider and calculate the
-- length difference between the two values, and if it's greater than 0, use
-- the new value instead of the old one
local parsed_component_new = parse_component(component, winid, true)
local length_difference = parsed_component.len - parsed_component_new.len

if length_difference > 0 then
-- Update statusline length and replace old parsed value with new one
statusline_length = statusline_length - length_difference
component_info.parsed_value = new_parsed_value
parsed_sections[indices[1]][indices[2]] = parsed_component_new
end
elseif component_info.value.truncate_hide then
statusline_length = statusline_length - component_info.parsed_value.len
component_info.parsed_value = {str = '', len = 0}
elseif component.truncate_hide then
statusline_length = statusline_length - parsed_component.len
parsed_sections[indices[1]][indices[2]] = {str = '', len = 0}
end

if statusline_length <= win_width then break end
Expand All @@ -418,38 +421,30 @@ function M.generate_statusline(winid)
-- If statusline still doesn't fit, start removing all components with truncate_hide
-- set to true regardless of whether they have short_provider or not
if statusline_length > win_width then
for _, component_info in ipairs(all_components) do
if component_info.value.truncate_hide then
statusline_length = statusline_length - component_info.parsed_value.len
component_info.parsed_value = {str = '', len = 0}
for _, indices in ipairs(component_indices) do
if sections[indices[1]][indices[2]].truncate_hide then
statusline_length = statusline_length -
parsed_sections[indices[1]][indices[2]].len

parsed_sections[indices[1]][indices[2]] = {str = '', len = 0}
end

if statusline_length <= win_width then break end
end
end
end

-- Now use the section number and component number to put the components strings in their
-- respective locations
local parsed_sections = {}

for _, component_info in ipairs(all_components) do
local section_index, component_index, component_str = component_info.section_index,
component_info.component_index,
component_info.parsed_value.str

if not parsed_sections[section_index] then
parsed_sections[section_index] = {}
end

parsed_sections[section_index][component_index] = component_str
end

-- Concatenate all components in each section to get a string for each section
local section_strs = {}

for i, parsed_section in ipairs(parsed_sections) do
section_strs[i] = table.concat(parsed_section)
local component_strs = {}

for j, parsed_component in ipairs(parsed_section) do
component_strs[j] = parsed_component.str
end

section_strs[i] = table.concat(component_strs)
end

-- Finally, concatenate all sections to get the statusline string
Expand Down

0 comments on commit 8aa26e9

Please sign in to comment.