This repository has been archived on 2024-03-28. You can view files and clone it, but cannot push or open issues or pull requests.
dotfiles/common/neovim/lua/statusline.lua

73 lines
1.9 KiB
Lua
Raw Normal View History

2021-06-13 11:02:27 -05:00
local fn = vim.fn
local api = vim.api
local status_line_max_length = 5
2021-06-11 21:32:54 -05:00
-- TODO: only update this portion when needed instead of every render?
2021-06-13 11:02:27 -05:00
local status_line_buffer_by_num = function(bufnum)
local bufinfo = fn.getbufinfo(bufnum)
2021-06-11 21:32:54 -05:00
local prefix = ' %#InactiveBuffer#'
local suffix = '%* '
if bufinfo.changed then
prefix = '%#DirtyBuffer# '
suffix = ' %*'
end
2021-06-13 11:02:27 -05:00
if bufinfo.hidden == 0 and fn.index(bufinfo.windows, vim.g.statusline_winid) >= 0 then
2021-06-11 21:32:54 -05:00
prefix = '%#ActiveBuffer# '
suffix = ' %*'
if bufinfo.changed then
prefix = '%#ActiveBuffer# *'
suffix = ' %*'
end
end
2021-06-13 11:02:27 -05:00
return prefix .. fn.fnamemodify(fn.bufname(bufnum), ':t') .. suffix
2021-06-11 21:32:54 -05:00
end
2021-06-13 11:02:27 -05:00
local status_line_buffers = function()
2021-06-11 21:32:54 -05:00
-- TODO: mark buffers with unsaved changes
local active_index = -1
local acc = {}
2021-06-13 11:02:27 -05:00
for _,bufnum in ipairs(api.nvim_list_bufs()) do
local bufinfo = fn.getbufinfo(bufnum)
2021-06-11 21:32:54 -05:00
if bufinfo.listed ~= 0 then
2021-06-13 11:02:27 -05:00
local entry = status_line_buffer_by_num(bufnum)
2021-06-11 21:32:54 -05:00
table.insert(acc, entry)
2021-06-13 11:02:27 -05:00
if fn.matchstr(entry, '^%#ActiveBuffer#') then
active_index = fn.index(acc, entry)
2021-06-11 21:32:54 -05:00
end
end
end
if active_index >= 0 then
-- TODO: instead implement this as a wraparound carousel?
2021-06-13 11:02:27 -05:00
local offset = status_line_max_length / 2
2021-06-11 21:32:54 -05:00
local min_buf_num = math.max(0, (active_index - offset))
2021-06-13 11:02:27 -05:00
local max_buf_num = math.min(#acc - 1, min_buf_num + status_line_max_length - 1)
min_buf_num = math.max(0, max_buf_num - status_line_max_length + 1)
local buflist = table.concat({unpack(acc, min_buf_num+1, max_buf_num+1)}, '')
2021-06-11 21:32:54 -05:00
local prefix = ''
local suffix = ''
if min_buf_num > 0 then
prefix = '< '
end
if max_buf_num < (#acc - 1) then
suffix = ' >'
end
2021-06-13 11:02:27 -05:00
return prefix .. buflist .. suffix
2021-06-11 21:32:54 -05:00
else
return table.concat(acc, '')
end
end
function StatusLine()
2021-06-13 11:02:27 -05:00
return status_line_buffers() .. '%*%=%c,%l/%L (%p%%)'
2021-06-11 21:32:54 -05:00
end
return {
setup=function()
2021-06-13 11:02:27 -05:00
vim.o.statusline = '%!v:lua.StatusLine()'
2021-06-11 21:32:54 -05:00
end
}