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

Git branch component: Add option to limit the length of the displayed branch name #1296

Closed

Conversation

Nandax00
Copy link

@Nandax00 Nandax00 commented Aug 24, 2024

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. "-", "_").

Demo in tabline with max_length = 30:

  • First one's straight-forward.
    branch name = thisisjustonereallylongbranchnamewithoutseparators =>
    image
  • Here, the 30-long substring would contain "inexplica" at the end, but it is stripped.
    branch name = this-one-contains-an-inexplicably-long-word =>
    image

I'm using the following tabline settings:

  tabline = {
    lualine_a = {
      {
        "tabs",
        -- options
        -- ...
      }
    },
    lualine_b = {
      {
        "branch",
        -- NEW OPTION
        max_length = 30,
      },
      "diff",
    },
  },

closes #1289.

@Nandax00 Nandax00 force-pushed the add_max_length_for_branch branch 4 times, most recently from b42f0e1 to 829065e Compare August 24, 2024 13:21
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. "-", "_")
@Nandax00 Nandax00 force-pushed the add_max_length_for_branch branch from 829065e to 9dde31e Compare August 27, 2024 11:01
@DavidePatria
Copy link

DavidePatria commented Sep 26, 2024

you can use fmt option and format a string as you wish

    lualine_b = { {
      'branch',
      fmt = function(str) return format(str, 10) end,
    } }

function format(str, len)
  if string.len(str) > len then
    return string.sub(str,1,len/2) .. '…' .. string.sub(str, str:len()-len/2+1, str:len())
  else
    return str
  end
end

@Nandax00
Copy link
Author

Thanks, I definitely missed that part in the docs :D Closing PR and ticket

@Nandax00 Nandax00 closed this Sep 26, 2024
@Nandax00 Nandax00 deleted the add_max_length_for_branch branch September 26, 2024 09:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feat: Configurable length for branch component
2 participants