chunk up the saved file to be git friendly

Now we don't save a bunch of irrelevant fields from editor buffers, just
lines.

The cost: autosave every 3 seconds becomes much more expensive.
This commit is contained in:
Kartik K. Agaram 2023-04-20 21:52:53 -07:00
parent fe560e9a6c
commit 0fd71eceb9
4 changed files with 33 additions and 3 deletions

View File

@ -3,6 +3,7 @@ initialize_editor = function(obj)
local scaled_fontsize = scale(20)
local scaled_lineheight = math.floor(scaled_fontsize*1.3)
obj.editor = edit.initialize_state(vy(obj.y), math.floor(vx(obj.x)), math.ceil(vx(obj.x+obj.w)), scaled_fontsize, scaled_lineheight)
obj.editor.lines = load_array(obj.data)
Text.redraw_all(obj.editor)
end
end

View File

@ -1,5 +1,27 @@
save_graph_to_disk = function()
local data = {next=First_available_id}
-- save a copy of Nodes with various fields deleted
local kvs = {}
for k,v in pairs(Nodes) do
v = table.copy(v)
v.data = {}
for i,l in ipairs(v.editor.lines) do
table.insert(v.data, l.data)
end
v.editor = nil
table.insert(kvs, {k, v})
end
-- sort the copy to avoid spurious diffs in version control
table.sort(kvs, function(a, b) return a[1] < b[1] end)
data.nodes = Nodes
local f = App.open_for_writing(Filename)
f:write(json.encode({next=First_available_id, nodes=Nodes}))
f:write('{ "next" : '..First_available_id..', "nodes" : {\n')
for i,kv in ipairs(kvs) do
if i > 1 then
f:write(',')
end
f:write('"'..kv[1]..'": '..json.encode(kv[2])..'\n')
end
f:write('}}\n')
f:close()
end
end

View File

@ -4,4 +4,4 @@ load_graph_from_disk = function()
f:close()
Nodes = data.nodes or {}
First_available_id = data.next
end
end

7
0068-table.copy Normal file
View File

@ -0,0 +1,7 @@
table.copy = function(h)
local result = {}
for k,v in pairs(h) do
result[k] = v
end
return result
end