Skip to content

Commit 37176bd

Browse files
committed
refactor(tabline): Changed how tabline is created
1 parent 5b42f8e commit 37176bd

File tree

1 file changed

+164
-0
lines changed

1 file changed

+164
-0
lines changed

lua/bars/tabline.lua

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
local tabline = {}
2+
local utils = require("bars.utils");
3+
local components = require("bars.components.tabline");
4+
5+
---@class tabline.config
6+
tabline.config = {
7+
---|fS
8+
9+
default = {
10+
parts = {
11+
{
12+
kind = "tabs",
13+
14+
active = {
15+
padding_left = " ",
16+
padding_right = " ",
17+
},
18+
inactive = {
19+
padding_left = " ",
20+
padding_right = " ",
21+
}
22+
}
23+
}
24+
}
25+
26+
---|fE
27+
};
28+
29+
---@type tabline.state
30+
tabline.state = {
31+
enable = true,
32+
attached = false
33+
};
34+
35+
--- Updates the configuration ID for the tabline.
36+
---@return string | nil
37+
tabline.update_id = function ()
38+
---|fS
39+
40+
local keys = vim.tbl_keys(tabline.config);
41+
local ignore = { "default" };
42+
table.sort(keys);
43+
44+
for _, key in ipairs(keys) do
45+
if vim.list_contains(ignore, key) then
46+
goto continue;
47+
elseif type(tabline.config[key]) ~= "table" then
48+
goto continue;
49+
end
50+
51+
local tmp = tabline.config[key];
52+
53+
if tmp.condition == true then
54+
return key;
55+
elseif pcall(tmp.condition --[[ @as function ]]) and tmp.condition() == true then
56+
return key;
57+
end
58+
59+
::continue::
60+
end
61+
62+
return "default";
63+
64+
---|fE
65+
end
66+
67+
--- Renders the tabline.
68+
---@return string
69+
tabline.render = function ()
70+
---|fS
71+
72+
if tabline.state.attached ~= true then
73+
return "";
74+
end
75+
76+
local tlID = vim.g.__tlID;
77+
78+
if not tlID then
79+
return "";
80+
end
81+
82+
local config = tabline.config[tlID];
83+
84+
if type(config) ~= "table" then
85+
return "";
86+
end
87+
88+
local _o = "%#Normal#";
89+
90+
for _, part in ipairs(config.parts or {}) do
91+
_o = _o .. components.get(part.kind, part, _o);
92+
end
93+
94+
return _o;
95+
96+
---|fE
97+
end
98+
99+
tabline.detach = function ()
100+
---|fS
101+
102+
tabline.state.attached = false;
103+
vim.o.tabline = vim.o.__tabline and vim.o.__tabline[1] or "";
104+
105+
vim.g.__tlID = nil;
106+
vim.g.__tabline = nil;
107+
108+
---|fE
109+
end
110+
111+
--- Attaches the tabline module.
112+
tabline.attach = function ()
113+
---|fS
114+
115+
if tabline.config.condition then
116+
local checked_condition, result = pcall(tabline.config.condition);
117+
118+
if checked_condition == false then
119+
tabline.detach();
120+
return;
121+
elseif result == false then
122+
tabline.detach();
123+
return;
124+
end
125+
end
126+
127+
local tlID = tabline.update_id();
128+
tabline.state.attached = true;
129+
130+
vim.g.__tlID = tlID;
131+
vim.g.__tabline = utils.to_constant(vim.o.tabline);
132+
133+
vim.o.tabline = "%!v:lua.require('bars.tabline').render()";
134+
135+
---|fE
136+
end
137+
138+
--- Cleans up invalid buffers and recalculates
139+
--- valid buffers config ID.
140+
tabline.clean = function ()
141+
if tabline.config.condition then
142+
local checked_condition, result = pcall(tabline.config.condition);
143+
144+
if checked_condition == false then
145+
tabline.detach();
146+
return;
147+
elseif result == false then
148+
tabline.detach();
149+
return;
150+
end
151+
end
152+
end
153+
154+
--- Sets up the tabline module.
155+
---@param config tabline.config | nil
156+
tabline.setup = function (config)
157+
if type(config) == "table" then
158+
tabline.config = vim.tbl_extend("force", tabline.config, config);
159+
end
160+
161+
vim.g.__tlID = tabline.update_id();
162+
end
163+
164+
return tabline;

0 commit comments

Comments
 (0)