Neovim plugin improving access to clipboard history (mirror)
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Added sqlite3.lua support, updates to README, persistence setup, added opts persist_path, persist_type

+103 -26
+4 -1
README.md
··· 54 54 | keymaps.yank | string | `"yy"` | 55 55 | keymaps.close | table of strings | `{ "<Esc>", "<C-c>", "q" }` | 56 56 | num_behavior | string defining jump behavior "prefix" or "jump" | `"prefix"` | 57 - 57 + | persist_type | string defining persistence type "sql" or "file" | `"sql"` | 58 + | persist_path | string defining path for persistence file | "/tmp/yankbank.db" | 58 59 59 60 If no separator is desired, pass in an empty string for sep: 60 61 ```lua ··· 67 68 navigation_prev = "k", 68 69 }, 69 70 num_behavior = "prefix", 71 + persist_type = "sql", 72 + persist_path = "/tmp/yankbank.db", 70 73 }) 71 74 end, 72 75 ```
+6
lua/yankbank/init.lua
··· 10 10 local reg_types = {} 11 11 local max_entries = 10 12 12 local sep = "-----" 13 + local persist_type = "file" 14 + local persist_path = "/tmp/yankbank.txt" 13 15 14 16 -- wrapper function for main plugin functionality 15 17 ---@param opts table|nil ··· 20 22 -- Fallback to defaults if necessary 21 23 local max_entries_opt = opts.max_entries or max_entries 22 24 local sep_opt = opts.sep or sep 25 + 23 26 opts.keymaps = opts.keymaps or {} 24 27 25 28 local bufnr, display_lines, line_yank_map = ··· 39 42 40 43 -- parse opts 41 44 max_entries = opts.max_entries or max_entries 45 + 46 + persist_type = opts.persist_type or persist_type 47 + persist_path = opts.persist_path or persist_path 42 48 43 49 -- create clipboard autocmds 44 50 clipboard.setup_yank_autocmd(yanks, reg_types, max_entries)
+2 -2
lua/yankbank/persistence.lua
··· 16 16 -- TODO: 17 17 require("persistence.file").setup_persistence( 18 18 yanks, 19 - opts.persist_file, 19 + opts.persist_path, 20 20 opts.max_entries 21 21 ) 22 22 elseif opts.method == "sqlite" then 23 23 -- TODO: 24 - print("sqlite persistence not yet implemented.") 24 + require("persistence.sql").init_db(yanks, opts.persist_path) 25 25 end 26 26 end 27 27
+11 -23
lua/yankbank/persistence/file.lua
··· 50 50 ---@param line string: line from file being checked 51 51 ---@return table|nil 52 52 local function check_for_entry(line) 53 - local i, l, rt = 54 - string.match(line, "<YANKBANK_ENTRY:(%d+),(%d+),(%a+)>") 53 + local i, l, rt = string.match(line, "<YANKBANK_ENTRY:(%d+),(%d+),(%a+)>") 55 54 if i then 56 55 return { 57 56 index = tonumber(i), ··· 87 86 local lines = {} 88 87 for line in f:lines() do 89 88 if 90 - string.match( 91 - line, 92 - "<YANKBANK_ENTRY:" .. n_entries .. ",%d+,%a+>" 93 - ) 89 + string.match(line, "<YANKBANK_ENTRY:" .. n_entries .. ",%d+,%a+>") 94 90 then 95 91 n_entries = n_entries - 1 96 92 lines[1] = "<YANKBANK_LIST:" .. n_entries .. ">\n" ··· 142 138 143 139 -- write entry header 144 140 -- FIX: #entry doesn't match number of lines when it is string (number of chars instead of lines) 145 - f:write( 146 - "<YANKBANK_ENTRY:1," 147 - .. #entry 148 - .. "," 149 - .. reg_type 150 - .. ">\n" 151 - ) 141 + f:write("<YANKBANK_ENTRY:1," .. #entry .. "," .. reg_type .. ">\n") 152 142 -- write entry 153 143 if type(entry) == "string" then 154 144 f:write(entry) ··· 161 151 162 152 -- write remaining lines 163 153 for i = 2, #lines do 164 - local n, l, rt = string.match( 165 - lines[i], 166 - "<YANKBANK_ENTRY:(%d+),(%d+),(%a+)>" 167 - ) 154 + local n, l, rt = 155 + string.match(lines[i], "<YANKBANK_ENTRY:(%d+),(%d+),(%a+)>") 168 156 if n then 169 157 lines[i] = "<YANKBANK_ENTRY:" 170 - .. n + 1 171 - .. "," 172 - .. l 173 - .. "," 174 - .. rt 175 - .. ">" 158 + .. n + 1 159 + .. "," 160 + .. l 161 + .. "," 162 + .. rt 163 + .. ">" 176 164 end 177 165 -- TODO: check headers 178 166 f:write(lines[i] .. "\n")
+80
lua/yankbank/persistence/sql.lua
··· 1 + local M = {} 2 + 3 + local sqlite = require("sqlite.db") 4 + 5 + -- @TODO: yank primary key? 6 + -- integer tracking for table position not controlled by sqlite3 7 + 8 + -- create db table for yanks, PK is row id and will increment automatically 9 + -- @param existing_yanks table 10 + -- @param uri string 11 + -- @return sqlite_db 12 + local function init_db(existing_yanks, uri) 13 + local db = sqlite({ 14 + uri = uri, 15 + }) 16 + db:open() 17 + 18 + db:create("yanks", { 19 + id = true, 20 + yank_content = { "text", required = true }, 21 + reg_type = { "text", required = true }, 22 + ensure = true, 23 + }) 24 + local status = db:status() 25 + 26 + if status ~= nil then 27 + print("yankbank db error: " + status.code) 28 + end 29 + -- @TODO: add functionality to add existing yanks to the db table "yanks" 30 + db:close() 31 + return db 32 + end 33 + 34 + -- add entry to DB 35 + -- @param db sqlite_db 36 + -- @param yank_content string 37 + -- @param reg_type string 38 + -- @return boolean 39 + local function add_to_yanktable(db, yank_content, reg_type) 40 + db:open() 41 + db:insert("yanks", { yank_content = yank_content, reg_type = reg_type }) 42 + local status = db:status() 43 + db:close() 44 + return status == nil 45 + end 46 + 47 + -- removes entry from yanktable 48 + -- @param db sqlite_db 49 + -- @param yank_content string 50 + -- @return boolean 51 + local function remove_from_yanktable(db, yank_content) 52 + db:open() 53 + db:delete("yanks", { where = { yank_content = yank_content } }) 54 + local status = db:status() 55 + db:close() 56 + return status == nil 57 + end 58 + 59 + -- returns all yanks in table sorted by recency descending 60 + -- @param db sqlite_db 61 + -- @return table[] 62 + local function get_yanks(db) 63 + db:open() 64 + local ret = db:select("yanks", { order_by = { asc = "id" } }) 65 + db:close() 66 + return ret 67 + end 68 + 69 + -- test function for db operations 70 + -- local function test_db() 71 + -- local test_db = init_db("/tmp/test_yankbank.db") 72 + -- add_to_yanktable(test_db, "Sample Yank", "reg") 73 + -- print(vim.inspect(get_yanks(test_db))) 74 + -- add_to_yanktable(test_db, "Sample Different Yank", "reg") 75 + -- remove_from_yanktable(test_db, "Sample Different Yank") 76 + -- print("after delete") 77 + -- print(vim.inspect(get_yanks(test_db))) 78 + -- end 79 + 80 + return M