nanozuki / tabby.nvim

A declarative, highly configurable, and neovim style tabline plugin. Use your nvim tabs as a workspace multiplexer!
MIT License
563 stars 21 forks source link

How to handle tab.name() for certain plugins (Overseer / Aerial etc) #129

Closed ls-devs closed 5 months ago

ls-devs commented 6 months ago

Hi, I was wondering if there's a way we can set the tab name to vim.bo.filetype instead of using [No Name] on certain window like Overseer or Aerial for example ?

Please see actually tab name when openning Overseer : image

Actually tabby config :

    require("tabby.tabline").set(function(line)
      return {
        {
          { "  ", hl = { fg = colors.green, bg = colors.none, style = "bold" } },
          line.sep(" ", { bg = colors.none }, { bg = colors.none }),
        },
        line.tabs().foreach(function(tab)
          local hl = tab.is_current() and "TabLineSel" or "TabLine"
          return {
            line.sep("", hl, { bg = colors.none }),
            tab.is_current() and "" or "󰆣",
            tab.name(),
            tab.close_btn(""),
            line.sep("", hl, { bg = colors.none }),
            hl = hl,
            margin = " ",
          }
        end),
        line.spacer(),
        line.wins_in_tab(line.api.get_current_tab()).foreach(function(win)
          return {
            line.sep("", "TabLine", { bg = colors.none }),
            win.is_current() and "  " or "  ",
            line.sep("", "TabLine", { bg = colors.none }),
            hl = { bg = colors.bg_dark, fg = colors.fg },
            margin = " ",
          }
        end),
        hl = { bg = colors.none },
      }
    end)
ls-devs commented 6 months ago

Actually, I ended up doing something like this :

EDIT : Edited code after some tweaks, ended up with something comfortable for me; but still gave me a headache. Is there any easier solution for this ?

    local getTabName = function(tab)
      local tabTail = vim.fn.fnamemodify(tab.name(), "%:t")
      local tabName = ""
      if string.len(tabTail) > 25 then
        local fileExt = string.match(tabTail, "[^.]+$")
        tabName = string.sub(tabTail, 0, 22 - string.len(fileExt)) .. "..." .. fileExt
      end
      if string.find(tabTail, "Floating") then
        if vim.bo.filetype == "" then
          local tail = vim.fn.expand("%:t")
          if string.find(tail, "toggleterm") then
            local pID = string.gsub(tail, "%d+:", "")
            tabName = string.gsub(pID, ";#toggleterm#%d+", "")
          end
          if string.find(tail, "npm") then
            tabName = string.gsub(tail, "%d+:", "")
          end
        else
          tabName = vim.bo.filetype
        end
      end
      if string.find(tab.name(), "%[%No Name]%[%d+%+%]") and string.find(vim.bo.filetype, "Overseer") then
        tabName = vim.bo.filetype
      end
      if tabName == "" then
        tabName = tab.name()
      end

      return tabName
    end

But I feel like it's an endless fight. Is there any solution I can achieve this or not really ?

nanozuki commented 5 months ago

For now, the better way to set it is to add the option line option. You can find windows from tabid. and get the file type from the window ID.

{
  tab_name = {
    name_fallback = function()
      local winid = vim.api.nvim_get_current_tabpage()
      local bufid = vim.api.nvim_win_get_buf(winid)
      local file_type = vim.api.nvim_buf_get_option(bufid, "filetype")
      if file_type == "Overseer" or file_type == "Aerial" then
        return file_type
      end
      return "[NO Name]"
    end,
  },
}
nanozuki commented 5 months ago

Welcome to more feedback and discussion.

I plan to refactor the documentation to add more instructions and some cookbooks. That will make this more easy to use.

ls-devs commented 5 months ago

@nanozuki Thanks for your reply. Unfortunately, the solution you provide leads me to a bug :

E5108: Error executing lua /home/ls-devs/.config/nvim/lua/ls-devs/plugins/ui/tabby.lua:7: Invalid window id: 1  
nanozuki commented 5 months ago

I'm sorry I use a wrong api, the code should be:

{
  tab_name = {
    name_fallback = function(tabid)
      local winid = vim.api.nvim_tabpage_get_win(tabid)
      local bufid = vim.api.nvim_win_get_buf(winid)
      local file_type = vim.api.nvim_buf_get_option(bufid, "filetype")
      if file_type == "Overseer" or file_type == "Aerial" then
        return file_type
      end
      return "[No Name]"
    end,
  },
}
ls-devs commented 5 months ago

I'm sorry I use a wrong api, the code should be:

{
  tab_name = {
    name_fallback = function(tabid)
      local winid = vim.api.nvim_tabpage_get_win(tabid)
      local bufid = vim.api.nvim_win_get_buf(winid)
      local file_type = vim.api.nvim_buf_get_option(bufid, "filetype")
      if file_type == "Overseer" or file_type == "Aerial" then
        return file_type
      end
      return "[No Name]"
    end,
  },
}

Can confirm it's working. Thanks for the help !