--[[ the main user interface for memoire ]] local Prompt = dofile "prompt.lua" local recrw = dofile "recrw.lua" local review = dofile "review.lua" --[[ load a deck (a table of tables of `Prompt`s) from a Recfile ]] local function loaddeck(fname) local fh = io.open(fname) if not fh then return nil end local rt = fh:read "a" if not rt then return nil end fh:close() local pr = recrw.read(rt) if not pr then return nil end local ep = Prompt:fromrecs(pr) return ep end --[[ save a deck (a table of tables of `Prompt`s) to a Recfile ]] local function savedeck(fname, deck) local rrf = {} for i, cg in ipairs(deck) do rrf[i] = Prompt:torec(cg) end local wot = recrw.write(rrf) local fh = io.open(fname, "w") if not fh then io.write("couldn't open output\n") end local wr, err = fh:write(wot) if not wr then io.write("couldn't write to output\n") end fh:close() end --[[ checks (returns boolean) if a response-string is affirmative ]] local function isyes(s) local f = s:sub(1, 1):lower() return f == "y" or f == "t" end --[[ clears the terminal implementation may be platform-specific ]] local function clear() -- XTerm io.write("\x1bc") end --[[ check action based on first command-line argument; if nothing valid given, default to help ]] local action = nil if arg[1] then local fc = arg[1]:sub(1, 1) if fc == "a" or fc == "h" or fc == "r" or fc == "s" then action = fc else action = "h" end else action = "h" end --[[ repository file is hardcoded for now; TODO: should be controllable by user in the future ]] local deckfname = "example.rec" --[[ do requested action ]] -- Add card(s) if action == "a" then -- TODO -- Help elseif action == "h" then io.write(string.format( "Usage: %s ACTION\n\nACTION may be one of\n (a)dd card\n (h)elp\n (r)eview session\n (s)tatistics\n", arg[0] )) -- Review session elseif action == "r" then local deck = loaddeck(deckfname) -- review each card that needs it for i, cg in ipairs(deck) do for j, pr in ipairs(cg) do if review.should(pr) then local st = os.time() io.write(string.format( "%s\n", pr.text:gsub("%%A", "___") )) io.read("l") io.write(string.format( "%s\nDid you get it? ", pr.text:gsub("%%A", pr.ans) )) local resp = io.read("l") review.update(pr, st, isyes(resp)) clear() end end end savedeck(deckfname, deck) -- Statistics elseif action == "s" then -- TODO else io.write "unreachable" end