Skip to content

Commit

Permalink
WIP: Buffers rework, show indicator in table view
Browse files Browse the repository at this point in the history
  • Loading branch information
Conni2461 committed Oct 31, 2020
1 parent 37c4f1b commit c784cbe
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 31 deletions.
41 changes: 32 additions & 9 deletions lua/telescope/builtin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -581,17 +581,40 @@ builtin.fd = builtin.find_files
builtin.buffers = function(opts)
opts = opts or {}

local buffers = filter(function(b)
return
(opts.show_all_buffers
or vim.api.nvim_buf_is_loaded(b))
and 1 == vim.fn.buflisted(b)
local buffers_list = vim.split(vim.fn.execute('buffers'), "\n")

local buffers = {}
local last_used_buffernr
opts.bufnr_width = 0
for i = 2, #buffers_list do
-- Currently we don't get buffername over the buffers command, this is because :buffers shows
-- me the fullfilepath, like this '~/.config/nvim/plugged/lua/telescope/builtin.lua'.
-- This is not really useful for us.
-- TODO(conni2461): Thats wrong, what about readonly buffers
local bufnr, indicator, lnum = string.match(
buffers_list[i], '(%d+)%s*([%%# ]?[ah ]?[-=RF? ]?[+x ]?).*line (%d+)'
)

if indicator:sub(1, 1) == "#" then
last_used_buffernr = bufnr
end

if not opts.show_all_buffers and not vim.api.nvim_buf_is_loaded(tonumber(bufnr)) then
goto continue
end

end, vim.api.nvim_list_bufs())
table.insert(buffers, {
bufnr = bufnr,
indicator = indicator,
lnum = lnum,
})

-- Determine the max_width for bufnr
if opts.bufnr_width < #bufnr then
opts.bufnr_width = #bufnr
end

if not opts.bufnr_width then
local max_bufnr = math.max(unpack(buffers))
opts.bufnr_width = #tostring(max_bufnr)
::continue::
end

pickers.new(opts, {
Expand Down
34 changes: 12 additions & 22 deletions lua/telescope/make_entry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ do
end

local execute_keys = {
path = function(t)
path = function(t)
return t.cwd .. path.separator .. t.filename, false
end,

Expand Down Expand Up @@ -262,17 +262,6 @@ function make_entry.gen_from_buffer(opts)

local cwd = vim.fn.expand(opts.cwd or vim.fn.getcwd())

local get_position = function(entry)
local tabpage_wins = vim.api.nvim_tabpage_list_wins(0)
for k, v in ipairs(tabpage_wins) do
if entry == vim.api.nvim_win_get_buf(v) then
return vim.api.nvim_win_get_cursor(v)
end
end

return {}
end

local make_display = function(entry)
local display_bufname
if opts.shorten_path then
Expand All @@ -281,17 +270,17 @@ function make_entry.gen_from_buffer(opts)
display_bufname = entry.filename
end

return string.format("%" .. opts.bufnr_width .. "d : %s",
entry.bufnr, display_bufname)
return utils.table_format({
[1] = {entry.bufnr, opts.bufnr_width},
[2] = {entry.indicator, 4},
[3] = {display_bufname, 0},
[4] = {entry.lnum, 0},
}, { " ", " ", ":" })
end

return function(entry)
local bufnr_str = tostring(entry)
local bufname = path.normalize(vim.api.nvim_buf_get_name(entry), cwd)

-- if bufname is inside the cwd, trim that part of the string

local position = get_position(entry)
local bufname = path.normalize(vim.api.nvim_buf_get_name(entry.bufnr), cwd)

if '' == bufname then
return nil
Expand All @@ -301,13 +290,14 @@ function make_entry.gen_from_buffer(opts)
valid = true,

value = bufname,
ordinal = bufnr_str .. " : " .. bufname,
ordinal = entry.bufnr .. " : " .. bufname,
display = make_display,

bufnr = entry,
bufnr = entry.bufnr,
filename = bufname,

lnum = position[1] or 1,
lnum = entry.lnum or 1,
indicator = entry.indicator,
}
end
end
Expand Down
27 changes: 27 additions & 0 deletions lua/telescope/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,31 @@ function utils.max_split(s, pattern, maxsplit)
return t
end

-- index are used to determine the correct order
-- elements = {
-- [1] = { element, max width }, -- max width should be greater than 0
-- [2] = { a, 0 } -- Use 0 to disable max width
-- [3] = { b, 0 } -- If b is nil, skip this column, should skip column for all rows
-- },
-- separator = " " -- either arbitrary string, when you wanna use the same separator between all elements
-- separator = { " ", ":" } -- or table, where [1] is separator between elements[1] and elements[2], etc
function utils.table_format(elements, separator)
local output = ""
for k, v in ipairs(elements) do
local text = v[1]
local width = v[2]
if text ~= nil then
if k > 1 then
output = output .. (type(separator) == "table" and separator[k - 1] or separator)
end
if width == 0 then
output = output .. string.format("%s", text)
else
output = output .. string.format("%" .. width .."s", text)
end
end
end
return output
end

return utils

0 comments on commit c784cbe

Please sign in to comment.