1
1
local winbar = {};
2
2
local components = require (" bars.components.winbar" );
3
3
4
+ --- Custom winbar.
5
+ --- @type string
6
+ local WBR = " %!v:lua.require('bars.winbar').render()" ;
7
+
4
8
--- @class winbar.config
5
9
winbar .config = {
6
10
--- | fS
@@ -709,14 +713,25 @@ winbar.detach = function (window)
709
713
vim .schedule (function ()
710
714
vim .w [window ].__wbID = nil ;
711
715
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
720
735
721
736
winbar .state .attached_windows [window ] = false ;
722
737
end );
@@ -763,16 +778,16 @@ winbar.attach = function (window)
763
778
elseif winbar .can_attach (window ) == false then
764
779
return ;
765
780
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
767
782
winbar .update_id (window );
768
783
return ;
769
784
end
770
785
end
771
786
772
787
winbar .update_id (window );
773
788
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 ;
776
791
777
792
--- | fE
778
793
end
@@ -783,7 +798,7 @@ winbar.global_attach = function ()
783
798
winbar .update_id (window );
784
799
end
785
800
786
- vim .o .winbar = " %!v:lua.require('bars.winbar').render() " ;
801
+ vim .o .winbar = WBR ;
787
802
end
788
803
789
804
--- Cleans up invalid buffers and recalculates
@@ -802,6 +817,31 @@ winbar.clean = function ()
802
817
--- | fE
803
818
end
804
819
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
+
805
845
--- Sets up the winbar module.
806
846
--- @param config winbar.config | nil
807
847
winbar .setup = function (config )
0 commit comments