Skip to content

Commit ed2a770

Browse files
author
cht
committed
add overflowtext
1 parent 41dec58 commit ed2a770

4 files changed

Lines changed: 91 additions & 7 deletions

File tree

ftdetect/csv-tools.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
au BufRead,BufNewFile *.csv,*.dat,*.tsv,*.tab set filetype=csv
22
au FileType csv command! -buffer CloseTopWindow lua require"csvtools".CloseWindow()
33
au FileType csv command! -buffer TopWindow lua require"csvtools".NewWindow()
4+
autocmd! InsertEnter *.csv,*.bat,*.tsv,*.tab lua require"csvtools".deleteMark()
45
"au FIiletype csv autocmd CursorMoved csv lua require"csvtools".Highlight()

lua/csvtools.lua

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
local api = vim.api
22
local highlight = require("csvtools.highlight")
3+
local overflow = require("csvtools.overflowtext")
34
local M = {
45
winid = nil,
56
buf = nil,
67
mainwindowbuf = nil,
8+
header = {},
79
before = 20,
810
after = 20,
911
clearafter = true,
12+
overflowtext = {
13+
markid = nil,
14+
ns_id = nil,
15+
id = nil,
16+
}
1017
}
11-
18+
function M.printheader()
19+
--for count = 1, #M.header do
20+
-- print(M.header[count])
21+
--end
22+
return M.header
23+
end
1224
function M.NewWindow()
1325
if M.winid == nil then
1426
M.mainwindowbuf = vim.api.nvim_get_current_buf()
@@ -26,9 +38,7 @@ function M.NewWindow()
2638
local win = vim.api.nvim_get_current_win()
2739
api.nvim_buf_set_lines(buf, 0, -1, false, { messages })
2840
api.nvim_win_set_buf(win, buf)
29-
highlight.highlighttop(buf, messages)
30-
--api.nvim_buf_add_highlight(buf, -1, 'WhidHeader',0,0,1)
31-
--api.nvim_buf_add_highlight(buf, -1, 'WhidSubHeader', 0, 1, 2)
41+
M.header = highlight.highlighttop(buf, messages)
3242
M.winid = win
3343
M.buf = buf
3444
M.add_mappings()
@@ -39,7 +49,8 @@ function M.CloseWindow()
3949
if M.winid ~= nil then
4050
vim.api.nvim_win_close(M.winid, true)
4151
M.winid = nil
42-
M.buf = nil
52+
M.buf = nil
53+
M.header = {}
4354
end
4455
end
4556

@@ -61,13 +72,15 @@ function M.Highlight()
6172
if vim.o.filetype == "csv" then
6273
M.mainwindowbuf = vim.api.nvim_get_current_buf()
6374
local line, _ = unpack(vim.api.nvim_win_get_cursor(0))
75+
--print(line)
6476
local length = vim.api.nvim_buf_line_count(M.mainwindowbuf)
6577
if M.clearafter then
6678
api.nvim_buf_clear_highlight(M.mainwindowbuf, -1, 0, length)
6779
end
6880
local start, final = getrange(line, length)
6981
--print(start)
7082
--print(final)
83+
M.overflowtext= overflow.OverFlow(line,M.header)
7184
for i = start, line, 1 do
7285
highlight.highlight(M.mainwindowbuf, i)
7386
end
@@ -76,6 +89,13 @@ function M.Highlight()
7689
end
7790
end
7891
end
92+
function M.deleteMark()
93+
vim.api.nvim_buf_del_extmark(
94+
M.overflowtext.markid,
95+
M.overflowtext.ns_id,
96+
M.overflowtext.id
97+
)
98+
end
7999
function M.add_mappings()
80100
M.mainwindowbuf = vim.api.nvim_get_current_buf()
81101
--print(M.mainwindowbuf)
@@ -84,8 +104,8 @@ function M.add_mappings()
84104
vim.api.nvim_buf_set_keymap(M.mainwindowbuf, "n", "<leader>tf", ":lua require'csvtools'.NewWindow()<cr>", opts)
85105
vim.api.nvim_buf_set_keymap(M.buf, "n", "<leader>td", ":lua require'csvtools'.CloseWindow()<cr>", opts)
86106
vim.api.nvim_buf_set_keymap(M.mainwindowbuf, "n", "<leader>td", ":lua require'csvtools'.CloseWindow()<cr>", opts)
87-
vim.api.nvim_buf_set_keymap(M.mainwindowbuf, "n", "<up>", ":lua require'csvtools'.Highlight()<cr>:-1<cr>", opts)
88-
vim.api.nvim_buf_set_keymap(M.mainwindowbuf, "n", "<down>", ":lua require'csvtools'.Highlight()<cr>:+1<cr>", opts)
107+
vim.api.nvim_buf_set_keymap(M.mainwindowbuf, "n", "<up>", ":-1<cr>:lua require'csvtools'.Highlight()<cr>", opts)
108+
vim.api.nvim_buf_set_keymap(M.mainwindowbuf, "n", "<down>", ":+1<cr>:lua require'csvtools'.Highlight()<cr>", opts)
89109
end
90110
function M.setup(opts)
91111
M = vim.tbl_deep_extend("force", M, opts)

