···2020---@param file string: file path
2121---@return table
2222local function read_lines(file)
2323- if not file_exists(file) then
2424- return {}
2525- end
2626- local lines = {}
2723 local f, err = io.open(file)
2824 if not f then
2925 error("Error opening file: " .. err)
3026 end
2727+ local lines = {}
3128 for line in f:lines() do
3229 lines[#lines + 1] = line
3330 end
···5350---@param line string: line from file being checked
5451---@return table|nil
5552local function check_for_entry(line)
5656- local i, l, ft, rt =
5757- string.match(line, "<YANKBANK_ENTRY:(%d+),(%d+),(%a+),(%a+)>")
5353+ local i, l, rt =
5454+ string.match(line, "<YANKBANK_ENTRY:(%d+),(%d+),(%a+)>")
5855 if i then
5956 return {
6057 index = tonumber(i),
6158 length = tonumber(l),
6262- file_type = ft,
6359 reg_type = rt,
6460 }
6561 end
···8581 if not f then
8682 error("Could not open file for reading: " .. err)
8783 end
8484+ -- FIX: extra newline on entries inserted after removal
88858986 -- read lines from file until matching entry is found
9087 local lines = {}
9191- -- TODO: does this for loop work?
9288 for line in f:lines() do
9393- print(line)
9489 if
9590 string.match(
9691 line,
9797- "<YANKBANK_ENTRY:" .. n_entries .. ",%d+,%a+,%a+>"
9292+ "<YANKBANK_ENTRY:" .. n_entries .. ",%d+,%a+>"
9893 )
9994 then
10095 n_entries = n_entries - 1
···112107 error("Could not open file for writing: " .. err)
113108 end
114109 for i = 1, #lines do
115115- f:write(lines[i])
110110+ -- TODO: check if newline is necessary for table
111111+ f:write(lines[i] .. "\n")
116112 end
117113 f:close()
118114end
119115116116+-- TODO: docs or remove function
120117local function open_file(file, mode)
121118 local f, err = io.open(file, mode)
122119 if not f then
···137134 end
138135 n_entries = n_entries + 1
139136140140- -- local f, err = io.open(file, "w")
141137 local lines = read_lines(file)
142142- print(vim.inspect(lines))
143138 local f = open_file(file, "w+")
144144- -- BUG: issues around here surrounding newline behavior
145145- -- - definintely an issue adding, only appears after inserting first entry
139139+140140+ -- add list header
146141 f:write("<YANKBANK_LIST:" .. n_entries .. ">\n")
147147- for i = 2, #lines do
148148- print(i)
149149- f:write(lines[i] .. "\n")
150150- print(lines[i])
151151- end
152142153143 -- write entry header
144144+ -- FIX: #entry doesn't match number of lines when it is string (number of chars instead of lines)
154145 f:write(
155155- "<YANKBANK_ENTRY:"
156156- .. n_entries
157157- .. ","
158158- .. #entry
159159- .. ",nil,"
160160- .. reg_type
161161- .. ">\n"
146146+ "<YANKBANK_ENTRY:1,"
147147+ .. #entry
148148+ .. ","
149149+ .. reg_type
150150+ .. ">\n"
162151 )
163163-164164- -- convert string entry to table
152152+ -- write entry
165153 if type(entry) == "string" then
166166- print(entry)
167167- -- local text = entry
168168- -- entry = {}
169169- -- for line in text:gmatch("[^\r\n]+") do
170170- -- table.insert(entry, line)
171171- -- end
172172- f:write(entry .. "\n")
173173- return
154154+ f:write(entry)
155155+ else
156156+ for i = 1, #entry do
157157+ -- TODO: check if newline is necessary for table
158158+ f:write(entry[i] .. "\n")
159159+ end
174160 end
175161176176- for i = 1, #entry do
177177- f:write(entry[i])
162162+ -- write remaining lines
163163+ for i = 2, #lines do
164164+ local n, l, rt = string.match(
165165+ lines[i],
166166+ "<YANKBANK_ENTRY:(%d+),(%d+),(%a+)>"
167167+ )
168168+ if n then
169169+ lines[i] = "<YANKBANK_ENTRY:"
170170+ .. n + 1
171171+ .. ","
172172+ .. l
173173+ .. ","
174174+ .. rt
175175+ .. ">"
176176+ end
177177+ -- TODO: check headers
178178+ f:write(lines[i] .. "\n")
178179 end
179180180181 f:close()
···202203 if res.index < max_entries then
203204 yanks[#yanks + 1] = entry
204205 reg_types[#reg_types + 1] = res.reg_type
205205- -- NOTE: ^ should I use table.insert instead? (would allow inserting at specific position)
206206 end
207207 -- skip lines that were added to entries
208208 i = i + res.length
···240240local yanks = {}
241241local reg_types = {}
242242M.setup_persistence("test.txt", 10, yanks, reg_types)
243243-print(vim.inspect(yanks))
244244-print(vim.inspect(reg_types))
243243+-- print(vim.inspect(yanks))
244244+-- print(vim.inspect(reg_types))
245245yanks = {}
246246reg_types = {}
247247-print(vim.inspect(yanks))
248248-print(vim.inspect(reg_types))
247247+-- print(vim.inspect(yanks))
248248+-- print(vim.inspect(reg_types))
249249M.setup_persistence("test1.txt", 10, yanks, reg_types)
250250m_entries = 10
251251-add_to_bankfile("test1.txt", "text", "v")
252252--- BUG: this isnt added to the testing file, file i/o might be too slow
253253-add_to_bankfile("test1.txt", "text1", "V")
251251+add_to_bankfile("test1.txt", "text11", "v")
252252+os.execute("sleep .1")
253253+-- add_to_bankfile("test1.txt", "text10", "V")
254254-- remove_last_entry("test1.txt")
255255256256return M