@@ -123,40 +123,13 @@ tabline.state = {
123
123
attached = false
124
124
};
125
125
126
- --- Updates the configuration ID for the tabline.
127
- --- @return string | nil
128
- tabline .update_id = function ()
129
- --- | fS
130
-
131
- local keys = vim .tbl_keys (tabline .config );
132
- local ignore = { " default" };
133
- table.sort (keys );
134
-
135
- local ID = " default" ;
136
-
137
- for _ , key in ipairs (keys ) do
138
- if vim .list_contains (ignore , key ) then
139
- goto continue ;
140
- elseif type (tabline .config [key ]) ~= " table" then
141
- goto continue ;
142
- end
143
-
144
- --- @type tabline.opts
145
- local tmp = tabline .config [key ];
146
-
147
- if tmp .condition == true then
148
- ID = key ;
149
- elseif pcall (tmp .condition --[[ @as fun():boolean ]] ) and tmp .condition () == true then
150
- ID = key ;
151
- end
152
-
153
- :: continue::
126
+ tabline .check_condition = function ()
127
+ if not tabline .config .condition then
128
+ return true ;
154
129
end
155
130
156
- vim .g .__tlID = ID ;
157
- tabline .state .attached = true ;
158
-
159
- --- | fE
131
+ local can_call , cond = pcall (tabline .config .condition );
132
+ return can_call and cond ;
160
133
end
161
134
162
135
--- Renders the tabline.
@@ -166,23 +139,21 @@ tabline.render = function ()
166
139
167
140
local components = require (" bars.components.tabline" );
168
141
169
- if tabline .state .attached ~= true then
142
+ if tabline .check_condition () == false then
143
+ tabline .detach ();
170
144
return " " ;
171
145
end
172
146
173
- local tlID = vim .g .__tlID ;
174
-
175
- if not tlID then
176
- return " " ;
177
- end
147
+ tabline .update_id ();
178
148
149
+ local tlID = vim .g .__tlID or " default" ;
179
150
local config = tabline .config [tlID ];
180
151
181
152
if type (config ) ~= " table" then
182
153
return " " ;
183
154
end
184
155
185
- local _o = " %#Normal# " ;
156
+ local _o = " " ;
186
157
187
158
for _ , component in ipairs (config .components or {}) do
188
159
_o = _o .. components .get (component .kind , component , _o );
@@ -193,119 +164,76 @@ tabline.render = function ()
193
164
--- | fE
194
165
end
195
166
196
- tabline .can_detach = function ()
197
- if not tabline .config .condition then
198
- return false ;
199
- end
200
-
201
- local checked_condition , result = pcall (tabline .config .condition );
202
-
203
- if checked_condition == false then
204
- return false ;
205
- elseif result == false then
206
- return true ;
207
- end
208
- end
209
-
210
- tabline .detach = function ()
167
+ --- Attaches the tabline module.
168
+ tabline .start = function ()
211
169
--- | fS
212
170
213
- vim .schedule (function ()
214
- tabline .state .attached = false ;
215
-
216
- --- Cached tabline.
217
- --- @type string | nil
218
- local _tl = vim .g .__tabline or " " ;
219
-
220
- if _tl == " " or _tl == nil then
221
- --- Reset tabline.
222
- vim .cmd (" set tabline&" );
223
- else
224
- vim .api .nvim_set_option_value (
225
- " tabline" ,
226
- _tl ,
227
- {
228
- scope = " global"
229
- }
230
- );
231
- end
171
+ if tabline .state .enable == false then
172
+ return ;
173
+ end
232
174
233
- vim .g .__tlID = nil ;
234
- vim .g .__tabline = nil ;
235
- end );
175
+ vim .api .nvim_set_option_value (" tabline" , TBL , { scope = " global" })
236
176
237
177
--- | fE
238
178
end
239
179
240
- tabline .can_attach = function ()
241
- if tabline .state .enable ~= true then
242
- return false ;
243
- elseif tabline .state .attached == true then
244
- return false ;
245
- elseif tabline .config .condition == nil then
246
- return true ;
247
- end
248
-
249
- local checked_condition , result = pcall (tabline .config .condition );
180
+ tabline .stop = function ()
181
+ local _tabline = vim .api .nvim_get_option_value (" tabline" , { scope = " global" });
250
182
251
- if checked_condition == false then
252
- return true ;
253
- elseif result == false then
254
- return false ;
183
+ if _tabline ~= TBL and _tabline ~= " " then
184
+ return ;
255
185
end
186
+
187
+ vim .api .nvim_set_option_value (
188
+ " tabline" ,
189
+ vim .g .__tabline or " " ,
190
+ {
191
+ scope = " global"
192
+ }
193
+ );
256
194
end
257
195
258
- --- Attaches the tabline module .
259
- tabline .attach = function ()
196
+ --- Updates the configuration ID for {window} .
197
+ tabline .update_id = function ()
260
198
--- | fS
261
199
262
- if tabline .can_attach () == false then
263
- return ;
264
- end
265
-
266
- tabline .update_id ();
267
-
268
- vim .g .__tabline = vim .o .tabline == TBL and " " or vim .o .tabline ;
269
- vim .o .tabline = TBL ;
200
+ --- @type string[]
201
+ local keys = vim .tbl_keys (tabline .config );
202
+ --- @type string[]
203
+ local ignore = { " default" };
204
+ table.sort (keys );
270
205
271
- --- | fE
272
- end
206
+ local ID = " default" ;
273
207
274
- --- Cleans up invalid buffers and recalculates
275
- --- valid buffers config ID.
276
- tabline .clean = function ()
277
- vim .schedule (function ()
278
- if tabline .can_detach () then
279
- tabline .detach ();
208
+ for _ , key in ipairs (keys ) do
209
+ if vim .list_contains (ignore , key ) then
210
+ goto continue ;
211
+ elseif type (tabline .config [key ]) ~= " table" then
212
+ goto continue ;
280
213
end
281
- end );
282
- end
283
214
284
- ---- ------------------------------------------------------------------
215
+ local tmp = tabline . config [ key ];
285
216
286
- --- Enables *all* attached windows.
287
- tabline .Enable = function ()
288
- tabline .state .enable = true ;
217
+ if tmp .condition == true then
218
+ ID = key ;
219
+ elseif type (tmp .condition ) == " function" then
220
+ --- @diagnostic disable-next-line
221
+ local can_eval , val = pcall (tmp .condition , buffer , window );
289
222
290
- tabline .attach ();
291
- end
223
+ if can_eval and val then
224
+ ID = key ;
225
+ end
226
+ end
292
227
293
- --- Disables *all* attached windows.
294
- tabline .Disable = function ()
295
- tabline .detach ();
228
+ --- @diagnostic enable : undefined-field
296
229
297
- tabline . state . enable = false ;
298
- end
230
+ :: continue ::
231
+ end
299
232
300
- ---- ------------------------------------------------------------------
233
+ vim .g .__tlID = ID ;
234
+ tabline .state .attached = true ;
301
235
302
- --- Toggles tabline.
303
- tabline .Toggle = function ()
304
- if tabline .state .enable == true then
305
- tabline .Disable ();
306
- else
307
- tabline .Enable ();
308
- end
236
+ --- | fE
309
237
end
310
238
311
239
---- ------------------------------------------------------------------
0 commit comments