|
1 | 1 | local api = vim.api |
2 | 2 |
|
3 | 3 | local M = { |
4 | | - winid = nil, |
5 | | - buf = nil, |
6 | | - mainwindowbuf = nil, |
7 | | - before = 5, |
8 | | - after = 5, |
| 4 | + winid = nil, |
| 5 | + buf = nil, |
| 6 | + mainwindowbuf = nil, |
| 7 | + before = 5, |
| 8 | + after = 5, |
9 | 9 | } |
10 | 10 | local highlight = require("csvtools.topbarhighlight") |
11 | 11 | function M.NewWindow() |
12 | | - if M.winid == nil then |
13 | | - M.mainwindowbuf = vim.api.nvim_get_current_buf() |
14 | | - --local file = vim.api.nvim_buf_get_name(0) |
15 | | - --local f = io.open(file, "r") |
16 | | - local messages = unpack(api.nvim_buf_get_lines(M.mainwindowbuf, 0, 1, true)) |
17 | | - if messages == nil then |
18 | | - return |
19 | | - end |
20 | | - --f:close() |
21 | | - messages = messages:gsub("%,", "|") |
22 | | - local buf = api.nvim_create_buf(false, true) -- create new emtpy buffer |
23 | | - vim.cmd([[sview]]) |
24 | | - api.nvim_win_set_height(0, 1) |
25 | | - local win = vim.api.nvim_get_current_win() |
26 | | - api.nvim_buf_set_lines(buf, 0, -1, false, { messages }) |
27 | | - api.nvim_win_set_buf(win, buf) |
28 | | - highlight.highlighttop(buf, messages) |
29 | | - --api.nvim_buf_add_highlight(buf, -1, 'WhidHeader',0,0,1) |
30 | | - --api.nvim_buf_add_highlight(buf, -1, 'WhidSubHeader', 0, 1, 2) |
31 | | - M.winid = win |
32 | | - M.buf = buf |
33 | | - M.add_mappings() |
34 | | - end |
| 12 | + if M.winid == nil then |
| 13 | + M.mainwindowbuf = vim.api.nvim_get_current_buf() |
| 14 | + --local file = vim.api.nvim_buf_get_name(0) |
| 15 | + --local f = io.open(file, "r") |
| 16 | + local messages = unpack(api.nvim_buf_get_lines(M.mainwindowbuf, 0, 1, true)) |
| 17 | + if messages == nil then |
| 18 | + return |
| 19 | + end |
| 20 | + --f:close() |
| 21 | + messages = messages:gsub("%,", "|") |
| 22 | + local buf = api.nvim_create_buf(false, true) -- create new emtpy buffer |
| 23 | + vim.cmd([[sview]]) |
| 24 | + api.nvim_win_set_height(0, 1) |
| 25 | + local win = vim.api.nvim_get_current_win() |
| 26 | + api.nvim_buf_set_lines(buf, 0, -1, false, { messages }) |
| 27 | + api.nvim_win_set_buf(win, buf) |
| 28 | + highlight.highlighttop(buf, messages) |
| 29 | + --api.nvim_buf_add_highlight(buf, -1, 'WhidHeader',0,0,1) |
| 30 | + --api.nvim_buf_add_highlight(buf, -1, 'WhidSubHeader', 0, 1, 2) |
| 31 | + M.winid = win |
| 32 | + M.buf = buf |
| 33 | + M.add_mappings() |
| 34 | + end |
35 | 35 | end |
36 | 36 | function M.CloseWindow() |
37 | | - if M.winid ~= nil then |
38 | | - vim.api.nvim_win_close(M.winid, true) |
39 | | - M.winid = nil |
40 | | - M.buf = nil |
41 | | - end |
| 37 | + if M.winid ~= nil then |
| 38 | + vim.api.nvim_win_close(M.winid, true) |
| 39 | + M.winid = nil |
| 40 | + M.buf = nil |
| 41 | + end |
42 | 42 | end |
43 | 43 | local function getrange(line, length) |
44 | | - local start = 1 |
45 | | - if line - M.before > 1 then |
46 | | - start = line - M.before |
47 | | - end |
48 | | - local final = length |
49 | | - if line + M.after < length then |
50 | | - final = line + M.after |
51 | | - end |
52 | | - return start, final |
| 44 | + local start = 1 |
| 45 | + if line - M.before > 1 then |
| 46 | + start = line - M.before |
| 47 | + end |
| 48 | + local final = length |
| 49 | + if line + M.after < length then |
| 50 | + final = line + M.after |
| 51 | + end |
| 52 | + return start, final |
53 | 53 | end |
54 | 54 | function M.Highlight() |
55 | | - if vim.o.filetype == "csv" then |
56 | | - M.mainwindowbuf = vim.api.nvim_get_current_buf() |
57 | | - local line, _ = unpack(vim.api.nvim_win_get_cursor(0)) |
58 | | - local length = vim.api.nvim_buf_line_count(M.mainwindowbuf) |
59 | | - local start, final = getrange(line, length) |
60 | | - --print(start) |
61 | | - --print(final) |
62 | | - for i = start, line, 1 do |
63 | | - highlight.highlight(M.mainwindowbuf, i) |
64 | | - end |
65 | | - for i = line, final, 1 do |
66 | | - highlight.highlight(M.mainwindowbuf, i) |
67 | | - end |
68 | | - end |
| 55 | + if vim.o.filetype == "csv" then |
| 56 | + M.mainwindowbuf = vim.api.nvim_get_current_buf() |
| 57 | + local line, _ = unpack(vim.api.nvim_win_get_cursor(0)) |
| 58 | + local length = vim.api.nvim_buf_line_count(M.mainwindowbuf) |
| 59 | + local start, final = getrange(line, length) |
| 60 | + --print(start) |
| 61 | + --print(final) |
| 62 | + for i = start, line, 1 do |
| 63 | + highlight.highlight(M.mainwindowbuf, i) |
| 64 | + end |
| 65 | + for i = line, final, 1 do |
| 66 | + highlight.highlight(M.mainwindowbuf, i) |
| 67 | + end |
| 68 | + end |
69 | 69 | end |
70 | 70 | function M.add_mappings() |
71 | | - M.mainwindowbuf = vim.api.nvim_get_current_buf() |
72 | | - --print(M.mainwindowbuf) |
73 | | - local opts = { nowait = true, noremap = true, silent = true } |
74 | | - --vim.api.nvim_buf_set_keymap(M.mainwindowbuf, "n", "<leader>td", ":lua require'csvtools'.CloseWindow<cr>", opts) |
75 | | - vim.api.nvim_buf_set_keymap(M.mainwindowbuf, "n", "<leader>tf", ":lua require'csvtools'.NewWindow()<cr>", opts) |
76 | | - vim.api.nvim_buf_set_keymap(M.buf, "n", "<leader>td", ":lua require'csvtools'.CloseWindow()<cr>", opts) |
77 | | - vim.api.nvim_buf_set_keymap(M.mainwindowbuf, "n", "<leader>td", ":lua require'csvtools'.CloseWindow()<cr>", opts) |
78 | | - vim.api.nvim_buf_set_keymap(M.mainwindowbuf, "n", "<up>", ":lua require'csvtools'.Highlight()<cr>:-1<cr>", opts) |
79 | | - vim.api.nvim_buf_set_keymap(M.mainwindowbuf, "n", "<down>", ":lua require'csvtools'.Highlight()<cr>:+1<cr>", opts) |
| 71 | + M.mainwindowbuf = vim.api.nvim_get_current_buf() |
| 72 | + --print(M.mainwindowbuf) |
| 73 | + local opts = { nowait = true, noremap = true, silent = true } |
| 74 | + --vim.api.nvim_buf_set_keymap(M.mainwindowbuf, "n", "<leader>td", ":lua require'csvtools'.CloseWindow<cr>", opts) |
| 75 | + vim.api.nvim_buf_set_keymap(M.mainwindowbuf, "n", "<leader>tf", ":lua require'csvtools'.NewWindow()<cr>", opts) |
| 76 | + vim.api.nvim_buf_set_keymap(M.buf, "n", "<leader>td", ":lua require'csvtools'.CloseWindow()<cr>", opts) |
| 77 | + vim.api.nvim_buf_set_keymap(M.mainwindowbuf, "n", "<leader>td", ":lua require'csvtools'.CloseWindow()<cr>", opts) |
| 78 | + vim.api.nvim_buf_set_keymap(M.mainwindowbuf, "n", "<up>", ":lua require'csvtools'.Highlight()<cr>:-1<cr>", opts) |
| 79 | + vim.api.nvim_buf_set_keymap(M.mainwindowbuf, "n", "<down>", ":lua require'csvtools'.Highlight()<cr>:+1<cr>", opts) |
80 | 80 | end |
81 | 81 | function M.setup(opts) |
82 | | - M = vim.tbl_deep_extend("force", M, opts) |
| 82 | + M = vim.tbl_deep_extend("force", M, opts) |
83 | 83 | end |
84 | 84 | return M |
0 commit comments