Debian Codegraph @

lua/lualine/component.lua

raw
1 -- Copyright (c) 2020-2021 shadmansaleh
2 -- MIT license, see LICENSE for more details.
3 local lualine_require = require('lualine_require')
4 local require = lualine_require.require
5 local M = require('lualine.utils.class'):extend()
6 local modules = lualine_require.lazy_require {
7 highlight = 'lualine.highlight',
8 utils_notices = 'lualine.utils.notices',
9 fn_store = 'lualine.utils.fn_store',
10 }
11
12 -- Used to provide a unique id for each component
13 local component_no = 1
14 function M._reset_components()
15 component_no = 1
16 end
17
18 -- variable to store component output for manipulation
19 M.status = ''
20
21 function M:__tostring()
22 local str = 'Component: ' .. self.options.component_name
23 if self.debug then
24 str = str .. '\n---------------------\n' .. vim.inspect(self)
25 end
26 return str
27 end
28
29 M.__is_lualine_component = true
30
31 ---initialize new component
32 ---@param options table options for component
33 function M:init(options)
34 self.options = options or {}
35 component_no = component_no + 1
36 if not self.options.component_name then
37 self.options.component_name = tostring(component_no)
38 end
39 self.component_no = component_no
40 self:set_separator()
41 self:create_option_highlights()
42 self:set_on_click()
43 end
44
45 ---sets the default separator for component based on whether the component
46 ---is in left sections or right sections when separator option is omitted.
47 function M:set_separator()
48 if self.options.separator == nil then
49 if self.options.component_separators then
50 if self.options.self.section < 'x' then
51 self.options.separator = self.options.component_separators.left
52 else
53 self.options.separator = self.options.component_separators.right
54 end
55 end
56 end
57 end
58
59 ---creates hl group from color option
60 function M:create_option_highlights()
61 -- set custom highlights
62 if self.options.color then
63 self.options.color_highlight = self:create_hl(self.options.color)
64 end
65 -- setup icon highlight
66 if type(self.options.icon) == 'table' and self.options.icon.color then
67 self.options.icon_color_highlight = self:create_hl(self.options.icon.color)
68 end
69 end
70
71 ---Setup on click function so they can be added during drawing.
72 function M:set_on_click()
73 if self.options.on_click ~= nil then
74 if vim.fn.has('nvim-0.8') == 0 then
75 modules.utils_notices.add_notice(
76 '### Options.on_click\nSorry `on_click` can only be used in neovim 0.8 or higher.\n'
77 )
78 self.options.on_click = nil
79 return
80 end
81 self.on_click_id = modules.fn_store.register_fn(self.component_no, self.options.on_click)
82 end
83 end
84
85 ---adds spaces to left and right of a component
86 function M:apply_padding()
87 local padding = self.options.padding
88 local l_padding, r_padding
89 if padding == nil then
90 padding = 1
91 end
92 if type(padding) == 'number' then
93 l_padding, r_padding = padding, padding
94 elseif type(padding) == 'table' then
95 l_padding, r_padding = padding.left, padding.right
96 end
97 if l_padding then
98 if self.status:find('%%#.*#') == 1 then
99 -- When component has changed the highlight at beginning
100 -- we will add the padding after the highlight
101 local pre_highlight = vim.fn.matchlist(self.status, [[\(%#.\{-\}#\)]])[2]
102 self.status = pre_highlight .. string.rep(' ', l_padding) .. self.status:sub(#pre_highlight + 1, #self.status)
103 else
104 self.status = string.rep(' ', l_padding) .. self.status
105 end
106 end
107 if r_padding then
108 if self.status:reverse():find('%*%%.*#.*#%%') == 1 then
109 -- When component has changed the highlight at the end
110 -- we will add the padding before the highlight terminates
111 self.status = self.status:sub(1, -3) .. string.rep(' ', r_padding) .. self.status:sub(-2, -1)
112 else
113 self.status = self.status .. string.rep(' ', r_padding)
114 end
115 end
116 end
117
118 ---applies custom highlights for component
119 function M:apply_highlights(default_highlight)
120 if self.options.color_highlight then
121 local hl_fmt
122 hl_fmt, M.color_fn_cache = self:format_hl(self.options.color_highlight)
123 self.status = hl_fmt .. self.status
124 end
125 if type(self.options.separator) ~= 'table' and self.status:find('%%#') then
126 -- Apply default highlight only when we aren't applying trans sep and
127 -- the component has changed it's hl. Since we won't be applying
128 -- regular sep in those cases so ending with default hl isn't necessary
129 self.status = self.status .. default_highlight
130 -- Also put it in applied sep so when sep get striped so does the hl
131 self.applied_separator = default_highlight
132 end
133 -- Prepend default hl when the component doesn't start with hl otherwise
134 -- color in previous component can cause side effect
135 if not self.status:find('^%%#') then
136 self.status = default_highlight .. self.status
137 end
138 end
139
140 ---apply icon to component (appends/prepends component with icon)
141 function M:apply_icon()
142 local icon = self.options.icon
143 if self.options.icons_enabled and icon then
144 if type(icon) == 'table' then
145 icon = icon[1]
146 end
147 if
148 self.options.icon_color_highlight
149 and type(self.options.icon) == 'table'
150 and self.options.icon.align == 'right'
151 then
152 self.status = table.concat {
153 self.status,
154 ' ',
155 self:format_hl(self.options.icon_color_highlight),
156 icon,
157 self:get_default_hl(),
158 }
159 elseif self.options.icon_color_highlight then
160 self.status = table.concat {
161 self:format_hl(self.options.icon_color_highlight),
162 icon,
163 self:get_default_hl(),
164 ' ',
165 self.status,
166 }
167 elseif type(self.options.icon) == 'table' and self.options.icon.align == 'right' then
168 self.status = table.concat({ self.status, icon }, ' ')
169 else
170 self.status = table.concat({ icon, self.status }, ' ')
171 end
172 end
173 end
174
175 ---apply separator at end of component only when
176 ---custom highlights haven't affected background
177 function M:apply_separator()
178 local separator = self.options.separator
179 if type(separator) == 'table' then
180 if self.options.separator[2] == '' then
181 if self.options.self.section < 'x' then
182 separator = self.options.component_separators.left
183 else
184 separator = self.options.component_separators.right
185 end
186 else
187 return
188 end
189 end
190 if separator and #separator > 0 then
191 self.status = self.status .. separator
192 self.applied_separator = self.applied_separator .. separator
193 end
194 end
195
196 ---apply transitional separator for the component
197 function M:apply_section_separators()
198 if type(self.options.separator) ~= 'table' then
199 return
200 end
201 if self.options.separator.left ~= nil and self.options.separator.left ~= '' then
202 self.status = string.format('%%z{%s}%s', self.options.separator.left, self.status)
203 self.strip_previous_separator = true
204 end
205 if self.options.separator.right ~= nil and self.options.separator.right ~= '' then
206 self.status = string.format('%s%%Z{%s}', self.status, self.options.separator.right)
207 end
208 end
209
210 ---Add on click function description to already drawn item
211 function M:apply_on_click()
212 if self.on_click_id then
213 self.status = self:format_fn(self.on_click_id, self.status)
214 end
215 end
216
217 ---remove separator from tail of this component.
218 ---called by lualine.utils.sections.draw_section to manage unnecessary separators
219 function M:strip_separator()
220 if not self.applied_separator then
221 self.applied_separator = ''
222 end
223 self.status = self.status:sub(1, (#self.status - #self.applied_separator))
224 self.applied_separator = nil
225 return self.status
226 end
227
228 function M:get_default_hl()
229 if self.options.color_highlight then
230 return self:format_hl(self.options.color_highlight)
231 elseif self.default_hl then
232 return self.default_hl
233 else
234 return modules.highlight.format_highlight(self.options.self.section)
235 end
236 end
237
238 ---create a lualine highlight for color
239 ---@param color table|string|function defined color for hl
240 ---@param hint string|nil hint for hl name
241 ---@return table an identifier to later retrieve the hl for application
242 function M:create_hl(color, hint)
243 hint = hint and self.options.component_name .. '_' .. hint or self.options.component_name
244 return modules.highlight.create_component_highlight_group(color, hint, self.options, false)
245 end
246
247 ---Get stl formatted hl group for hl_token
248 ---@param hl_token table identifier received from create_hl or create_component_highlight_group
249 ---@return string stl formatted hl group for hl_token
250 function M:format_hl(hl_token)
251 return modules.highlight.component_format_highlight(hl_token)
252 end
253
254 ---Wrap str with click format for function of id
255 ---@param id number
256 ---@param str string
257 ---@return string
258 function M:format_fn(id, str)
259 return string.format("%%%d@v:lua.require'lualine.utils.fn_store'.call_fn@%s%%T", id, str)
260 end
261
262 -- luacheck: push no unused args
263 ---actual function that updates a component. Must be overwritten with component functionality
264 function M:update_status(is_focused) end
265 -- luacheck: pop
266
267 ---driver code of the class
268 ---@param default_highlight string default hl group of section where component resides
269 ---@param is_focused boolean|number whether drawing for active or inactive statusline.
270 ---@return string stl formatted rendering string for component
271 function M:draw(default_highlight, is_focused)
272 self.status = ''
273 self.applied_separator = ''
274
275 if self.options.cond ~= nil and self.options.cond() ~= true then
276 return self.status
277 end
278 self.default_hl = default_highlight
279 local status = self:update_status(is_focused)
280 if self.options.fmt then
281 status = self.options.fmt(status or '', self)
282 end
283 if type(status) == 'string' and (#status > 0 or self.options.draw_empty) then
284 self.status = status
285 if #status > 0 then
286 self:apply_icon()
287 self:apply_padding()
288 end
289 self:apply_on_click()
290 self:apply_highlights(default_highlight)
291 self:apply_section_separators()
292 self:apply_separator()
293 end
294 return self.status
295 end
296
297 return M