diff options
author | Josh Rahm <rahm@google.com> | 2024-03-12 23:04:21 +0000 |
---|---|---|
committer | Josh Rahm <rahm@google.com> | 2024-03-12 23:04:21 +0000 |
commit | d7415ac09d7afecc430d64ea17b57ceab7432c2e (patch) | |
tree | 7ab43d8c002c7b31d119a9b5e5dfe67a2e164489 /lua/warp/util.lua | |
parent | ff00585c7c9e0cac8d1c75a8cb60fd82519b6856 (diff) | |
download | nvim-warp-d7415ac09d7afecc430d64ea17b57ceab7432c2e.tar.gz nvim-warp-d7415ac09d7afecc430d64ea17b57ceab7432c2e.tar.bz2 nvim-warp-d7415ac09d7afecc430d64ea17b57ceab7432c2e.zip |
Big refactor to warp.
Now two lines are displayed for row-choosing. Made row-choosing modular
as well.
Diffstat (limited to 'lua/warp/util.lua')
-rw-r--r-- | lua/warp/util.lua | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/lua/warp/util.lua b/lua/warp/util.lua new file mode 100644 index 0000000..4f1858e --- /dev/null +++ b/lua/warp/util.lua @@ -0,0 +1,77 @@ +local M = {} + +M.next_char = function() + vim.cmd("redraw!") + return vim.fn.nr2char(vim.fn.getchar()) +end + +M.new_panel = function(buf, row, col, width, height) + local w = vim.api.nvim_open_win(buf, false, { + relative = 'win', + row = row, + col = col, + width = width, + height = height, + focusable = true, + style = 'minimal' + }) + vim.api.nvim_win_set_option(w, "winhighlight", "Normal:WarpNormal") + return w +end + +M.open_horiz = function() + local current_win = vim.api.nvim_get_current_win() + local curpos = vim.api.nvim_win_get_cursor(0) + local topline = vim.fn.line('w0') + + local width_of_garbage = vim.fn.getwininfo(current_win)[1].textoff + local width = vim.api.nvim_win_get_width(current_win) - width_of_garbage + + local horiz_bufnr = vim.api.nvim_create_buf(0, 1) + local line_at = vim.fn.getline(curpos[1]) + local max_width = math.min(width, #line_at + 1) + + return { + buf = horiz_bufnr, + panel = M.new_panel(horiz_bufnr, curpos[1] - topline + 1, width_of_garbage - 1, + max_width + 2, 1) + } +end + +M.wrap_col_selector = function(strat) + local cleanup = function(win) + vim.api.nvim_buf_delete(vim.api.nvim_win_get_buf(win), {force = true}) + end + local w + + return { + run = function() + local data = M.open_horiz() + w = data.panel + + local f = function () + local disp = strat.display() + if disp then + vim.api.nvim_buf_set_lines(data.buf, 0, -1, 0, {' ' .. disp}) + local ch = M.next_char() + while ch ~= '\x1b' and strat.on_char(ch) do + vim.api.nvim_buf_set_lines(data.buf, 0, -1, 0, + {' ' .. strat.display()}) + ch = M.next_char() + end + end + end + f () + + cleanup(w) + end + } +end + +M.cleanup = function(win) + if win then + vim.api.nvim_buf_delete(vim.api.nvim_win_get_buf(win), {force = true}) + end +end + +return M |