John Smith commited on 2021-349 21:53:44
Showing 2 changed files, with 44 additions and 36 deletions.
`main.lua`'s `loaddeck` and `savedeck` functions have been moved out into `loader.lua`.
| ... | ... |
@@ -0,0 +1,39 @@ |
| 1 |
+--[[ |
|
| 2 |
+ deck loading and saving functions (working with Recfiles) |
|
| 3 |
+]] |
|
| 4 |
+ |
|
| 5 |
+local Prompt = dofile "prompt.lua" |
|
| 6 |
+local recrw = dofile "recrw.lua" |
|
| 7 |
+ |
|
| 8 |
+--[[ |
|
| 9 |
+ load a deck (a table of tables of `Prompt`s) from a Recfile |
|
| 10 |
+]] |
|
| 11 |
+local function loaddeck(fname) |
|
| 12 |
+ local fh = io.open(fname) |
|
| 13 |
+ if not fh then return nil end |
|
| 14 |
+ local rt = fh:read "a" |
|
| 15 |
+ if not rt then return nil end |
|
| 16 |
+ fh:close() |
|
| 17 |
+ local pr = recrw.read(rt) |
|
| 18 |
+ if not pr then return nil end |
|
| 19 |
+ local ep = Prompt:fromrecs(pr) |
|
| 20 |
+ return ep |
|
| 21 |
+end |
|
| 22 |
+ |
|
| 23 |
+--[[ |
|
| 24 |
+ save a deck (a table of tables of `Prompt`s) to a Recfile |
|
| 25 |
+]] |
|
| 26 |
+local function savedeck(fname, deck) |
|
| 27 |
+ local rrf = {}
|
|
| 28 |
+ for i, cg in ipairs(deck) do |
|
| 29 |
+ rrf[i] = Prompt:torec(cg) |
|
| 30 |
+ end |
|
| 31 |
+ local wot = recrw.write(rrf) |
|
| 32 |
+ local fh = io.open(fname, "w") |
|
| 33 |
+ if not fh then io.write("couldn't open output\n") end
|
|
| 34 |
+ local wr, err = fh:write(wot) |
|
| 35 |
+ if not wr then io.write("couldn't write to output\n") end
|
|
| 36 |
+ fh:close() |
|
| 37 |
+end |
|
| 38 |
+ |
|
| 39 |
+return { load = loaddeck, save = savedeck }
|
| ... | ... |
@@ -3,39 +3,8 @@ |
| 3 | 3 |
]] |
| 4 | 4 |
|
| 5 | 5 |
local Prompt = dofile "prompt.lua" |
| 6 |
-local recrw = dofile "recrw.lua" |
|
| 7 | 6 |
local review = dofile "review.lua" |
| 8 |
- |
|
| 9 |
---[[ |
|
| 10 |
- load a deck (a table of tables of `Prompt`s) from a Recfile |
|
| 11 |
-]] |
|
| 12 |
-local function loaddeck(fname) |
|
| 13 |
- local fh = io.open(fname) |
|
| 14 |
- if not fh then return nil end |
|
| 15 |
- local rt = fh:read "a" |
|
| 16 |
- if not rt then return nil end |
|
| 17 |
- fh:close() |
|
| 18 |
- local pr = recrw.read(rt) |
|
| 19 |
- if not pr then return nil end |
|
| 20 |
- local ep = Prompt:fromrecs(pr) |
|
| 21 |
- return ep |
|
| 22 |
-end |
|
| 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 |
|
| 7 |
+local loader = dofile "loader.lua" |
|
| 39 | 8 |
|
| 40 | 9 |
--[[ |
| 41 | 10 |
checks (returns boolean) if a response-string is affirmative |
| ... | ... |
@@ -81,7 +50,7 @@ local deckfname = "example.rec" |
| 81 | 50 |
]] |
| 82 | 51 |
-- Add card(s) |
| 83 | 52 |
if action == "a" then |
| 84 |
- local deck = loaddeck(deckfname) |
|
| 53 |
+ local deck = loader.load(deckfname) |
|
| 85 | 54 |
io.write("Reading prompts -- press Ctrl-D to stop\n")
|
| 86 | 55 |
local rt = io.read("a")
|
| 87 | 56 |
local cardctr = 0 |
| ... | ... |
@@ -102,7 +71,7 @@ if action == "a" then |
| 102 | 71 |
prctr |
| 103 | 72 |
)) |
| 104 | 73 |
if cardctr > 0 then |
| 105 |
- savedeck(deckfname, deck) |
|
| 74 |
+ loader.save(deckfname, deck) |
|
| 106 | 75 |
end |
| 107 | 76 |
-- Help |
| 108 | 77 |
elseif action == "h" then |
| ... | ... |
@@ -112,7 +81,7 @@ elseif action == "h" then |
| 112 | 81 |
)) |
| 113 | 82 |
-- Review session |
| 114 | 83 |
elseif action == "r" then |
| 115 |
- local deck = loaddeck(deckfname) |
|
| 84 |
+ local deck = loader.load(deckfname) |
|
| 116 | 85 |
local cardcount = 0 |
| 117 | 86 |
local start = os.time() |
| 118 | 87 |
-- review each card that needs it |
| ... | ... |
@@ -149,7 +118,7 @@ elseif action == "r" then |
| 149 | 118 |
"(%f seconds per card)\n", |
| 150 | 119 |
(endt - start) / cardcount |
| 151 | 120 |
)) |
| 152 |
- savedeck(deckfname, deck) |
|
| 121 |
+ loader.save(deckfname, deck) |
|
| 153 | 122 |
end |
| 154 | 123 |
-- Statistics |
| 155 | 124 |
elseif action == "s" then |
| 156 | 125 |