rebelot/heirline.nvim

HeirlineInitWinbar called before filetype resolves

Closed this issue · 1 comments

seblj commented

I can't properly disable the winbar on certain buffers, especially in startify which I use for my starting screen.

Minimal config:

local winbar = {
    fallback = false,
    {
        condition = function()
            return conditions.buffer_matches({
                buftype = { "nofile", "prompt", "help", "quickfix" },
                filetype = { "^git.*", "fugitive", "term", "startify" },
            })
        end,
        init = function()
            vim.opt_local.winbar = nil
        end,
    },
    { provider = "foobar" },
}

vim.api.nvim_create_autocmd("User", {
    pattern = "HeirlineInitWinbar",
    callback = function(args)
        local buf = args.buf
        local buftype = vim.tbl_contains({ "prompt", "nofile", "help", "quickfix" }, vim.bo[buf].buftype)
        local filetype = vim.tbl_contains({ "gitcommit", "fugitive", "startify" }, vim.bo[buf].filetype)
        if buftype or filetype then
            vim.opt_local.winbar = nil
        end
    end,
})

require("heirline").setup({
    winbar = winbar,
})

If I understand it correctly according to the cookbook, this should disable the winbar in the startify buffer, however it only works sometimes. I suspect this is because the autocmd that sets the winbar is called on BufWinEnter and VimEnter and that filetype is not resolved yet (sometimes). I confirmed this by trying to print out what the filetype was, and sometimes the filetype is empty in both the autocmd and in the first component in the winbar.

The winbar also disappears if I press j for example.

See video below that it sometimes work and sometimes not:

Screen.Recording.2023-02-20.at.18.24.22.mov

Awesome plugin btw!

seblj commented

A hack that seems to work is this:

local blocked_fts = {
    "term",
    "startify",
    "NvimTree",
    "packer",
    "startuptime",
}

vim.api.nvim_create_autocmd({ "BufWinEnter", "TabNew", "TabEnter", "BufEnter", "WinClosed", "BufWritePost" }, {
    group = vim.api.nvim_create_augroup("AttachWinbar", { clear = true }),
    desc = "Winbar only on some buffers",
    callback = function()
        vim.o.winbar = ""
        for _, w in pairs(vim.api.nvim_tabpage_list_wins(0)) do
            local buf, win = vim.bo[vim.api.nvim_win_get_buf(w)], vim.wo[w]
            if
                not vim.tbl_contains(blocked_fts, buf.filetype)
                and vim.fn.win_gettype(vim.api.nvim_win_get_number(w)) == ""
                and buf.buftype == ""
                and buf.filetype ~= ""
                and not win.diff
            then
                win.winbar = "%{%v:lua.require'heirline'.eval_winbar()%}"
            elseif win.diff then
                win.winbar = nil
            end
        end
    end,
})

-- Hack to make it work correctly to disable winbar in certain buffers
vim.api.nvim_create_autocmd("User", {
    pattern = "HeirlineInitWinbar",
    callback = function()
        vim.opt_local.winbar = nil
    end,
})

I have used the other autocmd with feline for a while and that has worked flawlessly for me, but it feels really hacky to subscribe to HeirlineInitWinbar and set winbar to nil