From 48162b981689c58e54ad44f45a9ba4b70a1c68d7 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Mon, 25 Jul 2022 19:56:39 -0700 Subject: [PATCH] have file API operate on state object --- drawing_tests.lua | 16 ++++++++-------- edit.lua | 4 ++-- file.lua | 15 +++++++-------- main.lua | 6 +++--- 4 files changed, 20 insertions(+), 21 deletions(-) diff --git a/drawing_tests.lua b/drawing_tests.lua index 5301318..f1e39a6 100644 --- a/drawing_tests.lua +++ b/drawing_tests.lua @@ -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 diff --git a/edit.lua b/edit.lua index 1b39dbb..e2d2dd9 100644 --- a/edit.lua +++ b/edit.lua @@ -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 diff --git a/file.lua b/file.lua index c5946d6..9440ae8 100644 --- a/file.lua +++ b/file.lua @@ -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 diff --git a/main.lua b/main.lua index 5cf8f08..ce8e778 100644 --- a/main.lua +++ b/main.lua @@ -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