--[[ the main user interface for memoire ]] local Prompt = dofile "prompt.lua" local review = dofile "review.lua" local loader = dofile "loader.lua" --[[ gets the score (again/hard/good/easy/correction) from a string by first character scores are numbers, higher is easier, -1 is correction ]] local function succfromstr(s) local f = s:sub(1, 1):lower() if f == "0" or f == "a" then return 0 elseif f == "1" or f == "h" then return 0.5 elseif f == "2" or f == "g" then return 1 elseif f == "3" or f == "e" then return 1.5 elseif f == "-" or f == "c" then return -1 end return 1 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 local deckfname = "example.rec" if arg[2] then deckfname = arg[2] end --[[ do requested action ]] -- Add card(s) if action == "a" then local deck = loader.load(deckfname) if deck then 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, os.time())) 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 else io.write("Failed to load deck\n") end -- Help elseif action == "h" then io.write(string.format( "Usage: %s ACTION [REPO]\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) if deck then -- collect list of index-pairs for prompts to be reviewed this session local ctrt = review.list(deck) io.write(string.format( "%d prompts are ready for review.\nHow many to review in this session? ", #ctrt )) local climt = io.read("l") local climn = tonumber(climt) if climn then local start = os.time() -- review prompts in this session's set for ind, ip in ipairs(ctrt) do if ind > climn then break end local pr = deck[ip[1]][ip[2]] -- 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\nAgain / Hard / Good / Easy / Correction ", pst:gsub("%%A", (pr.ans:gsub("%%", "%%%%"))) )) local resp = io.read("l") local sc = succfromstr(resp) if sc == -1 then deck[ip[1]][ip[2]] = deck[ip[1]][ip[2]] -- TODO: edit card else review.update(deck[ip[1]][ip[2]], st, sc) end clear() end local endt = os.time() io.write(string.format( "Reviewed %d prompts in %d seconds\n", math.min(#ctrt, climn), endt - start )) if #ctrt > 0 then io.write(string.format( "(%f seconds per card)\n", (endt - start) / math.min(#ctrt, climn) )) loader.save(deckfname, deck) end else io.write("Input not a number -- aborting\n") end else io.write("Failed to load deck\n") end -- Statistics elseif action == "s" then -- TODO else io.write "unreachable" end