diff --git a/README.md b/README.md index 238b283b8..9ef5d581d 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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 diff --git a/lua/lualine/components/branch/git_branch.lua b/lua/lualine/components/branch/git_branch.lua index 9474f3232..e2c15557d 100644 --- a/lua/lualine/components/branch/git_branch.lua +++ b/lua/lualine/components/branch/git_branch.lua @@ -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 @@ -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 @@ -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 diff --git a/lua/lualine/components/branch/init.lua b/lua/lualine/components/branch/init.lua index 7e75544d5..66687982b 100644 --- a/lua/lualine/components/branch/init.lua +++ b/lua/lualine/components/branch/init.lua @@ -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) diff --git a/tests/spec/component_spec.lua b/tests/spec/component_spec.lua index 3311b95b1..6f2cd6b73 100644 --- a/tests/spec/component_spec.lua +++ b/tests/spec/component_spec.lua @@ -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)