Also save the indexes

This commit is contained in:
Diogo Duarte
2024-04-19 13:42:20 +01:00
parent 94c661afe3
commit be82c3072e

View File

@@ -24,40 +24,48 @@ local _send_notification = ya.sync(
end end
) )
local _load_bookmarks = ya.sync(function(state) local _load_state = ya.sync(function(state)
ya.err("Persist Bookmarks") ya.err("Persist State")
ps.sub_remote("bookmarks", function(body) ps.sub_remote("bookmarks", function(body)
if not state.bookmarks and body then if not state.bookmarks and body then
state.indexes = {}
state.bookmarks = {} state.bookmarks = {}
for key, value in pairs(body.indexes) do
for _, value in pairs(body) do state.indexes[tonumber(key)] = value
table.insert(state.bookmarks, value) end
for key, value in pairs(body.bookmarks) do
state.bookmarks[tonumber(key)] = value
end end
end end
end) end)
end) end)
local _save_bookmark = ya.sync(function(state, bookmarks) local _save_state = ya.sync(function(state, bookmarks, indexes)
ya.err("Save Bookmark") ya.err("Save State")
if not bookmarks then if not bookmarks then
ps.pub_static(10, "bookmarks", nil) ps.pub_static(10, "bookmarks", nil)
return return
end end
local save_state = {}
if state.persist == "all" then if state.persist == "all" then
ps.pub_static(10, "bookmarks", bookmarks) save_state = { bookmarks = bookmarks, indexes = indexes }
return else -- VIM mode
end
-- VIM mode
local save_bookmarks = {} local save_bookmarks = {}
for _, value in pairs(bookmarks) do local save_indexes = {}
for key, value in pairs(bookmarks) do
-- Only save bookmarks in upper case keys -- Only save bookmarks in upper case keys
if string.match(value.on, "%u") then if string.match(value.on, "%u") then
table.insert(save_bookmarks, value) table.insert(save_bookmarks, value)
table.insert(save_indexes, indexes[key])
end end
end end
ps.pub_static(10, "bookmarks", save_bookmarks)
save_state = { bookmarks = save_bookmarks, indexes = save_indexes }
end
ya.err("SAVE STATE: " .. serialize(save_state))
ps.pub_static(10, "bookmarks", save_state)
end) end)
local _save_last_directory = ya.sync(function(state) local _save_last_directory = ya.sync(function(state)
@@ -98,8 +106,11 @@ local save_bookmark = ya.sync(function(state, idx)
cursor = folder.cursor, cursor = folder.cursor,
} }
ya.err("INDEXES " .. serialize(state.indexes))
ya.err("BOOKMARKS " .. serialize(state.bookmarks))
if state.persist then if state.persist then
_save_bookmark(state.bookmarks) _save_state(state.bookmarks, state.indexes)
end end
if state.notify and state.notify.enable then if state.notify and state.notify.enable then
@@ -137,7 +148,7 @@ local delete_bookmark = ya.sync(function(state, idx)
table.remove(state.bookmarks, idx) table.remove(state.bookmarks, idx)
if state.persist then if state.persist then
_save_bookmark(state.bookmarks) _save_state(state.bookmarks, state.indexes)
end end
end) end)
@@ -145,7 +156,7 @@ local delete_all_bookmarks = ya.sync(function(state)
state.bookmarks = nil state.bookmarks = nil
if state.persist then if state.persist then
_save_bookmark(nil) _save_state(nil)
end end
if state.notify and state.notify.enable then if state.notify and state.notify.enable then
@@ -197,7 +208,7 @@ return {
if args.persist == "all" or args.persist == "vim" then if args.persist == "all" or args.persist == "vim" then
state.persist = args.persist state.persist = args.persist
_load_bookmarks() _load_state()
end end
state.notify = { state.notify = {