|
| 1 | +--- Bufferinator menu module |
| 2 | +-- @module bufferinator.menu |
| 3 | +local actions = require("bufferinator.actions") |
| 4 | + |
| 5 | +local M = {} |
| 6 | + |
| 7 | +--- Open the Bufferinator floating menu. |
| 8 | +-- Shows buffer management options in a floating window. |
| 9 | +function M.open() |
| 10 | + local menu = { |
| 11 | + { |
| 12 | + desc = "Cancel the action", |
| 13 | + action = function() |
| 14 | + vim.notify("Cancelled", vim.log.levels.INFO) |
| 15 | + end, |
| 16 | + }, |
| 17 | + { desc = "Close hidden buffers", action = actions.close_hidden_buffers }, |
| 18 | + { desc = "Close current buffer", action = actions.close_current_buffer }, |
| 19 | + { desc = "Close all buffers except current", action = actions.close_other_buffers }, |
| 20 | + { desc = "Close all buffers (including current)", action = actions.close_all_buffers }, |
| 21 | + } |
| 22 | + |
| 23 | + local lines = { |
| 24 | + "Select action:", |
| 25 | + "1: " .. menu[1].desc, |
| 26 | + "2: " .. menu[2].desc, |
| 27 | + "3: " .. menu[3].desc, |
| 28 | + "4: " .. menu[4].desc, |
| 29 | + "5: " .. menu[5].desc, |
| 30 | + "", |
| 31 | + "Press number or q/Esc/Ctrl+C to cancel", |
| 32 | + } |
| 33 | + |
| 34 | + local width = 0 |
| 35 | + for _, line in ipairs(lines) do |
| 36 | + width = math.max(width, #line) |
| 37 | + end |
| 38 | + width = width + 4 |
| 39 | + |
| 40 | + local height = #lines + 2 |
| 41 | + |
| 42 | + local buf = vim.api.nvim_create_buf(false, true) |
| 43 | + vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines) |
| 44 | + vim.bo[buf].modifiable = false |
| 45 | + vim.bo[buf].buftype = "nofile" |
| 46 | + vim.bo[buf].bufhidden = "wipe" |
| 47 | + vim.bo[buf].swapfile = false |
| 48 | + |
| 49 | + local win = vim.api.nvim_open_win(buf, true, { |
| 50 | + relative = "editor", |
| 51 | + width = width, |
| 52 | + height = height, |
| 53 | + row = math.floor((vim.o.lines - height) / 2), |
| 54 | + col = math.floor((vim.o.columns - width) / 2), |
| 55 | + style = "minimal", |
| 56 | + border = "rounded", |
| 57 | + zindex = 1000, |
| 58 | + }) |
| 59 | + |
| 60 | + local function close_menu() |
| 61 | + if vim.api.nvim_win_is_valid(win) then |
| 62 | + vim.api.nvim_win_close(win, true) |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + for i = 1, #menu do |
| 67 | + vim.keymap.set("n", tostring(i), function() |
| 68 | + close_menu() |
| 69 | + menu[i].action() |
| 70 | + end, { buffer = buf, nowait = true, noremap = true, silent = true }) |
| 71 | + end |
| 72 | + vim.keymap.set("n", "q", close_menu, { buffer = buf, nowait = true, noremap = true, silent = true }) |
| 73 | + vim.keymap.set("n", "<Esc>", close_menu, { buffer = buf, nowait = true, noremap = true, silent = true }) |
| 74 | + vim.keymap.set("n", "<C-c>", close_menu, { buffer = buf, nowait = true, noremap = true, silent = true }) |
| 75 | + |
| 76 | + vim.cmd("normal! gg") |
| 77 | +end |
| 78 | + |
| 79 | +return M |
0 commit comments