DKL9 GitList
Repositories
DKL9 home
memoire
Code
Commits
Branches
Tags
Search
Tree:
b75c890
Branches
Tags
master
memoire
main.lua
Split Recfile reading/writing out from `main.lua`
John Smith
commited
b75c890
at 2021-349 21:53:44
main.lua
Blame
History
Raw
--[[ the main user interface for memoire ]] local Prompt = dofile "prompt.lua" local review = dofile "review.lua" local loader = dofile "loader.lua" --[[ 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 local deck = loader.load(deckfname) io.write("Reading prompts -- press Ctrl-D to stop\n") local rt = io.read("a") local cardctr = 0 local prctr = 0 for pt in rt:gmatch("(..-)\n\n") do local nc = {} -- these Prompts will just be converted to Rec, so we don't need to exclude answers from prompt-text for _re in pt:gmatch("%{[^%}]+%}") do table.insert(nc, Prompt:new(pt, "", os.time(), 30, 0, 0)) prctr = prctr + 1 end table.insert(deck, nc) cardctr = cardctr + 1 end io.write(string.format( "Added %d cards (%d prompts)\n", cardctr, prctr )) if cardctr > 0 then loader.save(deckfname, deck) end -- 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 = loader.load(deckfname) local cardcount = 0 local start = os.time() -- 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 -- remove braces for display local pst = pr.text:gsub("%{([^{}]+)%}", "%1") local st = os.time() io.write(string.format( "%s\n", pst:gsub("%%A", "___") )) io.read("l") io.write(string.format( "%s\nDid you get it? ", pst:gsub("%%A", pr.ans) )) local resp = io.read("l") review.update(pr, st, isyes(resp)) cardcount = cardcount + 1 clear() end end end local endt = os.time() io.write(string.format( "Reviewed %d prompts in %d seconds\n", cardcount, endt - start )) if cardcount > 0 then io.write(string.format( "(%f seconds per card)\n", (endt - start) / cardcount )) loader.save(deckfname, deck) end -- Statistics elseif action == "s" then -- TODO else io.write "unreachable" end