Skip to content

Commit da5a456

Browse files
committed
feat(winbar): Added new winbar
1 parent d924e1a commit da5a456

File tree

4 files changed

+781
-0
lines changed

4 files changed

+781
-0
lines changed

lua/bars/components/winbar.lua

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
local swC = {};
2+
local utils = require("bars.utils");
3+
4+
--- Node under cursor
5+
---@param buffer integer
6+
---@param window integer
7+
---@param main_config winbar.node
8+
---@return string
9+
swC.node = function (buffer, window, main_config)
10+
local check_parsers, parsers = pcall(vim.treesitter.get_parser, buffer);
11+
12+
if check_parsers == false then
13+
return "";
14+
elseif parsers == nil then
15+
return "";
16+
end
17+
18+
local ft = vim.bo[buffer].ft;
19+
local throttle = main_config.throttle or 50;
20+
21+
local before = vim.w[window].__node_time or 0;
22+
local old = vim.w[window].__node_data;
23+
local now = vim.uv.hrtime();
24+
25+
if old and (now - before) < (throttle * 1e6) then
26+
return old;
27+
end
28+
29+
local cursor = vim.api.nvim_win_get_cursor(window);
30+
local node = vim.treesitter.get_node({
31+
buffer = buffer,
32+
pos = { cursor[1] - 1, cursor[2] },
33+
34+
ignore_injections = false;
35+
});
36+
37+
local lang_config = utils.match(main_config, ft, {});
38+
39+
local lookup = main_config.lookup or 9;
40+
local _o = "";
41+
42+
while node do
43+
if lookup <= 0 then
44+
break;
45+
end
46+
47+
local item_config = utils.match(lang_config, node:type(), {});
48+
local has_sep = true;
49+
50+
if lookup == 1 then
51+
has_sep = false;
52+
elseif not node:parent() then
53+
has_sep = false;
54+
end
55+
56+
if item_config.ignore == true then
57+
goto ignore;
58+
end
59+
60+
_o = table.concat({
61+
has_sep == true and utils.set_hl(main_config.separator_hl) or "",
62+
has_sep == true and (main_config.separator or " > ") or "",
63+
64+
utils.set_hl(item_config.corner_left_hl or item_config.hl),
65+
item_config.corner_left or "",
66+
67+
utils.set_hl(item_config.padding_left_hl or item_config.hl),
68+
item_config.padding_left or "",
69+
70+
utils.set_hl(item_config.icon_hl or item_config.hl),
71+
item_config.icon or "",
72+
73+
utils.set_hl(item_config.hl),
74+
item_config.text or node:type() or "",
75+
76+
utils.set_hl(item_config.padding_right_hl or item_config.hl),
77+
item_config.padding_right or "",
78+
79+
utils.set_hl(item_config.corner_right_hl or item_config.hl),
80+
item_config.corner_right or "",
81+
82+
_o,
83+
});
84+
lookup = lookup - 1;
85+
86+
::ignore::
87+
88+
node = node:parent();
89+
end
90+
91+
if lookup <= 0 and node then
92+
local item_config = utils.match(lang_config, "__lookup", {});
93+
94+
_o = table.concat({
95+
96+
utils.set_hl(item_config.corner_left_hl or item_config.hl),
97+
item_config.corner_left or "",
98+
99+
utils.set_hl(item_config.padding_left_hl or item_config.hl),
100+
item_config.padding_left or "",
101+
102+
utils.set_hl(item_config.icon_hl or item_config.hl),
103+
item_config.icon or "",
104+
105+
utils.set_hl(item_config.hl),
106+
item_config.text or "",
107+
108+
utils.set_hl(item_config.padding_right_hl or item_config.hl),
109+
item_config.padding_right or "",
110+
111+
utils.set_hl(item_config.corner_right_hl or item_config.hl),
112+
item_config.corner_right or "",
113+
114+
utils.set_hl(main_config.separator_hl) or "",
115+
main_config.separator or " > ",
116+
117+
_o,
118+
});
119+
end
120+
121+
if type(main_config.max_width) == "number" then
122+
_o = table.concat({
123+
"%",
124+
0,
125+
".",
126+
main_config.max_width,
127+
"(",
128+
" ",
129+
_o,
130+
"%)"
131+
});
132+
else
133+
_o = " " .. _o;
134+
end
135+
136+
vim.w[window].__node_data = _o;
137+
vim.w[window].__node_time = now;
138+
139+
return _o;
140+
end
141+
142+
--- Returns the output of the section {name}.
143+
---@param name string
144+
---@param buffer integer
145+
---@param window integer
146+
---@param part_config table
147+
---@param statuscolumn string
148+
---@return string
149+
swC.get = function (name, buffer, window, part_config, statuscolumn)
150+
---|fS
151+
152+
if type(name) ~= "string" then
153+
--- Component doesn't exist.
154+
return "";
155+
elseif type(swC[name]) ~= "function" then
156+
--- Attempting to get internal property.
157+
return "";
158+
else
159+
if part_config.condition ~= nil then
160+
if part_config.condition == false then
161+
--- Part is disabled.
162+
return "";
163+
else
164+
local sucess, val = pcall(part_config.condition, buffer, window, statuscolumn);
165+
166+
if sucess == false then
167+
return "";
168+
elseif val == false then
169+
return "";
170+
end
171+
end
172+
end
173+
174+
local static_config = vim.deepcopy(part_config);
175+
176+
for key, value in pairs(static_config) do
177+
if type(value) ~= "function" then
178+
goto continue;
179+
end
180+
181+
local s_success, s_val = pcall(value, buffer, window, statuscolumn);
182+
183+
if s_success == false then
184+
static_config[key] = nil;
185+
else
186+
static_config[key] = s_val;
187+
end
188+
189+
::continue::
190+
end
191+
192+
--- Return component value.
193+
return swC[name](buffer, window, static_config, statuscolumn) or "";
194+
end
195+
196+
---|fE
197+
end
198+
199+
return swC

lua/bars/utils.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,15 @@ utils.set_hl = function (hl)
9191
end
9292
end
9393

94+
utils.to_constant = function (val)
95+
return setmetatable({}, {
96+
__index = function ()
97+
return val;
98+
end,
99+
__newindex = function ()
100+
end,
101+
__metatable = false
102+
});
103+
end
104+
94105
return utils;

0 commit comments

Comments
 (0)