Skip to content

Commit 7e5777b

Browse files
committed
rrfactor: Rewrote parts of the tabline module
1 parent fa288e8 commit 7e5777b

File tree

2 files changed

+68
-140
lines changed

2 files changed

+68
-140
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/tabline.lua

Lines changed: 57 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -123,40 +123,13 @@ tabline.state = {
123123
attached = false
124124
};
125125

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;
154129
end
155130

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;
160133
end
161134

162135
--- Renders the tabline.
@@ -166,23 +139,21 @@ tabline.render = function ()
166139

167140
local components = require("bars.components.tabline");
168141

169-
if tabline.state.attached ~= true then
142+
if tabline.check_condition() == false then
143+
tabline.detach();
170144
return "";
171145
end
172146

173-
local tlID = vim.g.__tlID;
174-
175-
if not tlID then
176-
return "";
177-
end
147+
tabline.update_id();
178148

149+
local tlID = vim.g.__tlID or "default";
179150
local config = tabline.config[tlID];
180151

181152
if type(config) ~= "table" then
182153
return "";
183154
end
184155

185-
local _o = "%#Normal#";
156+
local _o = "";
186157

187158
for _, component in ipairs(config.components or {}) do
188159
_o = _o .. components.get(component.kind, component, _o);
@@ -193,119 +164,76 @@ tabline.render = function ()
193164
---|fE
194165
end
195166

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 ()
211169
---|fS
212170

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
232174

233-
vim.g.__tlID = nil;
234-
vim.g.__tabline = nil;
235-
end);
175+
vim.api.nvim_set_option_value("tabline", TBL, { scope = "global" })
236176

237177
---|fE
238178
end
239179

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" });
250182

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;
255185
end
186+
187+
vim.api.nvim_set_option_value(
188+
"tabline",
189+
vim.g.__tabline or "",
190+
{
191+
scope = "global"
192+
}
193+
);
256194
end
257195

258-
--- Attaches the tabline module.
259-
tabline.attach = function ()
196+
--- Updates the configuration ID for {window}.
197+
tabline.update_id = function ()
260198
---|fS
261199

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);
270205

271-
---|fE
272-
end
206+
local ID = "default";
273207

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;
280213
end
281-
end);
282-
end
283214

284-
----------------------------------------------------------------------
215+
local tmp = tabline.config[key];
285216

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);
289222

290-
tabline.attach();
291-
end
223+
if can_eval and val then
224+
ID = key;
225+
end
226+
end
292227

293-
--- Disables *all* attached windows.
294-
tabline.Disable = function ()
295-
tabline.detach();
228+
---@diagnostic enable:undefined-field
296229

297-
tabline.state.enable = false;
298-
end
230+
::continue::
231+
end
299232

300-
----------------------------------------------------------------------
233+
vim.g.__tlID = ID;
234+
tabline.state.attached = true;
301235

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
309237
end
310238

311239
----------------------------------------------------------------------

0 commit comments

Comments
 (0)