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)
-- The format on disk isn't perfectly stable. Table fields can be reordered.
-- 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)
local drawing = Editor_state.lines[1]
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)
edit.update(Editor_state, 0)
-- change is saved
Editor_state.lines = load_from_disk(Editor_state.filename)
load_from_disk(Editor_state)
Text.redraw_all(Editor_state)
local p2 = Editor_state.lines[1].points[drawing.shapes[1].p2]
check_eq(p2.name, 'A', 'F - test_name_point/save')
@ -465,7 +465,7 @@ function test_move_point()
App.wait_fake_time(3.1)
edit.update(Editor_state, 0)
-- line is saved to disk
Editor_state.lines = load_from_disk(Editor_state.filename)
load_from_disk(Editor_state)
Text.redraw_all(Editor_state)
local drawing = Editor_state.lines[1]
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)
edit.update(Editor_state, 0)
-- change is saved
Editor_state.lines = load_from_disk(Editor_state.filename)
load_from_disk(Editor_state)
Text.redraw_all(Editor_state)
local p2 = Editor_state.lines[1].points[drawing.shapes[1].p2]
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)
edit.update(Editor_state, 0)
-- 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)
check_eq(#Editor_state.lines[1].shapes, 0, 'F - test_delete_lines_at_point/save')
end
@ -687,7 +687,7 @@ function test_undo_name_point()
App.wait_fake_time(3.1)
edit.update(Editor_state, 0)
-- undo is saved
Editor_state.lines = load_from_disk(Editor_state.filename)
load_from_disk(Editor_state)
Text.redraw_all(Editor_state)
local p2 = Editor_state.lines[1].points[drawing.shapes[1].p2]
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)
edit.update(Editor_state, 0)
-- undo is saved
Editor_state.lines = load_from_disk(Editor_state.filename)
load_from_disk(Editor_state)
Text.redraw_all(Editor_state)
local p2 = Editor_state.lines[1].points[drawing.shapes[1].p2]
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)
edit.update(Editor_state, 0)
-- undo is saved
Editor_state.lines = load_from_disk(Editor_state.filename)
load_from_disk(Editor_state)
Text.redraw_all(Editor_state)
check_eq(#Editor_state.lines[1].shapes, 2, 'F - test_undo_delete_point/save')
end

View File

@ -188,7 +188,7 @@ end
function edit.update(State, dt)
Drawing.update(State, dt)
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
end
end
@ -202,7 +202,7 @@ end
function edit.quit(State)
-- make sure to save before quitting
if State.next_save then
save_to_disk(State.lines, State.filename)
save_to_disk(State)
end
end

View File

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

View File

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