Skip to content

Commit 2d5de92

Browse files
committed
refactor(winbar): Added ability to reset winbar
If the cached value of the winbar is "" or `nil` then it is reset. feat(winbar): Added ability to Toggle winbar Use `winbar.toggle(winnr)` for specific window and `winbar.Toggle()` for globally.
1 parent 404dfb0 commit 2d5de92

File tree

1 file changed

+52
-12
lines changed

1 file changed

+52
-12
lines changed

lua/bars/winbar.lua

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
local winbar = {};
22
local components = require("bars.components.winbar");
33

4+
--- Custom winbar.
5+
---@type string
6+
local WBR = "%!v:lua.require('bars.winbar').render()";
7+
48
---@class winbar.config
59
winbar.config = {
610
---|fS
@@ -709,14 +713,25 @@ winbar.detach = function (window)
709713
vim.schedule(function ()
710714
vim.w[window].__wbID = nil;
711715

712-
vim.api.nvim_set_option_value(
713-
"winbar",
714-
vim.w[window].__winbar or vim.g.__winbar or "",
715-
{
716-
scope = "local",
717-
win = window
718-
}
719-
);
716+
--- Cached winbar.
717+
---@type string | nil
718+
local _wb = vim.w[window].__winbar or vim.g.__winbar or "";
719+
720+
if _wb == "" or _wb == nil then
721+
--- Reset winbar.
722+
vim.api.nvim_win_call(window, function ()
723+
vim.cmd("set winbar&");
724+
end);
725+
else
726+
vim.api.nvim_set_option_value(
727+
"winbar",
728+
_wb,
729+
{
730+
scope = "local",
731+
win = window
732+
}
733+
);
734+
end
720735

721736
winbar.state.attached_windows[window] = false;
722737
end);
@@ -763,16 +778,16 @@ winbar.attach = function (window)
763778
elseif winbar.can_attach(window) == false then
764779
return;
765780
elseif winbar.state.attached_windows[window] == true then
766-
if vim.wo[window].winbar == "%!v:lua.require('bars.winbar').render()" then
781+
if vim.wo[window].winbar == WBR then
767782
winbar.update_id(window);
768783
return;
769784
end
770785
end
771786

772787
winbar.update_id(window);
773788

774-
vim.w[window].__winbar = vim.wo[window].winbar;
775-
vim.wo[window].winbar = "%!v:lua.require('bars.winbar').render()";
789+
vim.w[window].__winbar = vim.wo[window].winbar == WBR and "" or vim.wo[window].winbar;
790+
vim.wo[window].winbar = WBR;
776791

777792
---|fE
778793
end
@@ -783,7 +798,7 @@ winbar.global_attach = function ()
783798
winbar.update_id(window);
784799
end
785800

786-
vim.o.winbar = "%!v:lua.require('bars.winbar').render()";
801+
vim.o.winbar = WBR;
787802
end
788803

789804
--- Cleans up invalid buffers and recalculates
@@ -802,6 +817,31 @@ winbar.clean = function ()
802817
---|fE
803818
end
804819

820+
--- Toggles state of given window.
821+
---@param window integer
822+
winbar.toggle = function (window)
823+
if type(window) ~= "number" or winbar.state.attached_windows[window] == nil then
824+
return;
825+
elseif winbar.state.attached_windows[window] == true then
826+
winbar.detach(window);
827+
else
828+
winbar.attach(window);
829+
end
830+
end
831+
832+
--- Toggles winbar **globally**.
833+
winbar.Toggle = function ()
834+
for window, state in pairs(winbar.state.attached_windows) do
835+
if state ~= nil then
836+
winbar.toggle(window);
837+
end
838+
end
839+
840+
--- true -> false,
841+
--- false -> true
842+
winbar.state.enable = not winbar.state.enable;
843+
end
844+
805845
--- Sets up the winbar module.
806846
---@param config winbar.config | nil
807847
winbar.setup = function (config)

0 commit comments

Comments
 (0)