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

Adding custom labels for tabs #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ Add this to your init.lua:
require('luatab').setup{}
```

Rename the current tab using:
```
:LuatabLabelRename
```

## Configuration

The plugin calls the `helpers.tabline` function to render the line. It uses the other functions defined in `helpers`, such as `cell,separator,devicon`.
Expand Down
46 changes: 42 additions & 4 deletions lua/luatab/init.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
local M = {}

M.title = function(bufnr)
M.tab_labels = {'default'}

M.title = function(bufnr, current_label)
local file = vim.fn.bufname(bufnr)
local buftype = vim.fn.getbufvar(bufnr, '&buftype')
local filetype = vim.fn.getbufvar(bufnr, '&filetype')

if current_label ~= "default" then
return current_label
end

if buftype == 'help' then
return 'help:' .. vim.fn.fnamemodify(file, ':t:r')
elseif buftype == 'quickfix' then
Expand Down Expand Up @@ -40,12 +46,17 @@ M.windowCount = function(index)
return nwins > 1 and '(' .. nwins .. ') ' or ''
end

M.devicon = function(bufnr, isSelected)
M.devicon = function(bufnr, isSelected, current_label)
local icon, devhl
local file = vim.fn.bufname(bufnr)
local buftype = vim.fn.getbufvar(bufnr, '&buftype')
local filetype = vim.fn.getbufvar(bufnr, '&filetype')
local devicons = require'nvim-web-devicons'

if current_label ~= 'default' then
return "🌝"
end

if filetype == 'TelescopePrompt' then
icon, devhl = devicons.get_icon('telescope')
elseif filetype == 'fugitive' then
Expand Down Expand Up @@ -78,13 +89,14 @@ M.cell = function(index)
local buflist = vim.fn.tabpagebuflist(index)
local winnr = vim.fn.tabpagewinnr(index)
local bufnr = buflist[winnr]
local current_label = M.tab_labels[index]
local hl = (isSelected and '%#TabLineSel#' or '%#TabLine#')

return hl .. '%' .. index .. 'T' .. ' ' ..
M.windowCount(index) ..
M.title(bufnr) .. ' ' ..
M.title(bufnr, current_label) .. ' ' ..
M.modified(bufnr) ..
M.devicon(bufnr, isSelected) .. '%T' ..
M.devicon(bufnr, isSelected, current_label) .. '%T' ..
M.separator(index)
end

Expand All @@ -100,6 +112,12 @@ M.tabline = function()
return line
end


M.rename_tab = function()
local new_label = vim.fn.input('New tab name: ')
M.tab_labels[vim.fn.tabpagenr()] = new_label
end

local setup = function(opts)
opts = opts or {}
if opts.title then M.title = opts.title end
Expand All @@ -123,6 +141,26 @@ please replace it with
]]
end

-- When you enter a new tab
vim.api.nvim_create_autocmd('TabNew', {
callback = function()
local tabs_labels = require('luatab').helpers.tab_labels
table.insert(tabs_labels, 'default')
end
})

-- When you leave a tab
vim.api.nvim_create_autocmd('TabClosed', {
callback = function()
local tabs_labels = require('luatab').helpers.tab_labels
local removed_index = vim.fn.expand('<afile>')
table.remove(tabs_labels, removed_index)
end
})

vim.api.nvim_create_user_command('LuatabLabelRename', 'lua require(\'luatab\').helpers.rename_tab()', {})

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
vim.api.nvim_create_user_command('LuatabLabelRename', 'lua require(\'luatab\').helpers.rename_tab()', {})
vim.api.nvim_create_user_command('LuatabLabelRename', require('luatab').helpers.rename_tab, {})



return {
helpers = M,
setup = setup,
Expand Down