lua/csvtools/highlight.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,25 @@ local M = {}
44
--@param buf winbuf
55
--@param line string
66
function M.highlighttop(buf, line)
7+
local header = {}
78
local cout = 1
9+
local length = 0
810
for i = 1, #line do
911
if line:sub(i, i) ~= "|" then
12+
length = length + 1
1013
if cout % 2 == 0 then
1114
--print(line:sub(i, i))
1215
api.nvim_buf_add_highlight(buf, -1, "WhidHeader", 0, i - 1, i)
1316
else
1417
api.nvim_buf_add_highlight(buf, -1, "WhidSubHeader", 0, i - 1, i)
1518
end
1619
else
20+
table.insert(header,length)
21+
length = 0
1722
cout = cout + 1
1823
end
1924
end
25+
return header
2026
end
2127

2228
--@param buf winbuf

lua/csvtools/overflowtext.lua

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
local M = {}
2+
local function Split(s, delimiter)
3+
local result = {};
4+
for match in (s..delimiter):gmatch("(.-)"..delimiter) do
5+
table.insert(result, match);
6+
end
7+
return result;
8+
end
9+
local ns_id = vim.api.nvim_create_namespace('demo')
10+
function M.OverFlow(line_num, header)
11+
local bnr = vim.fn.bufnr('%')
12+
local line = unpack(vim.api.nvim_buf_get_lines(0, line_num-1 , line_num , true))
13+
local output = Split(line,',')
14+
for count=1,#header do
15+
if output[count] == nil then
16+
output[count] = ''
17+
end
18+
local len = string.len(output[count])
19+
if len < header[count] then
20+
for _=1, header[count]-len do
21+
output[count] = output[count]..' '
22+
end
23+
elseif len > header[count] then
24+
--print(len," ",header[count])
25+
output[count] = output[count]:sub(1,header[count]-2)
26+
output[count] = output[count]..'..'
27+
--print("len=" , string.len(output[count]))
28+
end
29+
--print(header[count])
30+
end
31+
local virt_text = {}
32+
for count=1,#output do
33+
if count % 2 == 0 then
34+
table.insert(virt_text,{output[count],"WhidHeader"})
35+
else
36+
table.insert(virt_text,{output[count],"WhidSubHeader"})
37+
end
38+
table.insert(virt_text,{"|"})
39+
--table.insert(virt_text,{text,"WhidHeader"})
40+
--print(output[count])
41+
end
42+
local opts = {
43+
end_line = 10,
44+
id = 1,
45+
virt_text = virt_text,
46+
virt_text_pos = 'overlay',
47+
-- virt_text_win_col = 20,
48+
}
49+
--highlighttop2(bnr, text)
50+
--print("sss")
51+
return {
52+
markid = vim.api.nvim_buf_set_extmark(bnr, ns_id, line_num - 1 , 0, opts),
53+
ns_id = ns_id,
54+
id = 1,
55+
}
56+
end
57+
return M

0 commit comments

Comments
 (0)