|
| 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 |
0 commit comments