summaryrefslogtreecommitdiffstats
path: root/.config/nvim/lua/lines.lua
blob: 3e0b539fd26719176cb2ac2a994fe35ee011fb4f (plain) (blame)
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
101
102
103
104
105
106
107
108
109
110
111
112
---@class Line
local line = {}

---@param buffer integer
---@return string
function line.get_formated_bufname(buffer)
	buffer = buffer or 0

	local name = vim.api.nvim_buf_get_name(buffer)
	local buftype = vim.bo[buffer].buftype
	if buftype == "terminal" then
		name = vim.b[buffer].term_title
	else
		if #name == 0 then
			name = '[No Name] (' .. (buftype or vim.bo[buffer].filetype) .. ')'
		end
		local modified = vim.bo[buffer].modified and '[+]' or ""
		local readonly = vim.bo[buffer].readonly and '[RO]' or ""
		name = name .. modified .. readonly
	end

	return name
end

---@return string
function line.status()
	local window = vim.g.statusline_winid
	local fillchar
	if window == vim.api.nvim_get_current_win() then
		fillchar = vim.opt.fillchars:get()["stl"]
	else
		fillchar = vim.opt.fillchars:get()["stlnc"]
	end

	if fillchar == nil then
		fillchar = " "
	end

	local buffer = vim.api.nvim_win_get_buf(window)
	local buftype = vim.bo[buffer].buftype


	if buftype == "terminal" then
		return table.concat({ vim.b[buffer].term_title, vim.api.nvim_buf_get_name(buffer) }, '%=')
	end

	if vim.bo[buffer].filetype == "netrw"
	then
		return "%f"
	end

	--	return '%=%-11.S%k%-14.(%l,%c%V%) %P'
	local ruler = ""
	if vim.o.ruler then
		ruler = vim.o.rulerformat
		if #ruler == 0 then
			ruler = "%-14.(%l:%c%V%)" .. fillchar .. "%P"
		end
	end

	local clients = {}
	for _, client in ipairs(vim.lsp.get_clients()) do
		if client.attached_buffers[buffer] then
			table.insert(clients, client.name)
		end
	end

	local cli = ""
	if #clients ~= 0 then
		cli = "(" .. table.concat(clients, ", ") .. ")"
	end

	return table.concat({
		'%<%f',
		'%h%w%m%r',
		"%=",
		vim.bo[buffer].filetype,
		cli,
		vim.bo[buffer].spelllang,
		vim.bo[buffer].fileencoding,
		vim.bo[buffer].fileformat,
		'%-12.k',
		'%S',
		ruler
	}, fillchar)
end

---@return string
function line.tab()
	local tabs = vim.api.nvim_list_tabpages()
	local format = vim.tbl_map(function(tab)
		local str = ""
		if tab == vim.api.nvim_get_current_tabpage() then
			str = '%#TabLineSel#'
		else
			str = '%#TabLine#'
		end
		local buffer = vim.api.nvim_win_get_buf(vim.api.nvim_tabpage_get_win(tab))
		return
		    table.concat {
			    str,
			    "%", tab, 'T',
			    line.get_formated_bufname(buffer),
			    " %", tab, 'XX'
		    }
	end, tabs)
	local tabline = table.concat(format, " ")

	return tabline .. '%#TabLineFill#%T'
end

return line