Refactor `main.lua`
John Smith

John Smith commited on 2021-342 21:48:59
Showing 1 changed files, with 18 additions and 13 deletions.


`savedeck` is now a separate function,
rather than code embedded at the top level.
... ...
@@ -7,7 +7,7 @@ local recrw = dofile "recrw.lua"
7 7
 local review = dofile "review.lua"
8 8
 
9 9
 --[[
10
-    load a deck (a table of tables of `Prompts`) from a Recfile
10
+    load a deck (a table of tables of `Prompt`s) from a Recfile
11 11
 ]]
12 12
 local function loaddeck(fname)
13 13
     local fh = io.open(fname)
... ...
@@ -21,6 +21,22 @@ local function loaddeck(fname)
21 21
     return ep
22 22
 end
23 23
 
24
+--[[
25
+    save a deck (a table of tables of `Prompt`s) to a Recfile
26
+]]
27
+local function savedeck(fname, deck)
28
+    local rrf = {}
29
+    for i, cg in ipairs(deck) do
30
+        rrf[i] = Prompt:torec(cg)
31
+    end
32
+    local wot = recrw.write(rrf)
33
+    local fh = io.open(fname, "w")
34
+    if not fh then io.write("couldn't open output\n") end
35
+    local wr, err = fh:write(wot)
36
+    if not wr then io.write("couldn't write to output\n") end
37
+    fh:close()
38
+end
39
+
24 40
 --[[
25 41
     checks (returns boolean) if a response-string is affirmative
26 42
 ]]
... ...
@@ -95,18 +111,7 @@ elseif action == "r" then
95 111
             end
96 112
         end
97 113
     end
98
-    -- write out updated cards
99
-    -- TODO: split this out into a function
100
-    local rrf = {}
101
-    for i, cg in ipairs(deck) do
102
-        rrf[i] = Prompt:torec(cg)
103
-    end
104
-    local wot = recrw.write(rrf)
105
-    local wfh = io.open(deckfname, "w")
106
-    if not wfh then io.write("couldn't open output\n") end
107
-    local _wr, wr = wfh:write(wot)
108
-    if not _wr then io.write("couldn't write to output\n") end
109
-    wfh:close()
114
+    savedeck(deckfname, deck)
110 115
 -- Statistics
111 116
 elseif action == "s" then
112 117
     -- TODO
113 118