have file API operate on state object

This commit is contained in:
Kartik K. Agaram 2022-07-25 19:56:39 -07:00
parent e26470aada
commit 48162b9816
4 changed files with 20 additions and 21 deletions

View File

@ -55,7 +55,7 @@ function test_draw_line()
edit.update(Editor_state, 0) edit.update(Editor_state, 0)
-- The format on disk isn't perfectly stable. Table fields can be reordered. -- The format on disk isn't perfectly stable. Table fields can be reordered.
-- So just reload from disk to verify. -- So just reload from disk to verify.
Editor_state.lines = load_from_disk(Editor_state.filename) load_from_disk(Editor_state)
Text.redraw_all(Editor_state) Text.redraw_all(Editor_state)
local drawing = Editor_state.lines[1] local drawing = Editor_state.lines[1]
check_eq(#drawing.shapes, 1, 'F - test_draw_line/save/#shapes') check_eq(#drawing.shapes, 1, 'F - test_draw_line/save/#shapes')
@ -433,7 +433,7 @@ function test_name_point()
App.wait_fake_time(3.1) App.wait_fake_time(3.1)
edit.update(Editor_state, 0) edit.update(Editor_state, 0)
-- change is saved -- change is saved
Editor_state.lines = load_from_disk(Editor_state.filename) load_from_disk(Editor_state)
Text.redraw_all(Editor_state) Text.redraw_all(Editor_state)
local p2 = Editor_state.lines[1].points[drawing.shapes[1].p2] local p2 = Editor_state.lines[1].points[drawing.shapes[1].p2]
check_eq(p2.name, 'A', 'F - test_name_point/save') check_eq(p2.name, 'A', 'F - test_name_point/save')
@ -465,7 +465,7 @@ function test_move_point()
App.wait_fake_time(3.1) App.wait_fake_time(3.1)
edit.update(Editor_state, 0) edit.update(Editor_state, 0)
-- line is saved to disk -- line is saved to disk
Editor_state.lines = load_from_disk(Editor_state.filename) load_from_disk(Editor_state)
Text.redraw_all(Editor_state) Text.redraw_all(Editor_state)
local drawing = Editor_state.lines[1] local drawing = Editor_state.lines[1]
local p2 = Editor_state.lines[1].points[drawing.shapes[1].p2] local p2 = Editor_state.lines[1].points[drawing.shapes[1].p2]
@ -492,7 +492,7 @@ function test_move_point()
App.wait_fake_time(3.1) App.wait_fake_time(3.1)
edit.update(Editor_state, 0) edit.update(Editor_state, 0)
-- change is saved -- change is saved
Editor_state.lines = load_from_disk(Editor_state.filename) load_from_disk(Editor_state)
Text.redraw_all(Editor_state) Text.redraw_all(Editor_state)
local p2 = Editor_state.lines[1].points[drawing.shapes[1].p2] local p2 = Editor_state.lines[1].points[drawing.shapes[1].p2]
check_eq(p2.x, 26, 'F - test_move_point/save/x') check_eq(p2.x, 26, 'F - test_move_point/save/x')
@ -553,7 +553,7 @@ function test_delete_lines_at_point()
App.wait_fake_time(3.1) App.wait_fake_time(3.1)
edit.update(Editor_state, 0) edit.update(Editor_state, 0)
-- deleted points disappear after file is reloaded -- deleted points disappear after file is reloaded
Editor_state.lines = load_from_disk(Editor_state.filename) load_from_disk(Editor_state)
Text.redraw_all(Editor_state) Text.redraw_all(Editor_state)
check_eq(#Editor_state.lines[1].shapes, 0, 'F - test_delete_lines_at_point/save') check_eq(#Editor_state.lines[1].shapes, 0, 'F - test_delete_lines_at_point/save')
end end
@ -687,7 +687,7 @@ function test_undo_name_point()
App.wait_fake_time(3.1) App.wait_fake_time(3.1)
edit.update(Editor_state, 0) edit.update(Editor_state, 0)
-- undo is saved -- undo is saved
Editor_state.lines = load_from_disk(Editor_state.filename) load_from_disk(Editor_state)
Text.redraw_all(Editor_state) Text.redraw_all(Editor_state)
local p2 = Editor_state.lines[1].points[drawing.shapes[1].p2] local p2 = Editor_state.lines[1].points[drawing.shapes[1].p2]
check_eq(p2.name, '', 'F - test_undo_name_point/save') check_eq(p2.name, '', 'F - test_undo_name_point/save')
@ -738,7 +738,7 @@ function test_undo_move_point()
App.wait_fake_time(3.1) App.wait_fake_time(3.1)
edit.update(Editor_state, 0) edit.update(Editor_state, 0)
-- undo is saved -- undo is saved
Editor_state.lines = load_from_disk(Editor_state.filename) load_from_disk(Editor_state)
Text.redraw_all(Editor_state) Text.redraw_all(Editor_state)
local p2 = Editor_state.lines[1].points[drawing.shapes[1].p2] local p2 = Editor_state.lines[1].points[drawing.shapes[1].p2]
check_eq(p2.x, 35, 'F - test_undo_move_point/save/x') check_eq(p2.x, 35, 'F - test_undo_move_point/save/x')
@ -779,7 +779,7 @@ function test_undo_delete_point()
App.wait_fake_time(3.1) App.wait_fake_time(3.1)
edit.update(Editor_state, 0) edit.update(Editor_state, 0)
-- undo is saved -- undo is saved
Editor_state.lines = load_from_disk(Editor_state.filename) load_from_disk(Editor_state)
Text.redraw_all(Editor_state) Text.redraw_all(Editor_state)
check_eq(#Editor_state.lines[1].shapes, 2, 'F - test_undo_delete_point/save') check_eq(#Editor_state.lines[1].shapes, 2, 'F - test_undo_delete_point/save')
end end

View File

@ -188,7 +188,7 @@ end
function edit.update(State, dt) function edit.update(State, dt)
Drawing.update(State, dt) Drawing.update(State, dt)
if State.next_save and State.next_save < App.getTime() then if State.next_save and State.next_save < App.getTime() then
save_to_disk(State.lines, State.filename) save_to_disk(State)
State.next_save = nil State.next_save = nil
end end
end end
@ -202,7 +202,7 @@ end
function edit.quit(State) function edit.quit(State)
-- make sure to save before quitting -- make sure to save before quitting
if State.next_save then if State.next_save then
save_to_disk(State.lines, State.filename) save_to_disk(State)
end end
end end

View File

@ -1,9 +1,8 @@
-- primitives for saving to file and loading from file -- primitives for saving to file and loading from file
function load_from_disk(filename) function load_from_disk(State)
local infile = App.open_for_reading(filename) local infile = App.open_for_reading(State.filename)
local result = load_from_file(infile) State.lines = load_from_file(infile)
if infile then infile:close() end if infile then infile:close() end
return result
end end
function load_from_file(infile) function load_from_file(infile)
@ -26,12 +25,12 @@ function load_from_file(infile)
return result return result
end end
function save_to_disk(lines, filename) function save_to_disk(State)
local outfile = App.open_for_writing(filename) local outfile = App.open_for_writing(State.filename)
if outfile == nil then if outfile == nil then
error('failed to write to "'..filename..'"') error('failed to write to "'..State.filename..'"')
end end
for _,line in ipairs(lines) do for _,line in ipairs(State.lines) do
if line.mode == 'drawing' then if line.mode == 'drawing' then
store_drawing(outfile, line) store_drawing(outfile, line)
else else

View File

@ -39,13 +39,13 @@ function App.initialize(arg)
if #arg > 0 then if #arg > 0 then
Editor_state.filename = arg[1] Editor_state.filename = arg[1]
Editor_state.lines = load_from_disk(Editor_state.filename) load_from_disk(Editor_state)
Text.redraw_all(Editor_state) Text.redraw_all(Editor_state)
Editor_state.screen_top1 = {line=1, pos=1} Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.cursor1 = {line=1, pos=1} Editor_state.cursor1 = {line=1, pos=1}
edit.fixup_cursor(Editor_state) edit.fixup_cursor(Editor_state)
else else
Editor_state.lines = load_from_disk(Editor_state.filename) load_from_disk(Editor_state)
Text.redraw_all(Editor_state) Text.redraw_all(Editor_state)
edit.fixup_cursor(Editor_state) edit.fixup_cursor(Editor_state)
end end
@ -117,7 +117,7 @@ end
function App.filedropped(file) function App.filedropped(file)
-- first make sure to save edits on any existing file -- first make sure to save edits on any existing file
if Editor_state.next_save then if Editor_state.next_save then
save_to_disk(Editor_state.lines, Editor_state.filename) save_to_disk(Editor_state)
end end
-- clear the slate for the new file -- clear the slate for the new file
App.initialize_globals() -- in particular, forget all undo history App.initialize_globals() -- in particular, forget all undo history