--[[ 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() -- ANSI io.write("\x1b[H\x1b[J") end --[[ display text with images, inserted with `IMG[path]` implementation may be platform-specific `basepath` points to the base directory of the images ]] local function imgify(text, basepath, conf) if conf:get("BoldContext") then local csi, cei = text:find("^[^:]+:") local cpt = text:sub(csi, cei) io.write("\x1b[1m") io.write(cpt) io.write("\x1b[0m") text = text:sub(cei + 1) end -- adapted from https://stackoverflow.com/a/1579673 local IMG_PATTERN = "IMG%[([^%]]+)%]" local PATTERN = "(.-)" .. IMG_PATTERN -- Linux/dwm local DISPIMG = function(path, st) if path and #path > 0 then os.execute(string.format( "/usr/local/bin/iv -t '%s' %s%s &", st:gsub("'", "\""):gsub(IMG_PATTERN, ""), basepath, path )) end end local lastend = 1 local mstart, mend, prevtext, imgpath = text:find(PATTERN) io.write(prevtext or "") DISPIMG(imgpath, text) while mstart do lastend = mend + 1 mstart, mend, prevtext, imgpath = text:find(PATTERN, lastend) io.write(prevtext or "") DISPIMG(imgpath, text) end if lastend <= #text then io.write(text:sub(lastend)) end end --[[ rounds `x` to `n` places (default 0) after the radix point in base `b` (default 10) ]] local function round(x, n, b) local b = b or 10 local n = n or 0 if n == 0 then return math.tointeger(math.floor(x * b ^ n) / b ^ n) else return math.floor(x * b ^ n) / b ^ n end end --[[ return human-friendly string to present a duration (input in seconds), e.g. "44:37", "4.8h", "11d", "8.3m", "17.4y" ]] local function disptime(ts) local YDAY = 365.24 local MDAY = YDAY / 12 local DSEC = 86400 local HSEC = 3600 if ts >= 3 * YDAY * DSEC then return round(ts / (YDAY * DSEC), 1) .. "y" elseif ts >= 4 * MDAY * DSEC then return round(ts / (MDAY * DSEC), 1) .. "m" elseif ts >= 10 * DSEC then return round(ts / DSEC) .. "d" elseif ts >= 1.5 * DSEC then return round(ts / DSEC, 1) .. "d" elseif ts >= 8 * HSEC then return round(ts / HSEC) .. "h" elseif ts >= HSEC then return round(ts / HSEC, 1) .. "h" else return string.format("%d:%02d", round(ts / 60), round(ts % 60)) end end --[[ check if standard input is a terminal ]] local function isatty() local succ = os.execute("tty >/dev/null") return succ end --[[ return number indicating how much of a leech the prompt is ]] local function badness(pr) return pr.fail / (pr.fail + pr.succ) * math.log(pr.fail) end local actions = { a = { desc = "(a)dd card", nd = true, fn = function(config, deck) if isatty() then io.write("Reading prompts; press Ctrl-D to stop\n") else io.write("Reading prompts ...\n") end 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(), config:get("StartingEase") or 2.5)) 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 return deck end end, }, h = { desc = "(h)elp", nd = false, fn = function() io.write(string.format("Usage: %s ACTION\n\nACTION may be one of\n", arg[0])) local sal = {} for _, v in pairs(ACTIONS) do table.insert(sal, v.desc) end table.sort(sal) for _, v in ipairs(sal) do io.write(string.format("\t%s\n", v)) end end }, l = { desc = "(l)eech search", nd = true, fn = function(config, deck) local leechqueue = {} for i, cg in ipairs(deck) do for j, pr in ipairs(cg) do if pr.fail > 4 and pr.ease < 2 and pr.delay >= 0 then table.insert(leechqueue, { i, j }) end end end table.sort(leechqueue, function(a, b) return badness(deck[a[1]][a[2]]) > badness(deck[b[1]][b[2]]) end) io.write(string.format("Detected %d leeches:\n", #leechqueue)) for _, ij in ipairs(leechqueue) do io.write(string.format("%d:%d %s\n", ij[1], ij[2], deck[ij[1]][ij[2]])) end end, }, r = { desc = "(r)eview sesion", nd = true, fn = function(config, deck, deckfname) if not isatty() then io.write("Review should be done in the terminal\n") return nil end -- 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 )) io.flush() local climt = io.read("l") local climn = tonumber(climt) if not climn then io.write("Input not a number; aborting\n") return nil end local lrc = 0 local editqueue = {} 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() imgify(pst:gsub("%%A", "___"), deckfname:match(".*/") or "", config) io.write("\n") io.read("l") imgify(pst:gsub("%%A", (pr.ans:gsub("%%", "%%%%"))), deckfname:match(".*/") or "", config) io.write(string.format( "\nAgain / Hard (%s) / Good (%s) / Easy (%s) / Correction ", disptime(review.schedule(pr, st, 0.5, config)), disptime(review.schedule(pr, st, 1, config)), disptime(review.schedule(pr, st, 1.5, config)) )) io.flush() local resp = io.read("l") local sc = succfromstr(resp) if sc == -1 then table.insert(editqueue, ip[1]) else if sc == 0 then lrc = lrc + 1 end review.update(deck[ip[1]][ip[2]], st, sc, config) end clear() end if #ctrt > 0 then local endt = os.time() io.write(string.format( "Reviewed %d prompts in %d seconds\n", math.min(#ctrt, climn), endt - start )) io.write(string.format( "(%d lapses, %f seconds per card)\n", lrc, (endt - start) / math.min(#ctrt, climn) )) end if #editqueue > 0 then io.write(string.format( "\n%d cards queued for editing; press Enter to proceed", #editqueue )) io.flush() io.read("l") for ind, ci in ipairs(editqueue) do local pr = deck[ci][1] if pr then io.write(string.format( "Card #%d:\n%s\nCancel correction / Delete / Edit ", ci, pr:spt() )) io.flush() local resp = io.read("l"):sub(1, 1):lower() if resp == "d" then deck[ci][1].deleted = true elseif resp == "e" then io.write("Enter edited version:\n") local ev = io.read("l") local cardrec = Prompt:torec(deck[ci]) cardrec.PromptText = { ev } deck[ci] = Prompt:fromrecs({ cardrec }, config)[1] -- assume all other inputs to mean Cancel else end else io.write(string.format("Card #%d is invalid!\n", ci)) end end end if climn > 0 then return deck end end, }, s = { desc = "(s)tatistics", nd = false, fn = function() io.write("Not yet implemented ...\n") end }, t = { desc = "(t)oggle suspension of prompt", nd = true, fn = function(config, deck) local ip if arg[2] then local a, b = arg[2]:match("(%d+):(%d+)") if a and b then -- a and b are valid numbers because they came from a match ip = { tonumber(a), tonumber(b) } end end if not ip then io.write("Bad prompt index (must be cardnum:promptnum)\n") return nil end local sp = deck[ip[1]][ip[2]] local dir if sp.delay < 0 then dir = "Restoring" else dir = "Suspending" end io.write(string.format("%s %s\n", dir, sp)) sp.delay = -sp.delay return deck end, }, } return actions