1- --[[
2- # 🧬 Generics
3-
4- Base class for various bars & lines. All bars/lines extends from it.
5-
6- ## 📦 Variables
7-
8- | Name | Kind | Description |
9- |--------------------| ---------|-----------------------------------------------|
10- | `state` | table | Bars state. |
11- | `config` | table | Bars configuration. |
12- | `use_blank_output` | boolean | Uses blank output for windows without styles. |
13-
14- ## 🚧 Functions
15-
16- 1. `set_default_state`, Sets the config & state for a bar. **MUST BE USED WHEN CREATING A NEW BAR.**
17- 2. `current`, Gets current bar value. **MUST BE SET AFTER CREATION.**
18- 3. `should_attach`, Should a bar attach to a `window`?
19- 4. `should_detach`, Should a bar detach from a `window`?
20- 5. `set`, Sets bar/line for a `window`. **MUST BE SET AFTER CREATION.**
21- 6. `set`, Removes bar/line for a `window`. **MUST BE SET AFTER CREATION.**
22- 7. `attach`, Attaches to a `window`.
23- 8. `detach`, Detaches from a `window`.
24- 9. `enable`, Enables bar/line for a `window`.
25- 10. `disable`, Disables bar/line for a `window`.
26- 11. `toggle`, Toggles bar/line for a `window`.
27- 12. `update`, Update bar/line style for a `window`.
28- 13. `Toggle`, Toggle bar/line `globally`.
29- 14. `Enable`, Enable bar/line `globally`.
30- 15. `Disable`, Disable bar/line `globally`.
31- 16. `handle_new_window`, Handle events that causes a window to be attached/detached. Mainly `WinNew`..
32- 17. `update_style`, Updates the bar/line style for a `window`.
33- 18. `styled_component`, Gets output of a styled component of a bar/line.
34- 19. `get_styled_output`, Gets the styled bar/line for a window from a source.
35-
36- ## 📚 Usage
37-
38- ```lua
39- local bar = require("bars.generics").new();
40- bar:set_default_state();
41-
42- bar:attach(0);
43- bar:enable(0);
44- ```
45-
46- ]]
1+ --- @type bars.generic
2+ --- @diagnostic disable-next-line : missing-fields
473local generic = {};
484
49- --- @class bars.statusline.state
50- ---
51- --- @field enable boolean
52- --- @field window_state table<integer , boolean | nil>
53-
545function generic :set_default_state ()
556 self .state = {
567 enable = true ,
@@ -77,13 +28,8 @@ generic.var_name = "bars_style";
7728
7829---- ----------------------------------------------------------------------------
7930
80- --- Current value for a `bar`.
81- --- @param _ integer
8231function generic :current (_ ) return " " ; end
8332
84- --- Should we attach to `win`?
85- --- @param win integer
86- --- @return boolean
8733function generic :should_attach (win )
8834 if not self .state .enable then
8935 return false
@@ -120,9 +66,6 @@ function generic:should_attach (win)
12066 return true ;
12167end
12268
123- --- Should we detach from `win`?
124- --- @param win integer
125- --- @return boolean
12669function generic :should_detach (win )
12770 if not self .state .enable then
12871 return false
161104
162105---- ----------------------------------------------------------------------------
163106
164- --- Sets options for a specific bar.
165- --- @param _ integer
166107function generic :set (_ ) end
167-
168- --- Removes/resets options for a specific bar.
169- --- @param _ integer
170108function generic :remove (_ ) end
171109
172- --[[ Attaches a bar to `win`. ]]
173- --- @param win integer
174110function generic :attach (win )
175111 if self .state .window_state [win ] == true then return ; end
176112 self .state .window_state [win ] = true ;
@@ -179,17 +115,13 @@ function generic:attach (win)
179115 self :set (win );
180116end
181117
182- --[[ Detaches a bar from `win`. ]]
183- --- @param win integer
184118function generic :detach (win )
185119 if self .state .window_state [win ] == false then return ; end
186120 self .state .window_state [win ] = nil ;
187121
188122 self :remove (win );
189123end
190124
191- --[[ Enables a bar of `win`. ]]
192- --- @param win integer
193125function generic :enable (win )
194126 if not self .state .enable then
195127 return ;
@@ -203,8 +135,6 @@ function generic:enable (win)
203135 self :set (win );
204136end
205137
206- --[[ Disables a bar of `win`. ]]
207- --- @param win integer
208138function generic :disable (win )
209139 if not self .state .enable then
210140 return ;
@@ -216,8 +146,6 @@ function generic:disable (win)
216146 self :remove (win );
217147end
218148
219- --[[ Toggles a bar of `win`. ]]
220- --- @param win integer
221149function generic :toggle (win )
222150 if self .state .window_state [win ] then
223151 self :disable (win );
@@ -226,19 +154,22 @@ function generic:toggle (win)
226154 end
227155end
228156
229- --[[ Update a bar style of `win`. ]]
230- --- @param win integer
231157function generic :update (win )
232158 self :update_style (win );
233159end
234160
235161function generic :Toggle ()
236- for win , _ in pairs (self .state .window_state ) do
237- self :toggle (win );
238- end
162+ self :Enable ();
163+ self :Disable ();
239164end
240165
241166function generic :Enable ()
167+ if self .state .enable then
168+ return ;
169+ end
170+
171+ self .state .enable = true ;
172+
242173 for win , state in pairs (self .state .window_state ) do
243174 if state == false then
244175 self :enable (win );
@@ -247,15 +178,19 @@ function generic:Enable ()
247178end
248179
249180function generic :Disable ()
181+ if not self .state .enable then
182+ return ;
183+ end
184+
250185 for win , state in pairs (self .state .window_state ) do
251186 if state then
252187 self :disable (win );
253188 end
254189 end
190+
191+ self .state .enable = false ;
255192end
256193
257- --[[ Handles events that cause a bar to be `attached/detached`. ]]
258- --- @param win integer
259194function generic :handle_new_window (win )
260195 if not self .state .enable then
261196 return ;
@@ -268,7 +203,6 @@ function generic:handle_new_window (win)
268203 end
269204end
270205
271- --- @param win integer
272206function generic :update_style (win )
273207 local buf = vim .api .nvim_win_get_buf (win );
274208
@@ -299,12 +233,6 @@ function generic:update_style(win)
299233 vim .api .nvim_win_set_var (win , " _" .. self .var_name , style );
300234end
301235
302- --- @param win integer
303- --- @param buf integer
304- --- @param components table<string , function>
305- --- @param entry table
306- --- @param last string
307- --- @return string
308236function generic :styled_component (win , buf , components , entry , last )
309237 if entry .condition then
310238 if entry .condition == false then
@@ -346,9 +274,6 @@ function generic:styled_component (win, buf, components, entry, last)
346274 end
347275end
348276
349- --- @param win integer
350- --- @param components table<string , function>
351- --- @return string
352277function generic :get_styled_output (win , components )
353278 local buf = vim .api .nvim_win_get_buf (win );
354279
@@ -391,7 +316,10 @@ generic.__index = generic;
391316
392317local builder = {};
393318
319+ --[[ Creates a new `bar`. ]]
320+ --- @return bars.generic
394321builder .new = function ()
322+ --- @type bars.generic
395323 local out = setmetatable ({}, generic );
396324 out :set_default_state ();
397325
0 commit comments