|
| 1 | +local M = {} |
| 2 | + |
| 3 | +---@param results string[] |
| 4 | +---@param callback fun(integer) |
| 5 | +function M.createTelescopeWindow(results, callback, prompt_title, results_title) |
| 6 | + local pickers = require("telescope.pickers") |
| 7 | + local finders = require("telescope.finders") |
| 8 | + local conf = require("telescope.config").values |
| 9 | + |
| 10 | + local max_height = 40 |
| 11 | + local height = math.min(#results + 4, max_height) |
| 12 | + pickers |
| 13 | + .new({}, { |
| 14 | + prompt_title = prompt_title, |
| 15 | + results_title = results_title, |
| 16 | + finder = finders.new_table({ |
| 17 | + results = results, |
| 18 | + }), |
| 19 | + sorter = conf.generic_sorter({}), |
| 20 | + layout_strategy = "center", |
| 21 | + layout_config = { |
| 22 | + width = 80, |
| 23 | + height = height, |
| 24 | + prompt_position = "bottom", |
| 25 | + }, |
| 26 | + attach_mappings = function(_, map) |
| 27 | + map("i", "<CR>", function(prompt_bufnr) |
| 28 | + local selection = require("telescope.actions.state").get_selected_entry() |
| 29 | + require("telescope.actions").close(prompt_bufnr) |
| 30 | + callback(selection.index) |
| 31 | + end) |
| 32 | + return true |
| 33 | + end, |
| 34 | + }) |
| 35 | + :find() |
| 36 | +end |
| 37 | + |
| 38 | +---@param options string[] |
| 39 | +---@param callback fun(integer) |
| 40 | +function M.createWindow(options, callback) |
| 41 | + local buf = vim.api.nvim_create_buf(false, true) |
| 42 | + local width = 80 |
| 43 | + local max_height = 20 |
| 44 | + local height = math.min(#options + 2, max_height) |
| 45 | + |
| 46 | + local ui = vim.api.nvim_list_uis()[1] |
| 47 | + |
| 48 | + local opts = { |
| 49 | + relative = "editor", |
| 50 | + width = width, |
| 51 | + height = height, |
| 52 | + row = math.floor((ui.height - height) / 2), |
| 53 | + col = math.floor((ui.width - width) / 2), |
| 54 | + anchor = "NW", |
| 55 | + style = "minimal", |
| 56 | + border = "rounded", |
| 57 | + } |
| 58 | + |
| 59 | + local win = vim.api.nvim_open_win(buf, false, opts) |
| 60 | + vim.api.nvim_set_current_win(win) |
| 61 | + vim.api.nvim_buf_set_lines(buf, 0, -1, true, options) |
| 62 | + vim.bo[buf].modifiable = false |
| 63 | + vim.bo[buf].buftype = "nofile" |
| 64 | + vim.api.nvim_set_option_value("winhl", "Normal:MyHighlight", { win = win }) |
| 65 | + vim.api.nvim_win_set_cursor(win, { 1, 0 }) |
| 66 | + |
| 67 | + local function select_current_line() |
| 68 | + local row, _ = unpack(vim.api.nvim_win_get_cursor(win)) |
| 69 | + vim.api.nvim_win_close(win, true) |
| 70 | + callback(row) |
| 71 | + end |
| 72 | + |
| 73 | + vim.keymap.set("n", "q", function() |
| 74 | + vim.api.nvim_win_close(win, true) |
| 75 | + end, { buffer = buf, silent = true }) |
| 76 | + vim.keymap.set("n", "<CR>", select_current_line, { buffer = buf, silent = true }) |
| 77 | + |
| 78 | + for i in ipairs(options) do |
| 79 | + vim.keymap.set("n", tostring(i), function() |
| 80 | + vim.api.nvim_win_close(win, true) |
| 81 | + end, { buffer = buf, silent = true }) |
| 82 | + end |
| 83 | +end |
| 84 | + |
| 85 | +return M |
0 commit comments