feat: show index in buffers component instead of bufnr (#628)
Co-authored-by: shadmansaleh <13149513+shadmansaleh@users.noreply.github.com>
This commit is contained in:
parent
00dc7929fe
commit
c2165540a8
14
README.md
14
README.md
|
@ -438,8 +438,8 @@ sections = {
|
|||
show_modified_status = true, -- Shows indicator when the buffer is modified.
|
||||
|
||||
mode = 0, -- 0: Shows buffer name
|
||||
-- 1: Shows buffer index (bufnr)
|
||||
-- 2: Shows buffer name + buffer index (bufnr)
|
||||
-- 1: Shows buffer index
|
||||
-- 2: Shows buffer name + buffer index
|
||||
|
||||
max_length = vim.o.columns * 2 / 3, -- Maximum width of buffers component,
|
||||
-- it can also be a function that returns
|
||||
|
@ -610,8 +610,8 @@ sections = {
|
|||
show_modified_status = true, -- Shows indicator when the window is modified.
|
||||
|
||||
mode = 0, -- 0: Shows window name
|
||||
-- 1: Shows window index (bufnr)
|
||||
-- 2: Shows window name + window index (bufnr)
|
||||
-- 1: Shows window index
|
||||
-- 2: Shows window name + window index
|
||||
|
||||
max_length = vim.o.columns * 2 / 3, -- Maximum width of windows component,
|
||||
-- it can also be a function that returns
|
||||
|
@ -673,6 +673,12 @@ tabline = {
|
|||
Shows currently open buffers. Like bufferline . See
|
||||
[buffers options](#buffers-component-options)
|
||||
for all builtin behaviors of buffers component.
|
||||
You can use `:LualineBuffersJump` to jump to buffer based on index
|
||||
of buffer in buffers component.
|
||||
```vim
|
||||
:LualineBuffersJump 2 " Jumps to 2nd buffer in buffers component.
|
||||
:LualineBuffersJump $ " Jumps to last buffer in buffers component.
|
||||
```
|
||||
|
||||
#### Tabs
|
||||
Shows currently open tab. Like usual tabline. See
|
||||
|
|
|
@ -455,8 +455,8 @@ Component specific options These are options that are available on
|
|||
show_modified_status = true, -- Shows indicator when the buffer is modified.
|
||||
|
||||
mode = 0, -- 0: Shows buffer name
|
||||
-- 1: Shows buffer index (bufnr)
|
||||
-- 2: Shows buffer name + buffer index (bufnr)
|
||||
-- 1: Shows buffer index
|
||||
-- 2: Shows buffer name + buffer index
|
||||
|
||||
max_length = vim.o.columns * 2 / 3, -- Maximum width of buffers component,
|
||||
-- it can also be a function that returns
|
||||
|
@ -634,8 +634,8 @@ Component specific options These are options that are available on
|
|||
show_modified_status = true, -- Shows indicator when the window is modified.
|
||||
|
||||
mode = 0, -- 0: Shows window name
|
||||
-- 1: Shows window index (bufnr)
|
||||
-- 2: Shows window name + window index (bufnr)
|
||||
-- 1: Shows window index
|
||||
-- 2: Shows window name + window index
|
||||
|
||||
max_length = vim.o.columns * 2 / 3, -- Maximum width of windows component,
|
||||
-- it can also be a function that returns
|
||||
|
@ -704,6 +704,15 @@ Buffers Shows currently open buffers. Like
|
|||
bufferline. See
|
||||
|lualine-buffers-options| for all
|
||||
builtin behaviors of buffers component.
|
||||
You can use `:LualineBuffersJump` to
|
||||
jump to buffer based on index of buffer
|
||||
in buffers component.
|
||||
|
||||
|
||||
>
|
||||
:LualineBuffersJump 2 " Jumps to 2nd buffer in buffers component.
|
||||
:LualineBuffersJump $ " Jumps to last buffer in buffers component.
|
||||
<
|
||||
|
||||
|
||||
*lualine-Tabs*
|
||||
|
|
|
@ -10,6 +10,7 @@ local modules = require('lualine_require').lazy_require {
|
|||
function Buffer:init(opts)
|
||||
assert(opts.bufnr, 'Cannot create Buffer without bufnr')
|
||||
self.bufnr = opts.bufnr
|
||||
self.buf_index = opts.buf_index
|
||||
self.options = opts.options
|
||||
self.highlights = opts.highlights
|
||||
self:get_props()
|
||||
|
@ -162,10 +163,10 @@ function Buffer:apply_mode(name)
|
|||
end
|
||||
|
||||
if self.options.mode == 1 then
|
||||
return string.format('%s %s%s', self.bufnr, self.icon, self.modified_icon)
|
||||
return string.format('%s %s%s', self.buf_index or '', self.icon, self.modified_icon)
|
||||
end
|
||||
|
||||
return string.format('%s %s%s%s', self.bufnr, self.icon, name, self.modified_icon)
|
||||
return string.format('%s %s%s%s', self.buf_index or '', self.icon, name, self.modified_icon)
|
||||
end
|
||||
|
||||
return Buffer
|
||||
|
|
|
@ -67,11 +67,12 @@ function M:init(options)
|
|||
end
|
||||
end
|
||||
|
||||
function M:new_buffer(bufnr)
|
||||
function M:new_buffer(bufnr, buf_index)
|
||||
bufnr = bufnr or vim.api.nvim_get_current_buf()
|
||||
|
||||
buf_index = buf_index or ''
|
||||
return Buffer:new {
|
||||
bufnr = bufnr,
|
||||
buf_index = buf_index,
|
||||
options = self.options,
|
||||
highlights = self.highlights,
|
||||
}
|
||||
|
@ -79,9 +80,11 @@ end
|
|||
|
||||
function M:buffers()
|
||||
local buffers = {}
|
||||
M.bufpos2nr = {}
|
||||
for b = 1, vim.fn.bufnr('$') do
|
||||
if vim.fn.buflisted(b) ~= 0 and vim.api.nvim_buf_get_option(b, 'buftype') ~= 'quickfix' then
|
||||
buffers[#buffers + 1] = self:new_buffer(b)
|
||||
buffers[#buffers + 1] = self:new_buffer(b, #buffers + 1)
|
||||
M.bufpos2nr[#buffers] = b
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -213,10 +216,24 @@ function M:draw()
|
|||
return self.status
|
||||
end
|
||||
|
||||
function M.buffer_jump(buf_pos)
|
||||
if buf_pos == '$' then
|
||||
buf_pos = #M.bufpos2nr
|
||||
else
|
||||
buf_pos = tonumber(buf_pos)
|
||||
end
|
||||
if buf_pos < 1 or buf_pos > #M.bufpos2nr then
|
||||
error('Error: Unable to jump buffer position out of range')
|
||||
end
|
||||
vim.api.nvim_set_current_buf(M.bufpos2nr[buf_pos])
|
||||
end
|
||||
|
||||
vim.cmd([[
|
||||
function! LualineSwitchBuffer(bufnr, mouseclicks, mousebutton, modifiers)
|
||||
execute ":buffer " . a:bufnr
|
||||
endfunction
|
||||
|
||||
command! -nargs=1 LualineBuffersJump call v:lua.require'lualine.components.buffers'.buffer_jump(<f-args>)
|
||||
]])
|
||||
|
||||
return M
|
||||
|
|
Loading…
Reference in New Issue