Skip to content

Commit 17b364b

Browse files
author
cht
committed
allow to select if clear
1 parent ed2a770 commit 17b364b

4 files changed

Lines changed: 75 additions & 76 deletions

File tree

ftdetect/csv-tools.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ 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()
44
autocmd! InsertEnter *.csv,*.bat,*.tsv,*.tab lua require"csvtools".deleteMark()
5+
autocmd! InsertLeave *.csv,*.bat,*.tsv,*.tab lua require"csvtools".Highlight()
56
"au FIiletype csv autocmd CursorMoved csv lua require"csvtools".Highlight()

lua/csvtools.lua

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,22 @@ local M = {
55
winid = nil,
66
buf = nil,
77
mainwindowbuf = nil,
8-
header = {},
8+
header = {},
99
before = 20,
1010
after = 20,
1111
clearafter = true,
12-
overflowtext = {
13-
markid = nil,
14-
ns_id = nil,
15-
id = nil,
16-
}
12+
overflowtext = {
13+
markid = nil,
14+
ns_id = nil,
15+
id = nil,
16+
},
1717
}
18-
function M.printheader()
19-
--for count = 1, #M.header do
20-
-- print(M.header[count])
21-
--end
22-
return M.header
18+
function M.Ifclear()
19+
if M.clearafter then
20+
M.clearafter = false
21+
else
22+
M.clearafter = true
23+
end
2324
end
2425
function M.NewWindow()
2526
if M.winid == nil then
@@ -49,8 +50,8 @@ function M.CloseWindow()
4950
if M.winid ~= nil then
5051
vim.api.nvim_win_close(M.winid, true)
5152
M.winid = nil
52-
M.buf = nil
53-
M.header = {}
53+
M.buf = nil
54+
M.header = {}
5455
end
5556
end
5657

@@ -72,15 +73,15 @@ function M.Highlight()
7273
if vim.o.filetype == "csv" then
7374
M.mainwindowbuf = vim.api.nvim_get_current_buf()
7475
local line, _ = unpack(vim.api.nvim_win_get_cursor(0))
75-
--print(line)
76+
--print(line)
7677
local length = vim.api.nvim_buf_line_count(M.mainwindowbuf)
7778
if M.clearafter then
7879
api.nvim_buf_clear_highlight(M.mainwindowbuf, -1, 0, length)
7980
end
8081
local start, final = getrange(line, length)
8182
--print(start)
8283
--print(final)
83-
M.overflowtext= overflow.OverFlow(line,M.header)
84+
M.overflowtext = overflow.OverFlow(line, M.header)
8485
for i = start, line, 1 do
8586
highlight.highlight(M.mainwindowbuf, i)
8687
end
@@ -90,11 +91,7 @@ function M.Highlight()
9091
end
9192
end
9293
function M.deleteMark()
93-
vim.api.nvim_buf_del_extmark(
94-
M.overflowtext.markid,
95-
M.overflowtext.ns_id,
96-
M.overflowtext.id
97-
)
94+
vim.api.nvim_buf_del_extmark(M.overflowtext.markid, M.overflowtext.ns_id, M.overflowtext.id)
9895
end
9996
function M.add_mappings()
10097
M.mainwindowbuf = vim.api.nvim_get_current_buf()
@@ -104,6 +101,7 @@ function M.add_mappings()
104101
vim.api.nvim_buf_set_keymap(M.mainwindowbuf, "n", "<leader>tf", ":lua require'csvtools'.NewWindow()<cr>", opts)
105102
vim.api.nvim_buf_set_keymap(M.buf, "n", "<leader>td", ":lua require'csvtools'.CloseWindow()<cr>", opts)
106103
vim.api.nvim_buf_set_keymap(M.mainwindowbuf, "n", "<leader>td", ":lua require'csvtools'.CloseWindow()<cr>", opts)
104+
vim.api.nvim_buf_set_keymap(M.mainwindowbuf, "n", "<leader>tg", ":lua require'csvtools'.Ifclear()<cr>", opts)
107105
vim.api.nvim_buf_set_keymap(M.mainwindowbuf, "n", "<up>", ":-1<cr>:lua require'csvtools'.Highlight()<cr>", opts)
108106
vim.api.nvim_buf_set_keymap(M.mainwindowbuf, "n", "<down>", ":+1<cr>:lua require'csvtools'.Highlight()<cr>", opts)
109107
end

lua/csvtools/highlight.lua

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,25 @@ local M = {}
44
--@param buf winbuf
55
--@param line string
66
function M.highlighttop(buf, line)
7-
local header = {}
7+
local header = {}
88
local cout = 1
9-
local length = 0
9+
local length = 0
1010
for i = 1, #line do
1111
if line:sub(i, i) ~= "|" then
12-
length = length + 1
12+
length = length + 1
1313
if cout % 2 == 0 then
1414
--print(line:sub(i, i))
1515
api.nvim_buf_add_highlight(buf, -1, "WhidHeader", 0, i - 1, i)
1616
else
1717
api.nvim_buf_add_highlight(buf, -1, "WhidSubHeader", 0, i - 1, i)
1818
end
1919
else
20-
table.insert(header,length)
21-
length = 0
20+
table.insert(header, length)
21+
length = 0
2222
cout = cout + 1
2323
end
2424
end
25-
return header
25+
return header
2626
end
2727

2828
--@param buf winbuf

lua/csvtools/overflowtext.lua

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,57 @@
11
local M = {}
22
local function Split(s, delimiter)
3-
local result = {};
4-
for match in (s..delimiter):gmatch("(.-)"..delimiter) do
5-
table.insert(result, match);
3+
local result = {}
4+
for match in (s .. delimiter):gmatch("(.-)" .. delimiter) do
5+
table.insert(result, match)
66
end
7-
return result;
7+
return result
88
end
9-
local ns_id = vim.api.nvim_create_namespace('demo')
9+
local ns_id = vim.api.nvim_create_namespace("demo")
1010
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-
}
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+
}
5656
end
5757
return M

0 commit comments

Comments
 (0)