Neovim plugin improving access to clipboard history (mirror)

feat: added options to plugin setup

+53 -22
+17
README.md
··· 35 35 } 36 36 ``` 37 37 38 + The setup function also supports taking in a table of options: 39 + | Option | Type | Default | 40 + |-------------|--------------------------------------------|----------------| 41 + | max_entries | integer number of entries to show in popup | 10 | 42 + | sep | string separator to show between table entries | "-----" | 43 + 44 + 45 + If no separator is desired, pass in an empty string for sep: 46 + ```lua 47 + config = function() 48 + require('yankbank').setup({ 49 + max_entries = 12, 50 + sep = "", 51 + }) 52 + end, 53 + ``` 54 + 38 55 ## Usage 39 56 40 57 The popup menu can be opened with the command:`:YankBank`, an entry is pasted at the current cursor position by hitting enter, and the menu can be closed by hitting escape, ctrl-c, or q.
+5 -4
lua/yankbank/data.lua
··· 2 2 local M = {} 3 3 4 4 -- reformat yanks table for popup 5 - function M.get_display_lines(yanks) 5 + function M.get_display_lines(yanks, sep) 6 6 local display_lines = {} 7 7 local line_yank_map = {} 8 8 local yank_num = 0 ··· 39 39 table.insert(line_yank_map, i) 40 40 end 41 41 42 - -- Add a visual separator between yanks, aligned with the yank content 43 - -- TODO: allow turning off/on in plugin setup 44 42 if i < #yanks then 45 - table.insert(display_lines, string.rep(" ", max_digits + 2) .. "------") 43 + -- Add a visual separator between yanks, aligned with the yank content 44 + if sep ~= "" then 45 + table.insert(display_lines, string.rep(" ", max_digits + 2) .. sep) 46 + end 46 47 table.insert(line_yank_map, false) 47 48 end 48 49 end
+25 -12
lua/yankbank/init.lua
··· 8 8 -- initialize yanks tables 9 9 local yanks = {} 10 10 local max_entries = 10 11 + local sep = "-----" 11 12 12 13 -- wrapper function for main plugin functionality 13 - local function show_yank_bank() 14 - -- TODO: update max entries with passed in options 15 - local bufnr, display_lines, line_yank_map = menu.create_and_fill_buffer(yanks, max_entries) 14 + local function show_yank_bank(args) 15 + -- Parse command arguments directly if args are provided as a string 16 + local opts = {} 17 + if type(args) == "string" and args ~= "" then 18 + local parts = vim.split(args, "%s+", {true}) 19 + opts.max_entries = tonumber(parts[1]) 20 + opts.sep = parts[2] 21 + elseif type(args) == "table" then 22 + -- If opts is already a table, use it directly (for programmatic calls) 23 + opts = args 24 + end 25 + 26 + -- Fallback to defaults if necessary 27 + local max_entries_opt = opts.max_entries or max_entries 28 + local sep_opt = opts.sep or sep 29 + 30 + local bufnr, display_lines, line_yank_map = menu.create_and_fill_buffer(yanks, max_entries_opt, sep_opt) 16 31 local win_id = menu.open_window(bufnr, display_lines) 17 32 menu.set_keymaps(win_id, bufnr, yanks, line_yank_map) 18 33 end 19 34 20 35 -- plugin setup 21 36 function M.setup(opts) 22 - local o = {} 37 + opts = opts or {} 23 38 24 39 -- parse opts 25 - if opts ~= nil then 26 - o.max_entries = opts.max_entries or max_entries 27 - else 28 - o.max_entries = max_entries 29 - end 40 + max_entries = opts.max_entries or max_entries 41 + sep = opts.sep or sep 30 42 31 43 -- create clipboard autocmds 32 - clipboard.setup_yank_autocmd(yanks, o.max_entries) 44 + clipboard.setup_yank_autocmd(yanks, max_entries) 33 45 34 46 -- Create user command 35 47 -- TODO: allow params (i.e. keymaps/max_entries/separator) 36 - vim.api.nvim_create_user_command("YankBank", show_yank_bank, 37 - { desc = "Show Recent Yanks" }) 48 + vim.api.nvim_create_user_command("YankBank", function(args) 49 + show_yank_bank(args.args) 50 + end, { desc = "Show Recent Yanks", nargs = "*" }) 38 51 end 39 52 40 53 return M
+6 -6
lua/yankbank/menu.lua
··· 7 7 local helpers = require("yankbank.helpers") 8 8 9 9 -- create new buffer and reformat yank table for ui 10 - function M.create_and_fill_buffer(yanks, max_entries) 10 + function M.create_and_fill_buffer(yanks, max_entries, sep) 11 11 -- check the content of the system clipboard register 12 12 -- TODO: this could be replaced with some sort of polling of the + register 13 13 local text = vim.fn.getreg('+') ··· 29 29 local current_filetype = vim.bo.filetype 30 30 vim.api.nvim_buf_set_option(bufnr, 'filetype', current_filetype) 31 31 32 - local display_lines, line_yank_map = data.get_display_lines(yanks) 32 + local display_lines, line_yank_map = data.get_display_lines(yanks, sep) 33 33 34 34 -- replace current buffer contents with updated table 35 35 vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, display_lines) ··· 46 46 end 47 47 48 48 -- define buffer window width and height based on number of columns 49 - local width = math.min(max_width + 4, vim.api.nvim_get_option("columns") - 50) 50 - local height = math.min(#display_lines, vim.api.nvim_get_option("lines") - 4) 49 + local width = math.min(max_width + 4, vim.api.nvim_get_option("columns")) 50 + local height = math.min(#display_lines, vim.api.nvim_get_option("lines")) 51 51 52 52 -- open window 53 53 local win_id = vim.api.nvim_open_win(bufnr, true, { 54 54 relative = "editor", 55 55 width = width, 56 56 height = height, 57 - col = math.floor((vim.api.nvim_get_option("columns") - width) / 2 - 1), 58 - row = math.floor((vim.api.nvim_get_option("lines") - height) / 2 - 1), 57 + col = math.floor((vim.api.nvim_get_option("columns") - width) / 2), 58 + row = math.floor((vim.api.nvim_get_option("lines") - height) / 2), 59 59 border = "rounded", 60 60 style = "minimal", 61 61 })