Skip to content

Commit

Permalink
add overflowtext
Browse files Browse the repository at this point in the history
  • Loading branch information
cht committed Feb 16, 2022
1 parent 41dec58 commit ed2a770
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 7 deletions.
1 change: 1 addition & 0 deletions ftdetect/csv-tools.vim
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
au BufRead,BufNewFile *.csv,*.dat,*.tsv,*.tab set filetype=csv
au FileType csv command! -buffer CloseTopWindow lua require"csvtools".CloseWindow()
au FileType csv command! -buffer TopWindow lua require"csvtools".NewWindow()
autocmd! InsertEnter *.csv,*.bat,*.tsv,*.tab lua require"csvtools".deleteMark()
"au FIiletype csv autocmd CursorMoved csv lua require"csvtools".Highlight()
34 changes: 27 additions & 7 deletions lua/csvtools.lua
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
local api = vim.api
local highlight = require("csvtools.highlight")
local overflow = require("csvtools.overflowtext")
local M = {
winid = nil,
buf = nil,
mainwindowbuf = nil,
header = {},
before = 20,
after = 20,
clearafter = true,
overflowtext = {
markid = nil,
ns_id = nil,
id = nil,
}
}

function M.printheader()
--for count = 1, #M.header do
-- print(M.header[count])
--end
return M.header
end
function M.NewWindow()
if M.winid == nil then
M.mainwindowbuf = vim.api.nvim_get_current_buf()
Expand All @@ -26,9 +38,7 @@ function M.NewWindow()
local win = vim.api.nvim_get_current_win()
api.nvim_buf_set_lines(buf, 0, -1, false, { messages })
api.nvim_win_set_buf(win, buf)
highlight.highlighttop(buf, messages)
--api.nvim_buf_add_highlight(buf, -1, 'WhidHeader',0,0,1)
--api.nvim_buf_add_highlight(buf, -1, 'WhidSubHeader', 0, 1, 2)
M.header = highlight.highlighttop(buf, messages)
M.winid = win
M.buf = buf
M.add_mappings()
Expand All @@ -39,7 +49,8 @@ function M.CloseWindow()
if M.winid ~= nil then
vim.api.nvim_win_close(M.winid, true)
M.winid = nil
M.buf = nil
M.buf = nil
M.header = {}
end
end

Expand All @@ -61,13 +72,15 @@ function M.Highlight()
if vim.o.filetype == "csv" then
M.mainwindowbuf = vim.api.nvim_get_current_buf()
local line, _ = unpack(vim.api.nvim_win_get_cursor(0))
--print(line)
local length = vim.api.nvim_buf_line_count(M.mainwindowbuf)
if M.clearafter then
api.nvim_buf_clear_highlight(M.mainwindowbuf, -1, 0, length)
end
local start, final = getrange(line, length)
--print(start)
--print(final)
M.overflowtext= overflow.OverFlow(line,M.header)
for i = start, line, 1 do
highlight.highlight(M.mainwindowbuf, i)
end
Expand All @@ -76,6 +89,13 @@ function M.Highlight()
end
end
end
function M.deleteMark()
vim.api.nvim_buf_del_extmark(
M.overflowtext.markid,
M.overflowtext.ns_id,
M.overflowtext.id
)
end
function M.add_mappings()
M.mainwindowbuf = vim.api.nvim_get_current_buf()
--print(M.mainwindowbuf)
Expand All @@ -84,8 +104,8 @@ function M.add_mappings()
vim.api.nvim_buf_set_keymap(M.mainwindowbuf, "n", "<leader>tf", ":lua require'csvtools'.NewWindow()<cr>", opts)
vim.api.nvim_buf_set_keymap(M.buf, "n", "<leader>td", ":lua require'csvtools'.CloseWindow()<cr>", opts)
vim.api.nvim_buf_set_keymap(M.mainwindowbuf, "n", "<leader>td", ":lua require'csvtools'.CloseWindow()<cr>", opts)
vim.api.nvim_buf_set_keymap(M.mainwindowbuf, "n", "<up>", ":lua require'csvtools'.Highlight()<cr>:-1<cr>", opts)
vim.api.nvim_buf_set_keymap(M.mainwindowbuf, "n", "<down>", ":lua require'csvtools'.Highlight()<cr>:+1<cr>", opts)
vim.api.nvim_buf_set_keymap(M.mainwindowbuf, "n", "<up>", ":-1<cr>:lua require'csvtools'.Highlight()<cr>", opts)
vim.api.nvim_buf_set_keymap(M.mainwindowbuf, "n", "<down>", ":+1<cr>:lua require'csvtools'.Highlight()<cr>", opts)
end
function M.setup(opts)
M = vim.tbl_deep_extend("force", M, opts)
Expand Down
6 changes: 6 additions & 0 deletions lua/csvtools/highlight.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,25 @@ local M = {}
--@param buf winbuf
--@param line string
function M.highlighttop(buf, line)
local header = {}
local cout = 1
local length = 0
for i = 1, #line do
if line:sub(i, i) ~= "|" then
length = length + 1
if cout % 2 == 0 then
--print(line:sub(i, i))
api.nvim_buf_add_highlight(buf, -1, "WhidHeader", 0, i - 1, i)
else
api.nvim_buf_add_highlight(buf, -1, "WhidSubHeader", 0, i - 1, i)
end
else
table.insert(header,length)
length = 0
cout = cout + 1
end
end
return header
end

--@param buf winbuf
Expand Down
57 changes: 57 additions & 0 deletions lua/csvtools/overflowtext.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
local M = {}
local function Split(s, delimiter)
local result = {};
for match in (s..delimiter):gmatch("(.-)"..delimiter) do
table.insert(result, match);
end
return result;
end
local ns_id = vim.api.nvim_create_namespace('demo')
function M.OverFlow(line_num, header)
local bnr = vim.fn.bufnr('%')
local line = unpack(vim.api.nvim_buf_get_lines(0, line_num-1 , line_num , true))
local output = Split(line,',')
for count=1,#header do
if output[count] == nil then
output[count] = ''
end
local len = string.len(output[count])
if len < header[count] then
for _=1, header[count]-len do
output[count] = output[count]..' '
end
elseif len > header[count] then
--print(len," ",header[count])
output[count] = output[count]:sub(1,header[count]-2)
output[count] = output[count]..'..'
--print("len=" , string.len(output[count]))
end
--print(header[count])
end
local virt_text = {}
for count=1,#output do
if count % 2 == 0 then
table.insert(virt_text,{output[count],"WhidHeader"})
else
table.insert(virt_text,{output[count],"WhidSubHeader"})
end
table.insert(virt_text,{"|"})
--table.insert(virt_text,{text,"WhidHeader"})
--print(output[count])
end
local opts = {
end_line = 10,
id = 1,
virt_text = virt_text,
virt_text_pos = 'overlay',
-- virt_text_win_col = 20,
}
--highlighttop2(bnr, text)
--print("sss")
return {
markid = vim.api.nvim_buf_set_extmark(bnr, ns_id, line_num - 1 , 0, opts),
ns_id = ns_id,
id = 1,
}
end
return M

0 comments on commit ed2a770

Please sign in to comment.