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

Configure Feed

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

fix(WIP fixed entry indices and some formatting issues)

+47 -47
+47 -47
lua/yankbank/persistence/file.lua
··· 20 20 ---@param file string: file path 21 21 ---@return table 22 22 local function read_lines(file) 23 - if not file_exists(file) then 24 - return {} 25 - end 26 - local lines = {} 27 23 local f, err = io.open(file) 28 24 if not f then 29 25 error("Error opening file: " .. err) 30 26 end 27 + local lines = {} 31 28 for line in f:lines() do 32 29 lines[#lines + 1] = line 33 30 end ··· 53 50 ---@param line string: line from file being checked 54 51 ---@return table|nil 55 52 local function check_for_entry(line) 56 - local i, l, ft, rt = 57 - string.match(line, "<YANKBANK_ENTRY:(%d+),(%d+),(%a+),(%a+)>") 53 + local i, l, rt = 54 + string.match(line, "<YANKBANK_ENTRY:(%d+),(%d+),(%a+)>") 58 55 if i then 59 56 return { 60 57 index = tonumber(i), 61 58 length = tonumber(l), 62 - file_type = ft, 63 59 reg_type = rt, 64 60 } 65 61 end ··· 85 81 if not f then 86 82 error("Could not open file for reading: " .. err) 87 83 end 84 + -- FIX: extra newline on entries inserted after removal 88 85 89 86 -- read lines from file until matching entry is found 90 87 local lines = {} 91 - -- TODO: does this for loop work? 92 88 for line in f:lines() do 93 - print(line) 94 89 if 95 90 string.match( 96 91 line, 97 - "<YANKBANK_ENTRY:" .. n_entries .. ",%d+,%a+,%a+>" 92 + "<YANKBANK_ENTRY:" .. n_entries .. ",%d+,%a+>" 98 93 ) 99 94 then 100 95 n_entries = n_entries - 1 ··· 112 107 error("Could not open file for writing: " .. err) 113 108 end 114 109 for i = 1, #lines do 115 - f:write(lines[i]) 110 + -- TODO: check if newline is necessary for table 111 + f:write(lines[i] .. "\n") 116 112 end 117 113 f:close() 118 114 end 119 115 116 + -- TODO: docs or remove function 120 117 local function open_file(file, mode) 121 118 local f, err = io.open(file, mode) 122 119 if not f then ··· 137 134 end 138 135 n_entries = n_entries + 1 139 136 140 - -- local f, err = io.open(file, "w") 141 137 local lines = read_lines(file) 142 - print(vim.inspect(lines)) 143 138 local f = open_file(file, "w+") 144 - -- BUG: issues around here surrounding newline behavior 145 - -- - definintely an issue adding, only appears after inserting first entry 139 + 140 + -- add list header 146 141 f:write("<YANKBANK_LIST:" .. n_entries .. ">\n") 147 - for i = 2, #lines do 148 - print(i) 149 - f:write(lines[i] .. "\n") 150 - print(lines[i]) 151 - end 152 142 153 143 -- write entry header 144 + -- FIX: #entry doesn't match number of lines when it is string (number of chars instead of lines) 154 145 f:write( 155 - "<YANKBANK_ENTRY:" 156 - .. n_entries 157 - .. "," 158 - .. #entry 159 - .. ",nil," 160 - .. reg_type 161 - .. ">\n" 146 + "<YANKBANK_ENTRY:1," 147 + .. #entry 148 + .. "," 149 + .. reg_type 150 + .. ">\n" 162 151 ) 163 - 164 - -- convert string entry to table 152 + -- write entry 165 153 if type(entry) == "string" then 166 - print(entry) 167 - -- local text = entry 168 - -- entry = {} 169 - -- for line in text:gmatch("[^\r\n]+") do 170 - -- table.insert(entry, line) 171 - -- end 172 - f:write(entry .. "\n") 173 - return 154 + f:write(entry) 155 + else 156 + for i = 1, #entry do 157 + -- TODO: check if newline is necessary for table 158 + f:write(entry[i] .. "\n") 159 + end 174 160 end 175 161 176 - for i = 1, #entry do 177 - f:write(entry[i]) 162 + -- write remaining lines 163 + for i = 2, #lines do 164 + local n, l, rt = string.match( 165 + lines[i], 166 + "<YANKBANK_ENTRY:(%d+),(%d+),(%a+)>" 167 + ) 168 + if n then 169 + lines[i] = "<YANKBANK_ENTRY:" 170 + .. n + 1 171 + .. "," 172 + .. l 173 + .. "," 174 + .. rt 175 + .. ">" 176 + end 177 + -- TODO: check headers 178 + f:write(lines[i] .. "\n") 178 179 end 179 180 180 181 f:close() ··· 202 203 if res.index < max_entries then 203 204 yanks[#yanks + 1] = entry 204 205 reg_types[#reg_types + 1] = res.reg_type 205 - -- NOTE: ^ should I use table.insert instead? (would allow inserting at specific position) 206 206 end 207 207 -- skip lines that were added to entries 208 208 i = i + res.length ··· 240 240 local yanks = {} 241 241 local reg_types = {} 242 242 M.setup_persistence("test.txt", 10, yanks, reg_types) 243 - print(vim.inspect(yanks)) 244 - print(vim.inspect(reg_types)) 243 + -- print(vim.inspect(yanks)) 244 + -- print(vim.inspect(reg_types)) 245 245 yanks = {} 246 246 reg_types = {} 247 - print(vim.inspect(yanks)) 248 - print(vim.inspect(reg_types)) 247 + -- print(vim.inspect(yanks)) 248 + -- print(vim.inspect(reg_types)) 249 249 M.setup_persistence("test1.txt", 10, yanks, reg_types) 250 250 m_entries = 10 251 - add_to_bankfile("test1.txt", "text", "v") 252 - -- BUG: this isnt added to the testing file, file i/o might be too slow 253 - add_to_bankfile("test1.txt", "text1", "V") 251 + add_to_bankfile("test1.txt", "text11", "v") 252 + os.execute("sleep .1") 253 + -- add_to_bankfile("test1.txt", "text10", "V") 254 254 -- remove_last_entry("test1.txt") 255 255 256 256 return M