MunifTanjim / nougat.nvim

🍫 Hyperextensible Statusline / Tabline / Winbar for Neovim 🚀
MIT License
197 stars 3 forks source link

Support vim-gitbranch/vim-gitgutter as git branch/status source #80

Closed linrongbin16 closed 8 months ago

linrongbin16 commented 8 months ago

Did you check the docs and existing issues?

Problem

I'm using vim-gitbranch as git branch data source, and vim-gitgutter as git diff/status data source.

I know most people are using gitsigns, but it has some small bugs on Windows, gitgutter has much better cross-platform compatibilities.

For now I'm configure it with:

local function git_branch()
    if vim.g.loaded_gitbranch == nil or vim.g.loaded_gitbranch <= 0 then
        return ""
    end
    local name = vim.fn["gitbranch#name"]()
    return name or ""
end

local function git_diff()
    if vim.g.loaded_gitgutter == nil or vim.g.loaded_gitgutter <= 0 then
        return ""
    end
    local changes = vim.fn["GitGutterGetHunkSummary"]()
    if changes == nil or #changes ~= 3 then
        return ""
    end
    local symbols = { "+", "~", "-" }
    local builder = {}
    for i, c in ipairs(changes) do
        if type(c) == "number" and c > 0 then
            table.insert(builder, string.format("%s%d", symbols[i], c))
        end
    end
    return table.concat(builder, " ")
end

-- git branch and status
stl:add_item(Item({
    hl = { bg = color.magenta, fg = color.bg },
    sep_right = sep.right_upper_triangle_solid(true),
    prefix = "  ",
    suffix = " ",
    type = "lua_expr",
    content = function(ctx)
        local branch = git_branch()
        if string.len(branch) == 0 then
            return ""
        end
        local changes = git_diff()
        if string.len(changes) == 0 then
            return branch
        else
            return branch .. " " .. changes
        end
    end,
}))

It is working, looks like:

image

But I want to enable the green, yellow, red colors for the added, changed and removed lines count number.

How should I do it?

I am using the slanty.lua template, the git status sample is:

-- -- git changes
stl:add_item(nut.git.status.create({
    hl = { fg = color.bg },
    content = {
        nut.git.status.count("added", {
            hl = { bg = color.green },
            prefix = "+",
            sep_right = sep.right_upper_triangle_solid(true),
        }),
        nut.git.status.count("changed", {
            hl = { bg = color.yellow },
            prefix = "~",
            sep_right = sep.right_upper_triangle_solid(true),
        }),
        nut.git.status.count("removed", {
            hl = { bg = color.red },
            prefix = "-",
            sep_right = sep.right_upper_triangle_solid(true),
        }),
    },
}))

It seems the content in nut.git.status.count is not overwrite-able, correct?

Solution

Use self-defined data source for git status.

Alternatives

No response

Additional Context

No response

MunifTanjim commented 8 months ago

You can check the implementation of those components here: https://github.com/MunifTanjim/nougat.nvim/tree/main/lua/nougat/nut/git

For adding new provider for git branch, we'll need to update this: https://github.com/MunifTanjim/nougat.nvim/blob/700708b73c037e7800cbb27209247fdd1b6a122f/lua/nougat/nut/git/branch.lua#L12-L19

For adding new provider for git status, we'll need to update here: https://github.com/MunifTanjim/nougat.nvim/blob/700708b73c037e7800cbb27209247fdd1b6a122f/lua/nougat/cache/buffer.lua#L125-L160

linrongbin16 commented 8 months ago

I think I can have a try to make a PR for this

linrongbin16 commented 8 months ago

hi @MunifTanjim , I found in cache/buffer.lua:

https://github.com/linrongbin16/nougat.nvim/blob/700708b73c037e7800cbb27209247fdd1b6a122f/lua/nougat/cache/buffer.lua?plain=1#L135

It's using on_event to listen gitsigns git changes, but vim-gitgutter seems doesn't emit any events when the git changes happen.

      on_event("User GitSignsUpdate", function(params)

Is there any other way to trigger cache refresh? maybe a timer scheduled per second?

MunifTanjim commented 8 months ago

You can probably use this event: https://github.com/airblade/vim-gitgutter/blob/4b49965897b8264cd6f90fa47ddb917f4296c469/doc/gitgutter.txt#L244C1-L252 ?

So it'll be like:


on_event("User GitGutter", function(params)
  local bufnr = vim.g.gitgutter_hook_context.bufnr
  local data = vim.fn.GitGutterGetHunkSummary()
  local added, changed, removed = data[1], data[2], data[3]
end)
linrongbin16 commented 8 months ago

The 'git-branch' is very easy to implement as an 'Item', I think it's cool to leave it to user to define by themselves.