DKL9 GitList
Repositories
DKL9 home
memoire
Code
Commits
Branches
Tags
Search
Tree:
f8683b4
Branches
Tags
master
memoire
main.lua
Implement add feature in main interface
John Smith
commited
f8683b4
at 2021-347 21:18:55
main.lua
Blame
History
Raw
--[[ 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 local deck = loaddeck(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)", cardctr, prctr )) if cardctr > 0 then savedeck(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 = loaddeck(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 cards in %d seconds\n", cardcount, endt - start )) if cardcount > 0 then io.write(string.format( "(%f seconds per card)\n", (endt - start) / cardcount )) savedeck(deckfname, deck) end -- Statistics elseif action == "s" then -- TODO else io.write "unreachable" end