Skip to content

Commit 339b0bb

Browse files
committed
feat(tabline): Added a tab list for the tabline
Also supports mouse clicks!
1 parent 6834630 commit 339b0bb

File tree

1 file changed

+120
-14
lines changed

1 file changed

+120
-14
lines changed

lua/bars/components/tabline.lua

Lines changed: 120 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
local tlC = {};
22
local utils = require("bars.utils");
33

4+
--- Assuming the length of an array is
5+
--- {max}, gets the relative index from
6+
--- {val}.
7+
---@param max integer
8+
---@param val integer
9+
---@return integer
10+
local function wrapped_index (max, val)
11+
---+
12+
if val < 1 then
13+
if math.abs(val) < max then
14+
return max + val;
15+
else
16+
return 1;
17+
end
18+
elseif val > max then
19+
return (val % max) == 0 and max or (val % max);
20+
else
21+
return val;
22+
end
23+
---_
24+
end
25+
426
tlC.empty = function (config)
527
return table.concat({
628
utils.set_hl(config.hl),
@@ -12,37 +34,121 @@ tlC.tabs = function (config)
1234
local tabs = vim.api.nvim_list_tabpages();
1335
local _o = "";
1436

15-
for t, tab in ipairs(tabs) do
37+
if not vim.g.__bars_tabpage_from then
38+
vim.g.__bars_tabpage_from = 1;
39+
elseif vim.g.__bars_tabpage_from > #tabs then
40+
vim.g.__bars_tabpage_from = 1;
41+
end
42+
43+
---@type integer Start index. Must be above 0;
44+
local from = math.max(vim.g.__bars_tabpage_from, 1);
45+
---@type integer Number of tabs to show.
46+
local max = config.max or 5;
47+
48+
if from ~= 1 then
49+
_o = table.concat({
50+
_o,
51+
52+
"%@v:lua.__tab_from_decrease@",
53+
utils.create_segmant(config.nav_left_text, config.nav_left_hl),
54+
"%X"
55+
});
56+
end
57+
58+
local wrapped = false;
59+
60+
for t = from, from + (max - 1), 1 do
61+
local tab_index = wrapped_index(#tabs, t);
62+
local tab = tabs[tab_index];
63+
64+
if t > #tabs then
65+
if tab_index == wrapped_index(#tabs, from) and wrapped == true then
66+
--- Do not wrap around multiple
67+
--- times.
68+
break;
69+
elseif from == 1 and #tabs < max then
70+
--- If we are showing tabs from
71+
--- the start and the number of
72+
--- tabs are less than the amount
73+
--- we can show, then don't wrap.
74+
break;
75+
elseif tab_index == 1 then
76+
--- Wrap marker should only be added
77+
--- to the 1st entry.
78+
wrapped = true;
79+
80+
_o = table.concat({
81+
_o,
82+
utils.set_hl(config.overflow_hl),
83+
config.overflow_text or ""
84+
});
85+
else
86+
_o = table.concat({
87+
_o,
88+
utils.create_segmant(config.separator_text, config.separator_hl)
89+
});
90+
end
91+
elseif tab_index ~= 1 then
92+
_o = table.concat({
93+
_o,
94+
utils.create_segmant(config.separator_text, config.separator_hl)
95+
});
96+
end
97+
1698
local current = tab == vim.api.nvim_get_current_tabpage();
1799
local tab_config = current == true and (config.active or {}) or (config.inactive or {});
18100

19101
_o = table.concat({
20102
_o,
21103

22-
"%" .. t .. "T",
104+
current == false and "%" .. tab_index .. "T" or "",
23105

24-
utils.set_hl(tab_config.corner_left_hl or tab_config.hl),
25-
tab_config.corner_left or "",
106+
utils.create_segmant(tab_config.corner_left, tab_config.corner_left_hl or tab_config.hl),
107+
utils.create_segmant(tab_config.padding_left, tab_config.padding_left_hl or tab_config.hl),
26108

27-
utils.set_hl(tab_config.padding_left_hl or tab_config.hl),
28-
tab_config.padding_left or "",
109+
utils.create_segmant(tab_config.icon, tab_config.icon_hl),
110+
});
111+
112+
if type(tab_config.win_count) == "string" then
113+
local wins = vim.api.nvim_tabpage_list_wins(tab);
114+
115+
_o = table.concat({
116+
_o,
117+
118+
utils.create_segmant(tab, tab_config.hl),
119+
utils.create_segmant(
120+
string.format(tab_config.win_count, #wins),
121+
tab_config.win_count_hl
122+
),
123+
});
124+
else
125+
_o = table.concat({
126+
_o,
127+
utils.create_segmant(tab, tab_config.hl)
128+
});
129+
end
29130

30-
utils.set_hl(tab_config.icon_hl or tab_config.hl),
31-
tab_config.icon or "",
131+
_o = table.concat({
132+
_o,
32133

33-
utils.set_hl(tab_config.hl),
34-
tab,
134+
utils.create_segmant(tab_config.padding_right, tab_config.padding_right_hl or tab_config.hl),
135+
utils.create_segmant(tab_config.corner_right, tab_config.corner_right_hl or tab_config.hl),
35136

36-
utils.set_hl(tab_config.padding_right_hl or tab_config.hl),
37-
tab_config.padding_right or "",
137+
current == false and "%X" or ""
138+
});
139+
end
38140

39-
utils.set_hl(tab_config.corner_right_hl or tab_config.hl),
40-
tab_config.corner_right or "",
141+
if max < #tabs then
142+
_o = table.concat({
143+
_o,
41144

145+
"%@v:lua.__tab_from_increase@",
146+
utils.create_segmant(config.nav_right_text, config.nav_right_hl),
42147
"%X"
43148
});
44149
end
45150

151+
46152
return _o;
47153
end
48154

0 commit comments

Comments
 (0)