Skip to content

Commit 4ad5a32

Browse files
author
cht
committed
first
0 parents  commit 4ad5a32

4 files changed

Lines changed: 75 additions & 0 deletions

File tree

ftdetect/csv-tools.vim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
au FileType csv command! -buffer CloseTopWindow lua require"csvtools".CloseWindow()
2+
au FileType csv command! -buffer TopWindow lua require"csvtools".NewWindow()

lua/csvtools.lua

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
local api = vim.api
2+
3+
local M = {
4+
winid = nil,
5+
buf = nil,
6+
mainwindowbuf = nil
7+
}
8+
function M.NewWindow()
9+
if M.winid == nil then
10+
M.mainwindowbuf = vim.api.nvim_get_current_buf()
11+
local file = vim.api.nvim_buf_get_name(0)
12+
local f = io.open(file,"r")
13+
14+
local messages = f:read()
15+
if messages == nil then
16+
return
17+
end
18+
f:close()
19+
messages= messages:gsub("%,","|")
20+
local buf = api.nvim_create_buf(false, true) -- create new emtpy buffer
21+
vim.cmd[[sview]]
22+
api.nvim_win_set_height(0, 1)
23+
local win = vim.api.nvim_get_current_win()
24+
api.nvim_buf_set_lines(buf, 0, -1, false, { messages })
25+
api.nvim_win_set_buf(win,buf)
26+
require("csvtools.topbarhighlight").hightlight(buf,messages)
27+
--api.nvim_buf_add_highlight(buf, -1, 'WhidHeader',0,0,1)
28+
--api.nvim_buf_add_highlight(buf, -1, 'WhidSubHeader', 0, 1, 2)
29+
M.winid = win
30+
M.buf = buf
31+
M.add_mappings()
32+
end
33+
end
34+
function M.CloseWindow()
35+
if M.winid ~=nil then
36+
vim.api.nvim_win_close(M.winid,true)
37+
M.winid = nil
38+
M.buf = nil
39+
end
40+
end
41+
function M.add_mappings()
42+
--print(M.mainwindowbuf)
43+
local opts = { nowait = true, noremap = true, silent = true }
44+
--vim.api.nvim_buf_set_keymap(M.mainwindowbuf, "n", "<leader>td", ":lua require'csvtools'.CloseWindow<cr>", opts)
45+
vim.api.nvim_buf_set_keymap(M.mainwindowbuf, "n", "<leader>tf", ":lua require'csvtools'.NewWindow()<cr>", opts)
46+
vim.api.nvim_buf_set_keymap(M.buf, "n", "<leader>td", ":lua require'csvtools'.CloseWindow()<cr>", opts)
47+
vim.api.nvim_buf_set_keymap(M.mainwindowbuf, "n", "<leader>td", ":lua require'csvtools'.CloseWindow()<cr>", opts)
48+
end
49+
function M.setup(opts)
50+
M = vim.tbl_deep_extend("force",M, opts)
51+
end
52+
return M

lua/csvtools/topbarhighlight.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
local api = vim.api
2+
local M = {}
3+
function M.hightlight(buf,line)
4+
local cout = 1
5+
for i=1 ,#line do
6+
if line:sub(i,i) ~= '|' then
7+
if cout % 2 == 0 then
8+
cout = cout+ 1
9+
print(line:sub(i,i))
10+
api.nvim_buf_add_highlight(buf, -1, 'WhidHeader',0,i-1,i)
11+
else
12+
cout = cout +1
13+
api.nvim_buf_add_highlight(buf, -1, 'WhidSubHeader',0,i-1,i)
14+
end
15+
end
16+
end
17+
end
18+
return M

plugin/csv-tool.vim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
au FileType csv lua require"csvtools".NewWindow()
2+
hi def link WhidHeader Number
3+
hi def link WhidSubHeader Statement

0 commit comments

Comments
 (0)