Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: dir_display_limit #89

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lua/barbecue/config/template.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ local M = {
---@type boolean
show_dirname = true,

---Limits the number of directories to display.
---Any number below 1 disables this feature.
---
---NOTE: Requires show_dirname to be enabled
---
---@type integer
dir_display_limit = 0,

---Whether to display file name.
---
---@type boolean
Expand Down
34 changes: 22 additions & 12 deletions lua/barbecue/ui/components.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,50 @@ function M.dirname(bufnr)
local dirname =
vim.fn.fnamemodify(filename, config.user.modifiers.dirname .. ":h")

if dirname == "." then return {} end

---@type barbecue.Entry[]
local entries = {}

if dirname == "." then return {} end
if dirname:sub(1, 1) == "/" then
local protocol_start_index = dirname:find("://")
if protocol_start_index ~= nil then
local protocol = dirname:sub(1, protocol_start_index + 2)
table.insert(
entries,
Entry.new({
"/",
protocol,
highlight = theme.highlights.dirname,
})
)

dirname = dirname:sub(protocol_start_index + 3)
end

local protocol_start_index = dirname:find("://")
if protocol_start_index ~= nil then
local protocol = dirname:sub(1, protocol_start_index + 2)
local dirs = vim.list_extend(
dirname:sub(1, 1) == "/" and { "/" } or {},
vim.split(dirname, PATH_SEPARATOR, { trimempty = true })
)

local dir_display_limit = config.user.dir_display_limit
local dirs_start_index = (
dir_display_limit >= 1 and #dirs - dir_display_limit or 0
) + 1

if dirs_start_index > 1 then
table.insert(
entries,
Entry.new({
protocol,
config.user.symbols.ellipsis,
highlight = theme.highlights.dirname,
})
)

dirname = dirname:sub(protocol_start_index + 3)
end

local dirs = vim.split(dirname, PATH_SEPARATOR, { trimempty = true })
for _, dir in ipairs(dirs) do
for i = math.max(dirs_start_index, 1), #dirs, 1 do
table.insert(
entries,
Entry.new({
dir,
dirs[i],
highlight = theme.highlights.dirname,
})
)
Expand Down