Skip to content

Commit

Permalink
Git branch component: Add option to limit the length
Browse files Browse the repository at this point in the history
Added a new function that truncates git branch name if the max_length
option is set. It also attempts to strip the last incomplete word after
the last word separator (i.e. "-", "_")
  • Loading branch information
Nandax00 committed Aug 27, 2024
1 parent b431d22 commit 9dde31e
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 16 deletions.
43 changes: 29 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,21 @@ These are options that are available on specific components.
For example, you have option on `diagnostics` component to
specify what your diagnostic sources will be.

#### branch component options

```lua
sections = {
lualine_a = {
{
'branch',
max_length = 0, -- Maximum displayed characters of the branch name.
-- Takes effect, if the value is larger than 0.
-- Recommended setting: 30.
}
}
}
```

#### buffers component options

```lua
Expand Down Expand Up @@ -608,6 +623,20 @@ sections = {
}
```

#### encoding component options

```lua
sections = {
lualine_a = {
{
'encoding',
-- Show '[BOM]' when the file has a byte-order mark
show_bomb = false,
}
}
}
```

#### fileformat component options

```lua
Expand Down Expand Up @@ -670,20 +699,6 @@ sections = {
}
```

#### encoding component options

```lua
sections = {
lualine_a = {
{
'encoding',
-- Show '[BOM]' when the file has a byte-order mark
show_bomb = false,
}
}
}
```

#### searchcount component options

```lua
Expand Down
25 changes: 24 additions & 1 deletion lua/lualine/components/branch/git_branch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ local sep = package.config:sub(1, 1)
-- Windows doesn't like file watch for some reason.
local file_changed = sep ~= '\\' and vim.loop.new_fs_event() or vim.loop.new_fs_poll()
local git_dir_cache = {} -- Stores git paths that we already know of
local options = {max_length = 0}

---truncates git branch name based on options.max_length.
---also attempts to strip the last incomplete word after the last word separator (i.e. "-", "_")
local function truncate_git_branch()
current_git_branch = current_git_branch:sub(1, options.max_length)
if not current_git_branch:find("[-_]") then
current_git_branch = current_git_branch .. ''
return
end

-- make the truncated output nicer by cutting off the last incomplete word
local last_word_separator_idx = current_git_branch:len() - current_git_branch:reverse():find("[-_]")
current_git_branch = current_git_branch:sub(1, last_word_separator_idx) .. ''
end

---sets git_branch variable to branch name or commit hash if not on branch
---@param head_file string full path of .git/HEAD file
Expand All @@ -26,6 +41,9 @@ local function get_git_head(head_file)
local branch = HEAD:match('ref: refs/heads/(.+)$')
if branch then
current_git_branch = branch
if options.max_length ~= 0 and current_git_branch:len() > options.max_length then
truncate_git_branch()
end
else
current_git_branch = HEAD:sub(1, 6)
end
Expand Down Expand Up @@ -135,12 +153,17 @@ function M.find_git_dir(dir_path)
end

---initializes git_branch module
function M.init()
function M.init(opts)
if opts.max_length then
options.max_length = opts.max_length
end

-- run watch head on load so branch is present when component is loaded
M.find_git_dir()
-- update branch state of BufEnter as different Buffer may be on different repos
utils.define_autocmd('BufEnter', "lua require'lualine.components.branch.git_branch'.find_git_dir()")
end

function M.get_branch(bufnr)
if vim.g.actual_curbuf ~= nil and active_bufnr ~= vim.g.actual_curbuf then
-- Workaround for https://github.com/nvim-lualine/lualine.nvim/issues/286
Expand Down
2 changes: 1 addition & 1 deletion lua/lualine/components/branch/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ M.init = function(self, options)
if not self.options.icon then
self.options.icon = '' -- e0a0
end
modules.git_branch.init()
modules.git_branch.init(options)
end

M.update_status = function(_, is_focused)
Expand Down
27 changes: 27 additions & 0 deletions tests/spec/component_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -871,4 +871,31 @@ describe('Branch component', function()
local rev = git('rev-parse --short=6 HEAD'):sub(1, 6)
assert_component('branch', opts, rev)
end)

it('works with max_length option', function()
local opts = build_component_opts {
component_separators = { left = '', right = '' },
icons_enabled = false,
padding = 0,
max_length = 10,
}
local branch_comp = helpers.init_component('branch', opts)

local test = {
noseparator = "noseparato…",
separated_name = "separated…",
separatorafter_threshold = "separatora…",
short_name = "short_name",
}
-- hack to add a hyphenated key
test["other-sep-character"] = "other-sep…"

for input, expected in pairs(test) do
git('checkout -b ' .. input)
vim.cmd('e k')
vim.cmd('bd')
vim.cmd('e ' .. file)
assert_comp_ins(branch_comp, expected)
end
end)
end)

0 comments on commit 9dde31e

Please sign in to comment.