aboutsummaryrefslogtreecommitdiff
path: root/lua/cleantab/init.lua
blob: aa4a974fec1b1c1b7d68a74e9243971d49d2b4df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
local M = {}

M.fileTitle = function(file)
	file = vim.fn.fnamemodify(file, ':p:~')
	local tail = vim.fn.fnamemodify(file, ':t')

	if not tail then
		return vim.fn.fnamemodify(file, ':p:~:h') .. '/' -- e.g. ~/dir/
	elseif tail == 'init.lua'
	    or tail == '__init__.py' then
		return vim.fn.fnamemodify(vim.fn.fnamemodify(file, ':h'), ':t') .. '/' .. tail -- e.g. luatab/init.lua
	else
		return tail
	end
end

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

	if buftype == 'help' then
		return 'help:' .. vim.fn.fnamemodify(file, ':t:r')
	elseif buftype == 'quickfix' then
		return 'quickfix'
	elseif filetype == 'TelescopePrompt' then
		return 'Telescope'
	elseif filetype == 'git' then
		return 'Git'
	elseif filetype == 'fugitive' then
		return 'Fugitive'
	elseif filetype == 'NvimTree' then
		return 'NvimTree'
	elseif filetype == 'oil' then
		return 'Oil'
	elseif file:sub(file:len()-2, file:len()) == 'FZF' then
		return 'FZF'
	elseif buftype == 'terminal' then
		local _, mtch = string.match(file, "term:(.*):(%a+)")
		return mtch ~= nil and mtch or vim.fn.fnamemodify(vim.env.SHELL, ':t')
	elseif file == '' then
		return '[No Name]'
	else
		return M.fileTitle(vim.fn.simplify(file))
	end
end

M.modified = function(bufnr)
	return vim.fn.getbufvar(bufnr, '&modified') == 1 and '+' or ''
end

M.windowCount = function(index, hl)
	local nwins = vim.fn.tabpagewinnr(index, '$')
	return nwins > 1 and '%#TabLineSel#' .. nwins .. hl or ''
end

M.separator = function(index)
	return (index < vim.fn.tabpagenr('$') and '%#TabLine#|' or '')
end

M.cell = function(index)
	local isSelected = vim.fn.tabpagenr() == index
	local buflist = vim.fn.tabpagebuflist(index)
	local winnr = vim.fn.tabpagewinnr(index)
	local bufnr = buflist[winnr]
	local hl = (isSelected and '%#TabLineSel#' or '%#TabLine#')

	local prefix = M.windowCount(index, hl) .. M.modified(bufnr) .. ' '
	return hl .. '%' .. index .. 'T' .. ' ' ..
		(prefix == ' ' and '' or prefix) ..
		M.title(bufnr) .. ' ' ..
		M.separator(index)
end

M.tabline = function()
	local line = ''
	for i = 1, vim.fn.tabpagenr('$'), 1 do
		line = line .. M.cell(i)
	end
	line = line .. '%#TabLineFill#%='
	if vim.fn.tabpagenr('$') > 1 then
		line = line .. '%#TabLine#%999XX'
	end
	return line
end

local setup = function(opts)
	if opts then
		for key, value in pairs(opts) do
			M[key] = value
		end
	end

	vim.opt.tabline = '%!v:lua.require\'luatab\'.helpers.tabline()'
end

return {
	helpers = M,
	setup = setup,
}