Debian Codegraph @

lua/lualine/utils/section.lua

raw
1 -- Copyright (c) 2020-2021 hoob3rt
2 -- MIT license, see LICENSE for more details.
3 local M = {}
4 local require = require('lualine_require').require
5 local utils = require('lualine.utils.utils')
6 local color_utils = require('lualine.utils.color_utils')
7 local highlight = require('lualine.highlight')
8
9 ---runs draw function on components in section
10 ---handles separator edge cases :/
11 ---also handles default transitional separators at section boundaries
12 ---(why? I don't know)
13 ---@param section table list of components
14 ---@param section_name string used for getting proper hl
15 ---@param is_focused boolean
16 ---@return string formatted string for a section
17 --TODO Clean this up this does lots of messy stuff.
18 function M.draw_section(section, section_name, is_focused)
19 local highlight_name = highlight.format_highlight(section_name, is_focused)
20
21 local status = {}
22 for _, component in pairs(section) do
23 -- load components into status table
24 if type(component) ~= 'table' or (type(component) == 'table' and not component.component_no) then
25 return '' -- unknown element in section. section possibly not yet loaded
26 end
27 table.insert(status, component:draw(highlight_name, is_focused))
28 end
29
30 local section_color = utils.extract_highlight_colors(string.match(highlight_name, '%%#(.*)#'))
31
32 -- Flags required for knowing when to remove component separator
33 local strip_next_component = false
34 local last_component_found = false
35 local first_component_no = #section
36
37 -- Check through components to see when component separator need to be removed
38 for component_no = #section, 1, -1 do
39 if #status[component_no] > 0 then
40 first_component_no = component_no
41 end
42 -- Remove component separator with highlight for last component
43 if not last_component_found and #status[component_no] > 0 then
44 last_component_found = true
45 status[component_no] = section[component_no]:strip_separator()
46 if section_name < 'c' then
47 if
48 type(section[first_component_no].options.separator) ~= 'table'
49 and (section[1].options.section_separators.left ~= nil and section[1].options.section_separators.left ~= '')
50 then
51 status[component_no] =
52 string.format('%s%%Z{%s}', status[component_no], section[1].options.section_separators.left)
53 end
54 end
55 end
56 -- Remove component separator when color option is used in next component
57 if strip_next_component then
58 strip_next_component = false
59 status[component_no] = section[component_no]:strip_separator()
60 end
61 -- Remove component separator when color option is used to color background
62 if
63 (
64 type(section[component_no].options.color) == 'table'
65 and section[component_no].options.color.bg
66 and color_utils.sanitize_color(section[component_no].options.color.bg)
67 ~= color_utils.sanitize_color(section_color.bg)
68 )
69 or type(section[component_no].options.color) == 'string'
70 or (
71 type(section[component_no].options.color) == 'function'
72 and section[component_no].color_fn_cache
73 and section[component_no].color_fn_cache.bg
74 and color_utils.sanitize_color(section[component_no].color_fn_cache.bg)
75 ~= color_utils.sanitize_color(section_color.bg)
76 )
77 then
78 strip_next_component = true
79 status[component_no] = section[component_no]:strip_separator()
80 end
81
82 if section[component_no].strip_previous_separator == true then
83 strip_next_component = true
84 end
85 end
86
87 local left_separator_string = ''
88 if
89 section_name > 'x'
90 and section[first_component_no]
91 and type(section[first_component_no].options.separator) ~= 'table'
92 and (section[1].options.section_separators.right ~= nil and section[1].options.section_separators.right ~= '')
93 then
94 left_separator_string = string.format(
95 '%%z{%s}',
96 section[first_component_no].options.ls_separator or section[1].options.section_separators.right
97 )
98 end
99
100 -- Remove empty strings from status
101 status = utils.list_shrink(status)
102 local status_str = table.concat(status)
103
104 if #status_str == 0 then
105 return ''
106 end
107
108 local needs_hl
109
110 local find_start_trans_sep_start, find_start_trans_sep_end = status_str:find('^%%z{.-}')
111 if find_start_trans_sep_start then
112 -- the section doesn't need to be prepended with default hl when sections
113 -- first component has transitional sep
114 needs_hl = status_str:find('^%%#', find_start_trans_sep_end + 1)
115 else
116 needs_hl = status_str:find('^%%#')
117 end
118
119 if needs_hl then
120 -- Don't prepend with old highlight when the component changes it immediately
121 return left_separator_string .. status_str
122 else
123 return left_separator_string .. highlight_name .. status_str
124 end
125 end
126
127 return M