Skip to content

Commit 0bd5395

Browse files
committed
feat(tabline): Added current bufname support for tabline
1 parent 607a208 commit 0bd5395

File tree

4 files changed

+113
-5
lines changed

4 files changed

+113
-5
lines changed

lua/bars/components/tabline.lua

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ tlC.tabs = function (config)
8686
end
8787

8888
local wrapped = false;
89+
local rendered_paths = {};
8990

9091
for t = from, from + (max - 1), 1 do
9192
local tab_index = wrapped_index(#tabs, t);
@@ -137,6 +138,7 @@ tlC.tabs = function (config)
137138
utils.create_segmant(tab_config.padding_left, tab_config.padding_left_hl or tab_config.hl),
138139

139140
utils.create_segmant(tab_config.icon, tab_config.icon_hl),
141+
utils.create_segmant(tab, tab_config.hl)
140142
});
141143

142144
if type(tab_config.win_count) == "string" then
@@ -145,16 +147,42 @@ tlC.tabs = function (config)
145147
_o = table.concat({
146148
_o,
147149

148-
utils.create_segmant(tab, tab_config.hl),
150+
utils.create_segmant(tab_config.divider, tab_config.divider_hl),
149151
utils.create_segmant(
150152
string.format(tab_config.win_count, #wins),
151153
tab_config.win_count_hl
152154
),
153155
});
154-
else
156+
end
157+
158+
if type(tab_config.bufname) == "string" then
159+
---@type integer
160+
local winnr = vim.fn.tabpagewinnr(tab);
161+
local winid = vim.fn.win_getid(winnr);
162+
163+
local bufname = vim.api.nvim_buf_get_name(vim.api.nvim_win_get_buf(winid))
164+
local path;
165+
166+
if bufname == "" then
167+
path = "No name";
168+
else
169+
path = utils.truncate_path(
170+
vim.fn.fnamemodify(bufname, ":~"),
171+
{
172+
existing_paths = rendered_paths,
173+
}
174+
);
175+
table.insert(rendered_paths, path);
176+
end
177+
155178
_o = table.concat({
156179
_o,
157-
utils.create_segmant(tab, tab_config.hl)
180+
181+
utils.create_segmant(tab_config.divider, tab_config.divider_hl),
182+
utils.create_segmant(
183+
string.format(tab_config.bufname, path),
184+
tab_config.bufname_hl
185+
),
158186
});
159187
end
160188

lua/bars/tabline.lua

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
local tabline = {}
2-
local utils = require("bars.utils");
32
local components = require("bars.components.tabline");
43

54
---@class tabline.config
@@ -38,9 +37,13 @@ tabline.config = {
3837
padding_left = " ",
3938
padding_right = " ",
4039

41-
win_count = " ┃ 󰨝 %d",
40+
divider = "",
41+
42+
win_count = "󰨝 %d",
4243
win_count_hl = nil,
4344

45+
-- bufname = "󰳽 %s",
46+
4447
icon = "󰛺 ",
4548

4649
hl = "Color4R"
@@ -49,8 +52,12 @@ tabline.config = {
4952
padding_left = " ",
5053
padding_right = " ",
5154

55+
divider = " | ",
56+
5257
icon = "󰛻 ",
5358

59+
-- bufname = "󰳽 %s",
60+
5461
hl = "Color0B"
5562
}
5663
},

lua/bars/utils.lua

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,72 @@ utils.create_to_buf = function (buffer)
159159
end
160160
end
161161

162+
--- Component-style path truncation.
163+
---@param path string
164+
---@param opts table
165+
---@return string
166+
utils.truncate_path = function (path, opts)
167+
local default_opts = {
168+
existing_paths = {},
169+
raw_segmants = {},
170+
rewrite_segmants = {},
171+
172+
length = 1
173+
};
174+
175+
local function match (text)
176+
local keys = vim.tbl_keys(opts.rewrite_segmants);
177+
table.sort(keys);
178+
179+
for _, k in ipairs(keys) do
180+
if string.match(text, k) ~= nil then
181+
return opts.rewrite_segmants[k];
182+
end
183+
end
184+
end
185+
186+
path = path or "";
187+
188+
if type(opts) == "table" then
189+
opts = vim.tbl_deep_extend("force", default_opts, opts);
190+
else
191+
opts = default_opts;
192+
end
193+
194+
--- Break the path into parts.
195+
---@type string[]
196+
local path_parts = vim.split(path, "/", { trimempty = true });
197+
local _o;
198+
199+
while _o == nil or vim.tbl_contains(opts.existing_paths, _o) do
200+
---@type string
201+
local part = table.remove(path_parts);
202+
local rewrite = match(part);
203+
204+
if rewrite then
205+
local is_callable, rename = pcall(rewrite, part);
206+
207+
if is_callable == true and type(rename) == "string" then
208+
part = rename;
209+
elseif type(rewrite) == "string" then
210+
part = rewrite;
211+
end
212+
elseif _o ~= nil and vim.list_contains(opts.raw_segmants, part) == false then
213+
if string.match(part, "^%.") then
214+
part = vim.fn.strcharpart(part, 0, opts.length + 1);
215+
else
216+
part = vim.fn.strcharpart(part, 0, opts.length);
217+
end
218+
end
219+
220+
_o = table.concat({
221+
part,
222+
_o ~= nil and "/" or "",
223+
_o or ""
224+
})
225+
end
226+
227+
return _o;
228+
end
229+
162230
return utils;

lua/definitions/tabline.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@
9393
--- Highlight group for window count.
9494
---@field win_count_hl? string
9595
---
96+
--- Text to use between the bufname &
97+
--- window count.
98+
---@field divider? string
99+
---@field divider_hl? string
100+
---
96101
---@field corner_left? string
97102
---@field padding_left? string
98103
---@field icon? string

0 commit comments

Comments
 (0)