Skip to content

Commit cab033b

Browse files
committed
refactor: Refactored how ID updates happen
1 parent d26a2c4 commit cab033b

File tree

4 files changed

+27
-99
lines changed

4 files changed

+27
-99
lines changed

lua/bars.lua

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ bars.config = {
99
statuscolumn = true,
1010
statusline = true,
1111
tabline = true,
12-
winbar = function ()
13-
if not _G.is_within_termux then
14-
--- The function doesn't
15-
--- exist.
16-
return true;
17-
else
18-
--- Winbar should be disabled
19-
--- while inside Termux.
20-
return _G.is_within_termux() == false;
21-
end
22-
end,
12+
-- winbar = function ()
13+
-- if not _G.is_within_termux then
14+
-- --- The function doesn't
15+
-- --- exist.
16+
-- return true;
17+
-- else
18+
-- --- Winbar should be disabled
19+
-- --- while inside Termux.
20+
-- return _G.is_within_termux() == false;
21+
-- end
22+
-- end,
2323

2424
---|fE
2525
};

lua/bars/statuscolumn.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@ statuscolumn.render = function ()
237237
return "";
238238
end
239239

240+
statuscolumn.update_id(window);
241+
240242
--- Statuscolumn config ID.
241243
---@type string
242244
local scID = vim.w[window].__scID or "default";
@@ -284,6 +286,11 @@ statuscolumn.start = function ()
284286
end
285287

286288
statuscolumn.attach = function (window)
289+
if statuscolumn.state.enable == false then
290+
statuscolumn.detach(window);
291+
return;
292+
end
293+
287294
local _statuscolumn = vim.wo[window].statuscolumn;
288295

289296
if _statuscolumn == STC then

lua/bars/statusline.lua

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,6 @@ statusline.render = function ()
484484
local window = vim.g.statusline_winid;
485485
local buffer = vim.api.nvim_win_get_buf(window);
486486

487-
statusline.update_id(window);
488-
489487
if vim.list_contains(statusline.config.ignore_filetypes, vim.bo[buffer].ft) then
490488
statusline.detach(window);
491489
return "";
@@ -497,6 +495,8 @@ statusline.render = function ()
497495
return "";
498496
end
499497

498+
statusline.update_id(window);
499+
500500
local slID = vim.w[window].__slID or "default";
501501
local config = statusline.config[slID];
502502

@@ -521,17 +521,6 @@ statusline.start = function ()
521521

522522
if statusline.state.enable == false then
523523
return;
524-
elseif statusline.config.condition then
525-
---@diagnostic disable-next-line
526-
local ran_cond, stat = pcall(statusline.config.condition, vim.api.nvim_get_current_buf(), vim.api.nvim_get_current_win());
527-
528-
if ran_cond == false or stat == false then
529-
return;
530-
end
531-
end
532-
533-
for _, window in ipairs(vim.api.nvim_list_wins()) do
534-
statusline.update_id(window);
535524
end
536525

537526
vim.api.nvim_set_option_value("statusline", STL, { scope = "global" })

lua/bars/winbar.lua

Lines changed: 7 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,6 @@ winbar.config = {
1010
ignore_filetypes = { "blink-cmp-menu" },
1111
ignore_buftypes = { "nofile", "help" },
1212

13-
condition = function ()
14-
---|fS
15-
16-
if _G.is_within_termux then
17-
--- Do NOT use this inside Termux.
18-
--- Screen space is too precious.
19-
return not _G.is_within_termux();
20-
end
21-
22-
return true;
23-
24-
---|fE
25-
end,
26-
2713
default = {
2814
components = {
2915
---|fS
@@ -627,8 +613,6 @@ winbar.render = function ()
627613
local window = vim.g.statusline_winid;
628614
local buffer = vim.api.nvim_win_get_buf(window);
629615

630-
winbar.update_id(window);
631-
632616
if vim.list_contains(winbar.config.ignore_filetypes, vim.bo[buffer].ft) then
633617
winbar.detach(window);
634618
return "";
@@ -640,6 +624,8 @@ winbar.render = function ()
640624
return "";
641625
end
642626

627+
winbar.update_id(window);
628+
643629
local wbID = vim.w[window].__wbbID or "default";
644630
local config = winbar.config[wbID];
645631

@@ -664,17 +650,6 @@ winbar.start = function ()
664650

665651
if winbar.state.enable == false then
666652
return;
667-
elseif winbar.config.condition then
668-
---@diagnostic disable-next-line
669-
local ran_cond, stat = pcall(winbar.config.condition, vim.api.nvim_get_current_buf(), vim.api.nvim_get_current_win());
670-
671-
if ran_cond == false or stat == false then
672-
return;
673-
end
674-
end
675-
676-
for _, window in ipairs(vim.api.nvim_list_wins()) do
677-
winbar.update_id(window);
678653
end
679654

680655
vim.api.nvim_set_option_value("winbar", WBR, { scope = "global" })
@@ -683,6 +658,11 @@ winbar.start = function ()
683658
end
684659

685660
winbar.attach = function (window)
661+
if winbar.state.enable == false then
662+
winbar.detach(window);
663+
return;
664+
end
665+
686666
local _statusline = vim.api.nvim_get_option_value("winbar", { scope = "local", win = window });
687667

688668
if _statusline == WBR then
@@ -789,50 +769,6 @@ winbar.update_id = function (window)
789769
---|fE
790770
end
791771

792-
--- Updates the configuration ID for {window}.
793-
---@param window integer
794-
---@return string | nil
795-
winbar.update_id = function (window)
796-
---|fS
797-
798-
if type(window) ~= "number" then
799-
return;
800-
elseif vim.api.nvim_win_is_valid(window) == false then
801-
return;
802-
end
803-
804-
local buffer = vim.api.nvim_win_get_buf(window);
805-
806-
local keys = vim.tbl_keys(winbar.config);
807-
local ignore = { "ignore_filetypes", "ignore_buftypes", "default" };
808-
table.sort(keys);
809-
810-
local ID = "default";
811-
812-
for _, key in ipairs(keys) do
813-
if vim.list_contains(ignore, key) then
814-
goto continue;
815-
elseif type(winbar.config[key]) ~= "table" then
816-
goto continue;
817-
end
818-
819-
local tmp = winbar.config[key];
820-
821-
if tmp.condition == true then
822-
ID = key;
823-
elseif pcall(tmp.condition --[[ @as function ]], buffer, window) and tmp.condition(buffer, window) == true then
824-
ID = key;
825-
end
826-
827-
::continue::
828-
end
829-
830-
vim.w[window].__wbID = ID;
831-
winbar.state.attached_windows[window] = true;
832-
833-
---|fE
834-
end
835-
836772

837773
--- Sets up the winbar module.
838774
---@param config winbar.config | boolean | nil
@@ -845,10 +781,6 @@ winbar.setup = function (config)
845781
winbar.state.enable = config;
846782
end
847783

848-
for window, _ in pairs(winbar.state.attached_windows) do
849-
vim.w[window].__wbID = winbar.update_id(window);
850-
end
851-
852784
---|fE
853785
end
854786

0 commit comments

Comments
 (0)