Skip to content

Commit a0d03ec

Browse files
committed
feat(tabline): Added tabpage list locking
The tabpage list can now be *locked* to prevent new tabs from changing the position on the list.
1 parent 909ba6c commit a0d03ec

File tree

4 files changed

+98
-27
lines changed

4 files changed

+98
-27
lines changed

lua/bars/components/tabline.lua

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,37 @@ tlC.tabs = function (config)
4040
vim.g.__bars_tabpage_from = 1;
4141
end
4242

43+
if not vim.g.__bars_tabpage_list_locked then
44+
vim.g.__bars_tabpage_list_locked = false;
45+
end
46+
4347
---@type integer Start index. Must be above 0;
4448
local from = math.max(vim.g.__bars_tabpage_from, 1);
4549
---@type integer Number of tabs to show.
4650
local max = config.max or 5;
51+
---@type boolean Is the list position locked?
52+
local locked = vim.g.__bars_tabpage_list_locked;
53+
54+
--- Maximum number of tabs to show.
55+
--- Stored to he used by `autocmds`.
56+
vim.g.__tabline_max_tabs = max;
4757

4858
if from ~= 1 then
49-
_o = table.concat({
50-
_o,
59+
if locked == true then
60+
_o = table.concat({
61+
_o,
5162

52-
"%@v:lua.__tab_from_decrease@",
53-
utils.create_segmant(config.nav_left_text, config.nav_left_hl),
54-
"%X"
55-
});
63+
utils.create_segmant(config.nav_left_locked_text, config.nav_left_locked_hl),
64+
});
65+
else
66+
_o = table.concat({
67+
_o,
68+
69+
"%@v:lua.__tab_from_decrease@",
70+
utils.create_segmant(config.nav_left_text, config.nav_left_hl),
71+
"%X"
72+
});
73+
end
5674
end
5775

5876
local wrapped = false;
@@ -134,18 +152,29 @@ tlC.tabs = function (config)
134152
utils.create_segmant(tab_config.padding_right, tab_config.padding_right_hl or tab_config.hl),
135153
utils.create_segmant(tab_config.corner_right, tab_config.corner_right_hl or tab_config.hl),
136154

137-
current == false and "%X" or ""
155+
current == false and "%T" or ""
138156
});
139157
end
140158

141159
if max < #tabs then
142-
_o = table.concat({
143-
_o,
160+
if locked == true then
161+
_o = table.concat({
162+
_o,
144163

145-
"%@v:lua.__tab_from_increase@",
146-
utils.create_segmant(config.nav_right_text, config.nav_right_hl),
147-
"%X"
148-
});
164+
utils.create_segmant(config.separator_text, config.separator_hl),
165+
utils.create_segmant(config.nav_right_locked_text, config.nav_right_locked_hl),
166+
});
167+
else
168+
_o = table.concat({
169+
_o,
170+
171+
utils.create_segmant(config.separator_text, config.separator_hl),
172+
173+
"%@v:lua.__tab_from_increase@",
174+
utils.create_segmant(config.nav_right_text, config.nav_right_hl),
175+
"%X"
176+
});
177+
end
149178
end
150179

151180

lua/bars/tabline.lua

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,17 @@ tabline.config = {
1818
overflow_text = "",
1919
overflow_hl = "Layer1I",
2020

21-
nav_left_text = " < ",
22-
nav_left_hl = nil,
21+
nav_left_text = " ",
22+
nav_left_hl = "Color0",
2323

24-
nav_right_text = " > ",
25-
nav_right_hl = nil,
24+
nav_left_locked_text = " ",
25+
nav_left_locked_hl = "Color1",
26+
27+
nav_right_text = "",
28+
nav_right_hl = "Color0",
29+
30+
nav_right_locked_text = " 󰌾 ",
31+
nav_right_locked_hl = "Color1",
2632

2733
active = {
2834
padding_left = " ",

lua/bars/utils.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ utils.constant = function (val)
9696
__index = function ()
9797
return val;
9898
end,
99-
__newindex = function ()
100-
end,
99+
__newindex = function () end,
100+
101101
__metatable = false
102102
});
103103
end

plugin/bars.lua

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,52 @@ vim.api.nvim_create_autocmd({ "ModeChanged" }, {
107107
callback = function (event)
108108
---|fS
109109

110-
pcall(vim.api.nvim__redraw, {
111-
buf = event.buf,
112-
flush = true,
110+
--- We wrap this in `vim.schedule()` so
111+
--- that the update happens after doing
112+
--- something like,
113+
---
114+
--- ```vim
115+
--- :lua vim.g.__bars_tabpage_list_locked = false
116+
--- ```
117+
---
118+
--- We no longer have to redraw the screen
119+
--- twice!
120+
vim.schedule(function ()
121+
--- Unstable API function.
122+
--- Use `pcall()`
123+
pcall(vim.api.nvim__redraw, {
124+
buf = event.buf,
125+
flush = true,
126+
127+
statuscolumn = true,
128+
statusline = true,
129+
winbar = true,
130+
tabline = true
131+
});
132+
end);
133+
134+
---|fE
135+
end
136+
});
137+
138+
--- Update the tab list when opening new windows.
139+
vim.api.nvim_create_autocmd({ "TabNew" }, {
140+
callback = function ()
141+
---|fS
113142

114-
statuscolumn = true,
115-
statusline = true,
116-
winbar = true,
117-
tabline = true
118-
});
143+
local max = vim.g.__tabline_max_tabs or 5;
144+
local tabs = #vim.api.nvim_list_tabpages();
145+
146+
if not package.loaded["bars.tabline"] then
147+
return;
148+
elseif vim.g.__bars_tabpage_list_locked == true then
149+
--- List movement locked.
150+
return;
151+
elseif tabs <= max then
152+
return;
153+
end
119154

155+
vim.g.__bars_tabpage_from = math.max(1, tabs - math.floor(max * 0.25));
120156
---|fE
121157
end
122158
});

0 commit comments

Comments
 (0)