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

Custom highlight groups #69

Open
husa opened this issue Jan 7, 2025 · 2 comments
Open

Custom highlight groups #69

husa opened this issue Jan 7, 2025 · 2 comments

Comments

@husa
Copy link

husa commented Jan 7, 2025

Would be great to have custom highlight groups for faster and easier identification of target.
Something like this:
Screenshot 2025-01-07 at 17 48 40

I've made a small proof of concept. If you're interested I can share how I approached it.

@leath-dub
Copy link
Owner

Would be great to have custom highlight groups for faster and easier identification of target. Something like this: Screenshot 2025-01-07 at 17 48 40

I've made a small proof of concept. If you're interested I can share how I approached it.

Please do share, I am very happy to review code and help people. I am very busy with final year so my own development on the project is very limited lately.

@husa
Copy link
Author

husa commented Jan 10, 2025

The idea is to define highlight groups(boundaries) together with actual line formatting.

---@class BufferList
---@field items {
--- id: string,
--- name: string,
--- is_dir: boolean,
--- basename: string,
--- dirname: string,
--- anchor: string,
--- formatted_name: string, -- pad basename
--- icon: {
---   symbol: string,
---   hlgroup: string,
--- },
---}[]
local BufferList = {
  items = {},
}

-- other methods
-- ....

function BufferList:format_lines()
  for _, buffer in pairs(self.items) do
    ---@type {text: string, hlgroup: string}[]
    local format = {
      { text = buffer.anchor, hlgroup = highlight_groups.anchor.name },
      { text = buffer.icon.symbol, hlgroup = buffer.icon.hlgroup },
      { text = buffer.formatted_name, hlgroup = highlight_groups.filename.name },
      { text = buffer.dirname, hlgroup = highlight_groups.dirname.name },
    }
    local delimeter = " "
    local line_text = pluck("text", format)
    local line = table.concat(line_text, delimeter)

    ---@type {first: integer, last: integer, hlgroup: string}[]
    local highlights = {}
    for i = 1, #format do
      local first = 0
      if i >= 2 then
        first = highlights[i - 1].last + #delimeter
      end
      highlights[i] = {
        first = first,
        last = first + #format[i].text,
        hlgroup = format[i].hlgroup,
      }
    end
    buffer.line = line
    buffer.highlights = highlights
  end
end

This is just a quick and dirty approach, to make things work, but hope you get the idea.
Potentially format can be defined as a list of strings, for some customizabilty, ie. {'tag', 'icon', 'filename', 'dirname'}. Or even, including custom parts that the plugin consumer will have to implement themself, such as custom icon for directories or path shortening, etc.

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

No branches or pull requests

2 participants