Skip to content

Commit

Permalink
add optional to auto remove on add_component
Browse files Browse the repository at this point in the history
  • Loading branch information
zztrieuzz committed Oct 20, 2022
1 parent a34d139 commit c743248
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
7 changes: 7 additions & 0 deletions lua/windline/cache_utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,12 @@ M.set_cache_buffer = function(bufnr, variable_name, value)
d_value[bufnr][variable_name] = value
end

M.get_cache_buffer = function(bufnr, variable_name)
if not d_value[bufnr] then
d_value[bufnr] = {}
end
return d_value[bufnr][variable_name]
end

_G.WindLine.cache_buffer_cb = M.cache_buffer_cb
return M
17 changes: 11 additions & 6 deletions lua/windline/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ end
-- position string|number position can be an index or a name of previous component
--- position == left add component before a first divider (%=)
--- position == right add component after a first divider (%=)
--- auto_remove auto remove old component with same name
--- colors_name table a modifer colors to add to a new component
--- autocmd boolean It use an auto command to add component to default statusline.
M.add_component = function(component, opt)
Expand All @@ -389,6 +390,9 @@ M.add_component = function(component, opt)
M.add_autocmd_component(component, opt)
return
end
if opt.auto_remove then
M.remove_component(opt)
end
local line = M.get_statusline_ft(opt.filetype or '') or M.default_line
local added = false
local status_line = line[opt.kind or 'active']
Expand Down Expand Up @@ -453,6 +457,10 @@ M.add_autocmd_component = function(component, opts)
component.name = opts.name or component.name
opts.name = component.name
end
if opts.auto_remove then
M.remove_auto_component(opts)
M.remove_component(opts)
end
M.state.auto_comps[component.name] = {
component = component,
is_added = false,
Expand Down Expand Up @@ -482,12 +490,9 @@ M.check_autocmd_component = function(bufnr)
end

M.remove_auto_component = function(opts)
for index, value in pairs(M.state.auto_comps) do
if value.opts.name == opts.name then
table.remove(M.state.auto_comps, index)
break
end
end
M.state.auto_comps = vim.tbl_filter(function(value)
return value.opts.name ~= opts.name
end, M.state.auto_comps)
if #M.state.auto_comps == 0 then
M.state.auto_comps = nil
end
Expand Down

0 comments on commit c743248

Please sign in to comment.